doted filenames in import statements

  • Thread starter Jean-Michel Pichavant
  • Start date
J

Jean-Michel Pichavant

Hi fellows,

I'd like to use the dynamic __import__ statement. It works pretty well
with non dotted names, but I cannot figure how to make it work with
dotted file paths.

example:

file = "/home/dsp/test.py"
test = __import__(file)

works like a charm

file = "/home/dsp/4.6.0.0/test.py"
test = __import__(file)
=> no module name blalalal found.

Any suggestion ? I tried multiple escape technics without any success.

JM
 
P

Piet van Oostrum

Jean-Michel Pichavant said:
JP> Hi fellows,
JP> I'd like to use the dynamic __import__ statement. It works pretty well with
JP> non dotted names, but I cannot figure how to make it work with dotted file
JP> paths.

What is a dotted file path? Is that 4.6.0.0?
JP> example:
JP> file = "/home/dsp/test.py"
JP> test = __import__(file)
JP> works like a charm

That's not supposed to work. In older pythons it did work but that's a
bug. In newer pythons it doesn't. __import__ works on module names, not
spam = __import__('/Users/piet/TEMP/test.py', globals(), locals(), [], -1)
Traceback (most recent call last):
JP> file = "/home/dsp/4.6.0.0/test.py"
JP> test = __import__(file)
JP> => no module name blalalal found.
JP> Any suggestion ? I tried multiple escape technics without any success.

Rightly so.

I think the best would be to add the directory to sys.path
sys.path.add('/home/dsp/4.6.0.0')
and then
__import__('test', ... )
 
J

Jean-Michel Pichavant

Piet said:
[snip]
JP> file = "/home/dsp/4.6.0.0/test.py"
JP> test = __import__(file)
JP> => no module name blalalal found.


JP> Any suggestion ? I tried multiple escape technics without any success.

Rightly so.

I think the best would be to add the directory to sys.path
sys.path.add('/home/dsp/4.6.0.0')
and then
__import__('test', ... )

I see. My problem is that a have to import 2 different files having de
same name. In the same name space I have 2 objects from 2 different
software branches, let's say 4.6.0 and 4.6.1.
The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py.

If I add 4.6.1 to sys.path, the import statement will look like:
self._orb = __import__('orb')
The problem is, python wil assume orb is already imported and will
assign the module from the 4.6.0 branch to my 4.6.1 object.

Do I have to mess up with sys.modules keys to make python import the
correct file ? Is there a standard/proper way to do that ?

JM
 
T

Terry Reedy

Jean-Michel Pichavant said:
Piet said:
[snip]
JP> file = "/home/dsp/4.6.0.0/test.py"
JP> test = __import__(file)
JP> => no module name blalalal found.


JP> Any suggestion ? I tried multiple escape technics without any
success.

Rightly so.

I think the best would be to add the directory to sys.path
sys.path.add('/home/dsp/4.6.0.0')
and then
__import__('test', ... )

I see. My problem is that a have to import 2 different files having de
same name. In the same name space I have 2 objects from 2 different
software branches, let's say 4.6.0 and 4.6.1.
The first object shall import 4.6.0/orb.py and the second one 4.6.1/orb.py.

If I add 4.6.1 to sys.path, the import statement will look like:
self._orb = __import__('orb')
The problem is, python wil assume orb is already imported and will
assign the module from the 4.6.0 branch to my 4.6.1 object.

Do I have to mess up with sys.modules keys to make python import the
correct file ? Is there a standard/proper way to do that ?

If you make the directory names into proper identifiers like v460 and
v461 and add __init__.py to each to make them packages and have both on
search path, then

import v460.orb #or import v460.orb as orb460
import v461.orb #or import v460.orb as orb461

will get you both. One way or another, they have to get different names
within Python.

Terry Jan Reedy
 
J

Jean-Michel Pichavant

Terry said:
Jean-Michel Pichavant said:
Piet said:
[snip]

JP> file = "/home/dsp/4.6.0.0/test.py"
JP> test = __import__(file)
JP> => no module name blalalal found.


JP> Any suggestion ? I tried multiple escape technics without any
success.

Rightly so.

I think the best would be to add the directory to sys.path
sys.path.add('/home/dsp/4.6.0.0')
and then
__import__('test', ... )

I see. My problem is that a have to import 2 different files having
de same name. In the same name space I have 2 objects from 2
different software branches, let's say 4.6.0 and 4.6.1.
The first object shall import 4.6.0/orb.py and the second one
4.6.1/orb.py.

If I add 4.6.1 to sys.path, the import statement will look like:
self._orb = __import__('orb')
The problem is, python wil assume orb is already imported and will
assign the module from the 4.6.0 branch to my 4.6.1 object.

Do I have to mess up with sys.modules keys to make python import the
correct file ? Is there a standard/proper way to do that ?

If you make the directory names into proper identifiers like v460 and
v461 and add __init__.py to each to make them packages and have both
on search path, then

import v460.orb #or import v460.orb as orb460
import v461.orb #or import v460.orb as orb461

will get you both. One way or another, they have to get different
names within Python.

Terry Jan Reedy

I finally had to write my own import statement as I prefered to
manipulate the python objects instead of manipulating third party files.
Basically when importing 'file.py' it records it in sys.modules as
sys.modules['__magic_word_file''] and then I remove the standard
reference. This allows me to import file.py again, but with a totally
different path. (path is temporarily added to sys.path)

JM
 

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

No members online now.

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,654
Latest member
LannySinge

Latest Threads

Top