Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64422

Re: import file without .py into another module

Date 2014-01-21 09:36 -0600
From Tim Chase <python.list@tim.thechases.com>
Subject Re: import file without .py into another module
References <6c68b312-4979-454f-b483-b4a39c64196b@googlegroups.com> <mailman.5786.1390316784.18130.python-list@python.org> <022c00a5-87ee-4b2a-a6f6-e23c8bda1e12@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.5787.1390318534.18130.python-list@python.org> (permalink)

Show all headers | View raw


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



Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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