Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'attribute': 0.07; 'subject:file': 0.07; 'sys': 0.07; 'string': 0.09; 'line:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'repeated': 0.09; 'subject:into': 0.09; 'subject:module': 0.09; 'python': 0.11; 'arg': 0.16; 'caching': 0.16; 'imports': 0.16; "module's": 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:import': 0.16; 'sys.modules': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'work,': 0.20; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '31,': 0.24; 'config': 0.24; 'subject: .': 0.24; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; 'function': 0.29; "doesn't": 0.30; 'work.': 0.31; 'directory,': 0.31; 'file': 0.32; '(most': 0.33; 'but': 0.35; 'false': 0.36; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'first': 0.61; 'complete': 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; 'imp': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: import file without .py into another module Date: Tue, 21 Jan 2014 16:40:09 +0100 Organization: None References: <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> <022c00a5-87ee-4b2a-a6f6-e23c8bda1e12@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084917c.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390318785 news.xs4all.nl 2847 [2001:888:2000:d::a6]:44049 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64424 kevinbercaw@gmail.com wrote: >> > How do I get the value of the config file variable "myVar"?? It seems >> > it's interpreting the variable name as a string rather than a variable >> > name. I don't see any python function stringToVariable. >> The line: >> >> configModuleObject = imp.load_source(fileName, filePath) >> imports the module and then binds it to the name configModuleObject, >> >> therefore: >> >> print configModuleObject.myVar > > Yep, I tried that right off as that's how I thought it would work, but it > doesn't work. Traceback (most recent call last): > File "mainScript.py", line 31, in > print configModuleObject.myVar > AttributeError: 'module' object has no attribute 'myVar' Try again with module = imp.load_source("made_up_name", "a15800") print module.myVar If the file "a15800" is not in the current working directory, give the complete path, e. g: module = imp.load_source("made_up_name", "/path/to/a15800") print module.myVar The first arg serves as the module's name which is used for caching to speed up repeated imports: >>> import imp >>> import sys >>> "made_up_name" in sys.modules False >>> module = imp.load_source("made_up_name", "a15800") >>> module.myVar 'hello' >>> "made_up_name" in sys.modules True >>> module