Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

import file without .py into another module

44 views
Skip to first unread message

kevin...@gmail.com

unread,
Jan 21, 2014, 9:44:13 AM1/21/14
to
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.

MRAB

unread,
Jan 21, 2014, 10:06:16 AM1/21/14
to pytho...@python.org
The line:

configModuleObject = imp.load_source(fileName, filePath)

imports the module and then binds it to the name configModuleObject,
therefore:

print configModuleObject.myVar

kevin...@gmail.com

unread,
Jan 21, 2014, 10:13:59 AM1/21/14
to
On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote:
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 <module>
print configModuleObject.myVar
AttributeError: 'module' object has no attribute 'myVar'

kevin...@gmail.com

unread,
Jan 21, 2014, 10:27:50 AM1/21/14
to
FYI - more info from interactive session, query configModuleObject and configModuleName using imp.find_module:
>>> imp.find_module("configModuleObject")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named configModuleObject
>>> imp.find_module("a15800")
(<open file 'a15800.pyc', mode 'rb' at 0x2b503b72d288>, 'a15800.pyc', ('.pyc', 'rb', 2))
>>> imp.find_module(configModuleName)
(<open file 'a15800.pyc', mode 'rb' at 0x2b503b72d300>, 'a15800.pyc', ('.pyc', 'rb', 2))

Tim Chase

unread,
Jan 21, 2014, 10:36:20 AM1/21/14
to pytho...@python.org
Check what you're passing for fileName/FilePath:

>>> import imp
>>> with open('demo.txt', 'wb') as f:
... f.write('x = 42\ny = "hello"\n')
...
>>> d = imp.load_source('SomeName', 'demo.txt')
>>> dir(d)
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'x', 'y']
>>> d.x
42
>>> d.y
'hello'
>>> d.__name__
'SomeName'

-tkc




Peter Otten

unread,
Jan 21, 2014, 10:40:09 AM1/21/14
to pytho...@python.org
kevin...@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 <module>
> 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
<module 'made_up_name' from 'a15800c'>


kevin...@gmail.com

unread,
Jan 21, 2014, 10:50:40 AM1/21/14
to
Thanks Peter Otten, that worked. I was not able to understand the documentation for imp.load_source correctly. Thanks so much!

Mark Lawrence

unread,
Jan 21, 2014, 1:10:22 PM1/21/14
to pytho...@python.org
On 21/01/2014 15:50, kevin...@gmail.com wrote:

[snipped the double line spaced stuff courtesy of google]

Would you please read and action this
https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing the
double line spacing that google inserts, thanks.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

0 new messages