Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
doted filenames in import statements
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Jean-Michel Pichavant  
View profile  
 More options Jul 21 2009, 5:42 pm
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Tue, 21 Jul 2009 23:42:41 +0200
Local: Tues, Jul 21 2009 5:42 pm
Subject: doted filenames in import statements
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Jul 21 2009, 5:59 pm
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Tue, 21 Jul 2009 14:59:17 -0700
Local: Tues, Jul 21 2009 5:59 pm
Subject: Re: doted filenames in import statements
On Tue, Jul 21, 2009 at 2:42 PM, Jean-Michel

You want the imp.load_module() function:
http://docs.python.org/library/imp.html#imp.load_module

__import__() only operates on module/package names. I'm not sure how
you even got it to work with a filename...

Cheers,
Chris
--
http://blog.rebertia.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile  
 More options Jul 21 2009, 6:29 pm
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@cs.uu.nl>
Date: Wed, 22 Jul 2009 00:29:52 +0200
Local: Tues, Jul 21 2009 6:29 pm
Subject: Re: doted filenames in import statements

>>>>> Jean-Michel Pichavant <jeanmic...@sequans.com> (JP) wrote:
>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
on file names.

Python 2.6:

>>> spam = __import__('/Users/piet/TEMP/test.py', globals(), locals(), [], -1)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: Import by filename is not supported.

>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', ... )
--
Piet van Oostrum <p...@cs.uu.nl>
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: p...@vanoostrum.org


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Christian Heimes  
View profile  
 More options Jul 21 2009, 6:48 pm
Newsgroups: comp.lang.python
From: Christian Heimes <li...@cheimes.de>
Date: Wed, 22 Jul 2009 00:48:40 +0200
Local: Tues, Jul 21 2009 6:48 pm
Subject: Re: doted filenames in import statements

Chris Rebert wrote:
> You want the imp.load_module() function:
> http://docs.python.org/library/imp.html#imp.load_module

> __import__() only operates on module/package names. I'm not sure how
> you even got it to work with a filename...

It used to work with filenames but it was a bug. I guess several people
are swearing curses on me for removing this 'feature'. *g*

Christian


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Michel Pichavant  
View profile  
 More options Jul 22 2009, 8:16 am
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Wed, 22 Jul 2009 14:16:19 +0200
Local: Wed, Jul 22 2009 8:16 am
Subject: Re: doted filenames in import statements

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options Jul 22 2009, 12:44 pm
Newsgroups: comp.lang.python
From: Terry Reedy <tjre...@udel.edu>
Date: Wed, 22 Jul 2009 12:44:08 -0400
Local: Wed, Jul 22 2009 12:44 pm
Subject: Re: doted filenames in import statements

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jean-Michel Pichavant  
View profile  
 More options Jul 24 2009, 8:34 am
Newsgroups: comp.lang.python
From: Jean-Michel Pichavant <jeanmic...@sequans.com>
Date: Fri, 24 Jul 2009 14:34:23 +0200
Local: Fri, Jul 24 2009 8:34 am
Subject: Re: doted filenames in import statements

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »