Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64415 > unrolled thread
| Started by | kevinbercaw@gmail.com |
|---|---|
| First post | 2014-01-21 06:44 -0800 |
| Last post | 2014-01-21 07:27 -0800 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | kevinbercaw@gmail.com |
|---|---|
| Date | 2014-01-21 06:44 -0800 |
| Subject | import file without .py into another module |
| Message-ID | <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2014-01-21 15:06 +0000 |
| Message-ID | <mailman.5786.1390316784.18130.python-list@python.org> |
| In reply to | #64415 |
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
[toc] | [prev] | [next] | [standalone]
| From | kevinbercaw@gmail.com |
|---|---|
| Date | 2014-01-21 07:13 -0800 |
| Message-ID | <022c00a5-87ee-4b2a-a6f6-e23c8bda1e12@googlegroups.com> |
| In reply to | #64416 |
On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote:
> On 2014-01-21 14:44, 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
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'
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2014-01-21 09:36 -0600 |
| Message-ID | <mailman.5787.1390318534.18130.python-list@python.org> |
| In reply to | #64417 |
On 2014-01-21 07:13, kevinbercaw@gmail.com wrote:
>On Tuesday, January 21, 2014 10:06:16 AM UTC-5, MRAB wrote:
>> 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'
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-01-21 16:40 +0100 |
| Message-ID | <mailman.5788.1390318785.18130.python-list@python.org> |
| In reply to | #64417 |
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 <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'>
[toc] | [prev] | [next] | [standalone]
| From | kevinbercaw@gmail.com |
|---|---|
| Date | 2014-01-21 07:50 -0800 |
| Message-ID | <f82562d2-e4f4-4baf-946f-f0e392f5e7ad@googlegroups.com> |
| In reply to | #64424 |
On Tuesday, January 21, 2014 10:40:09 AM UTC-5, Peter Otten wrote:
> 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'>
Thanks Peter Otten, that worked. I was not able to understand the documentation for imp.load_source correctly. Thanks so much!
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-01-21 18:10 +0000 |
| Message-ID | <mailman.5806.1390327847.18130.python-list@python.org> |
| In reply to | #64426 |
On 21/01/2014 15:50, kevinbercaw@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
[toc] | [prev] | [next] | [standalone]
| From | kevinbercaw@gmail.com |
|---|---|
| Date | 2014-01-21 07:27 -0800 |
| Message-ID | <4ba01a96-493f-4e1c-9cb7-0d46cd7ef103@googlegroups.com> |
| In reply to | #64415 |
On Tuesday, January 21, 2014 9:44:13 AM UTC-5, kevin...@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.
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))
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web