import file without .py into another module

K

kevinbercaw

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.
 
M

MRAB

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
 
K

kevinbercaw

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


*** Output: ***
configModuleName = a15800
myVarName = a15800.myVar
Traceback (most recent call last):
File "mainScript.py", line 27, in <module>

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

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'
 
K

kevinbercaw

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.

FYI - more info from interactive session, query configModuleObject and configModuleName using imp.find_module:Traceback (most recent call last):
(<open file 'a15800.pyc', mode 'rb' at 0x2b503b72d300>, 'a15800.pyc', ('.pyc', 'rb', 2))
 
T

Tim Chase

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'

Check what you're passing for fileName/FilePath:
... f.write('x = 42\ny = "hello"\n')
...['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'x', 'y']'SomeName'

-tkc
 
P

Peter Otten

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:
<module 'made_up_name' from 'a15800c'>
 
K

kevinbercaw

(e-mail address removed) wrote:










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:




<module 'made_up_name' from 'a15800c'>

Thanks Peter Otten, that worked. I was not able to understand the documentation for imp.load_source correctly. Thanks so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top