Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.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; 'example:': 0.03; 'subject:file': 0.07; 'sys': 0.07; 'variables': 0.07; 'string': 0.09; '%s"': 0.09; '__name__': 0.09; 'filename': 0.09; 'line:': 0.09; 'subject:into': 0.09; 'subject:module': 0.09; 'python': 0.11; 'def': 0.12; "'__main__':": 0.16; '.py': 0.16; 'accepts': 0.16; 'arguments:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'imports': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nameerror:': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'subject:import': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'module': 0.19; 'seems': 0.21; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'config': 0.24; 'script.': 0.24; 'subject: .': 0.24; 'script': 0.25; '15,': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'sets': 0.30; '"",': 0.31; 'question:': 0.31; 'file': 0.32; '(most': 0.33; 'received:84': 0.35; 'but': 0.35; 'skip:> 10': 0.36; 'two': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'recent': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'full': 0.61; 'email addr:gmail.com': 0.63; 'name': 0.63; 'header:Reply-To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'imp': 0.84; 'reply-to:addr:python.org': 0.84; '***': 0.95 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=JLW1sq6b c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=5FYZ9MsUIQAA:10 a=_RGnEak3AZsA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=QGdadq7IuTYA:10 a=pGLkceISAAAA:8 a=3hQZ2SEpeK7uriL5afcA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 21 Jan 2014 15:06:16 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: import file without .py into another module References: <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> In-Reply-To: <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1390316784 news.xs4all.nl 2955 [2001:888:2000:d::a6]:48467 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:64416 On 2014-01-21 14:44, kevinbercaw@gmail.com wrote: > I have a python script that accepts two arguments: > sys.argv[1] is the full directory path to a config script. The script is python but does not have a .py extension! > sys.argv[2] is the file name of the config script > > For example: > mainScript.py ./ a15800 > > > The config script sets variables that I want to be able to use in the main script. > > *** contents of "a15800": *** > myVar = "hello" > > *** contents of "mainScript.py": *** > def printVars(configModuleName): > myVarName = ("%s.myVar" % configModuleName) > print "myVarName = %s" % myVarName > myVarValue = eval(myVarName) > print "myVarValue = %s" % myVarValue > > > if __name__ == '__main__': > import sys > import imp > filePath = sys.argv[1] > fileName = sys.argv[2] > configModuleObject = imp.load_source(fileName, filePath) > configModuleName = configModuleObject.__name__ > print "configModuleName = %s" % configModuleName > printVars(configModuleName) > > *** Output: *** >>mainScript.py ./ a15800 > configModuleName = a15800 > myVarName = a15800.myVar > Traceback (most recent call last): > File "mainScript.py", line 27, in > printVars(configModuleName) > File "mainScript.py", line 15, in printVars > myVarValue = eval(myVarName) > File "", line 1, in > NameError: name 'a15800' is not defined > > *** Question: *** > 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