Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64416
| Date | 2014-01-21 15:06 +0000 |
|---|---|
| From | MRAB <python@mrabarnett.plus.com> |
| Subject | Re: import file without .py into another module |
| References | <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.5786.1390316784.18130.python-list@python.org> (permalink) |
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 <module>
> printVars(configModuleName)
> File "mainScript.py", line 15, in printVars
> myVarValue = eval(myVarName)
> File "<string>", line 1, in <module>
> 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
import file without .py into another module kevinbercaw@gmail.com - 2014-01-21 06:44 -0800
Re: import file without .py into another module MRAB <python@mrabarnett.plus.com> - 2014-01-21 15:06 +0000
Re: import file without .py into another module kevinbercaw@gmail.com - 2014-01-21 07:13 -0800
Re: import file without .py into another module Tim Chase <python.list@tim.thechases.com> - 2014-01-21 09:36 -0600
Re: import file without .py into another module Peter Otten <__peter__@web.de> - 2014-01-21 16:40 +0100
Re: import file without .py into another module kevinbercaw@gmail.com - 2014-01-21 07:50 -0800
Re: import file without .py into another module Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-21 18:10 +0000
Re: import file without .py into another module kevinbercaw@gmail.com - 2014-01-21 07:27 -0800
csiph-web