Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > "import" not working?

Reply
Thread Tools

"import" not working?

 
 
Lionel
Guest
Posts: n/a
 
      02-20-2009
Hello all:

I've crafted several classes and exceptions which I've stored in a
file called "DataFileType.py". I then invoke them from within other
files like this:


# Top of file

import sys
sys.path.append("c:\DataFileTypes")
from DataFileTypes import *

data = None

try:
# Note: "INTData" is a class defined in "DataFileTypes"
data = INTData("C:\SomeRawDataFile.int")

except DataFileError:
print("Error opening data file")

except ResourceFileError:
print("Error opening resource file")


The above works very well. No complaints. However, I'm experimenting
with the wxPython gui library and found that this no longer seems to
work when I add the crucial bits to one of their examples. I've copied
and run an example that came with wxPython and verified that, with no
modification on my part, it runs with no problem. I then add ONLY my
import instructions and try to instantiate an object as follows (I'm
only showing a portion of the file):


#!/usr/bin/env python

from numpy import arange, sin, pi

import matplotlib

matplotlib.use('WX')
from matplotlib.backends.backend_wx import FigureCanvasWx as
FigureCanvas

from matplotlib.figure import Figure

from wx import *

# The following 4 lines are my additions to the example code:
import sys
sys.path.append("c:\DataFileTypes")
from DataFileTypes import *
data = INTData("C:\SomeRawDataFile.int")


class CanvasFrame(Frame):
..
..
etc
..
..



Running the above program (which I've called "guiplottest.py")
generates an immediate error with the following traceback:

C:\Guiplottest.py
Traceback <most recent call last>:
File "C:\GuiPlotTest.py", line 19, in <module>
data = INTData("C:\SomeRawDataFile.int")
NameError: name 'INTData' is not defined


But "INTData" is defined...it's defined in "DataFileTypes" from which
I've imported everything. What's happening here? Thanks in advance!

-L
 
Reply With Quote
 
 
 
 
Emile van Sebille
Guest
Posts: n/a
 
      02-20-2009
Lionel wrote:
> from DataFileTypes import *


That's an import where you don't know what's getting import'd -- ie,
namespace pollution


[snip]

> from wx import *
>


and more here. Try being explicit with your naming.

HTH,

Emile



 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-20-2009
On Fri, Feb 20, 2009 at 3:33 PM, Lionel <(E-Mail Removed)> wrote:
> Hello all:
>
> I've crafted several classes and exceptions which I've stored in a
> file called "DataFileType.py". I then invoke them from within other
> files like this:
>
>
> # Top of file
>
> import sys
> sys.path.append("c:\DataFileTypes")


Recall that the backslash is the escape character in Python and that
therefore you need to put \\ to get a backslash in the resulting path
string. Thus, the path you think you're adding isn't the path that's
getting added.
Alternatively, you can just use forward slashes instead (yes, that
works on Windows from Python).

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com
 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-20-2009
On Feb 20, 3:52*pm, Chris Rebert <(E-Mail Removed)> wrote:
> On Fri, Feb 20, 2009 at 3:33 PM, Lionel <(E-Mail Removed)> wrote:
> > Hello all:

>
> > I've crafted several classes and exceptions which I've stored in a
> > file called "DataFileType.py". I then invoke them from within other
> > files like this:

>
> > # Top of file

>
> > import sys
> > sys.path.append("c:\DataFileTypes")

>
> Recall that the backslash is the escape character in Python and that
> therefore you need to put \\ to get a backslash in the resulting path
> string. Thus, the path you think you're adding isn't the path that's
> getting added.
> Alternatively, you can just use forward slashes instead (yes, that
> works on Windows from Python).
>
> Cheers,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com


But I'm only using a single backslash in the first example I gave, and
it works just fine there. How can this be?
 
Reply With Quote
 
Matimus
Guest
Posts: n/a
 
      02-21-2009
On Feb 20, 3:56*pm, Lionel <(E-Mail Removed)> wrote:
> On Feb 20, 3:52*pm, Chris Rebert <(E-Mail Removed)> wrote:
>
>
>
> > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <(E-Mail Removed)> wrote:
> > > Hello all:

>
> > > I've crafted several classes and exceptions which I've stored in a
> > > file called "DataFileType.py". I then invoke them from within other
> > > files like this:

>
> > > # Top of file

>
> > > import sys
> > > sys.path.append("c:\DataFileTypes")

>
> > Recall that the backslash is the escape character in Python and that
> > therefore you need to put \\ to get a backslash in the resulting path
> > string. Thus, the path you think you're adding isn't the path that's
> > getting added.
> > Alternatively, you can just use forward slashes instead (yes, that
> > works on Windows from Python).

>
> > Cheers,
> > Chris

>
> > --
> > Follow the path of the Iguana...http://rebertia.com

>
> But I'm only using a single backslash in the first example I gave, and
> it works just fine there. How can this be?


You must be running the python script from a directory where the file
you are trying to import is already in the path. It never tries to
look in the (bad) path because it found a file with the same name
locally. My guess is that you are running the wx example from another
location, and that is when you run into problems.

Matt
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-21-2009
Chris Rebert wrote:
> On Fri, Feb 20, 2009 at 3:33 PM, Lionel <(E-Mail Removed)> wrote:
>> Hello all:
>>
>> I've crafted several classes and exceptions which I've stored in a
>> file called "DataFileType.py". I then invoke them from within other
>> files like this:
>>
>>
>> # Top of file
>>
>> import sys
>> sys.path.append("c:\DataFileTypes")

>
> Recall that the backslash is the escape character in Python and that
> therefore you need to put \\ to get a backslash in the resulting path
> string. Thus, the path you think you're adding isn't the path that's
> getting added.
> Alternatively, you can just use forward slashes instead (yes, that
> works on Windows from Python).
>

In fact "\D" isn't a defined escape character, so this particular usage
does give the right path, though your general point is good.

>>> "\D"

'\\D'
>>> len("\D")

2

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-21-2009
On Feb 20, 4:15*pm, Matimus <(E-Mail Removed)> wrote:
> On Feb 20, 3:56*pm, Lionel <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Feb 20, 3:52*pm, Chris Rebert <(E-Mail Removed)> wrote:

>
> > > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <(E-Mail Removed)> wrote:
> > > > Hello all:

>
> > > > I've crafted several classes and exceptions which I've stored in a
> > > > file called "DataFileType.py". I then invoke them from within other
> > > > files like this:

>
> > > > # Top of file

>
> > > > import sys
> > > > sys.path.append("c:\DataFileTypes")

>
> > > Recall that the backslash is the escape character in Python and that
> > > therefore you need to put \\ to get a backslash in the resulting path
> > > string. Thus, the path you think you're adding isn't the path that's
> > > getting added.
> > > Alternatively, you can just use forward slashes instead (yes, that
> > > works on Windows from Python).

>
> > > Cheers,
> > > Chris

>
> > > --
> > > Follow the path of the Iguana...http://rebertia.com

>
> > But I'm only using a single backslash in the first example I gave, and
> > it works just fine there. How can this be?

>
> You must be running the python script from a directory where the file
> you are trying to import is already in the path. It never tries to
> look in the (bad) path because it found a file with the same name
> locally. My guess is that you are running the wx example from another
> location, and that is when you run into problems.
>
> Matt- Hide quoted text -
>
> - Show quoted text -


Okay, moving the wx example into the same directory containing the
first example that was working fixed it. This directory only contains
these two modules and nothing else. The old directory which contained
the example that wasn't working did not contain a module with the same
name as the one I was trying to import, so i don't know why this "fix"
worked.
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      02-21-2009
En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <(E-Mail Removed)>
escribió:

> Okay, moving the wx example into the same directory containing the
> first example that was working fixed it. This directory only contains
> these two modules and nothing else. The old directory which contained
> the example that wasn't working did not contain a module with the same
> name as the one I was trying to import, so i don't know why this "fix"
> worked.


Just play safe:

- Don't use "from xxx import *", least from two places at the same time.
The wx package has a short name on purpose - use "import wx" and then
wx.Frame, etc.

- Don't play with sys.path if you don't have to; you can put your own
modules in a place already listed (like Lib\site-packages). Or, use a .pth
file if you want to add a copmletely separate directory like
c:\DataFileTypes

--
Gabriel Genellina

 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-23-2009
On Feb 21, 12:37*am, "Gabriel Genellina" <(E-Mail Removed)>
wrote:
> En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <(E-Mail Removed)> *
> escribió:
>
> > Okay, moving the wx example into the same directory containing the
> > first example that was working fixed it. This directory only contains
> > these two modules and nothing else. The old directory which contained
> > the example that wasn't working did not contain a module with the same
> > name as the one I was trying to import, so i don't know why this "fix"
> > worked.

>
> Just play safe:
>
> - Don't use "from xxx import *", least from two places at the same time. *
> The wx package has a short name on purpose - use "import wx" and then *
> wx.Frame, etc.
>
> - Don't play with sys.path if you don't have to; you can put your own *
> modules in a place already listed (like Lib\site-packages). Or, use a .pth *
> file if you want to add a copmletely separate directory like *
> c:\DataFileTypes
>
> --
> Gabriel Genellina


Okay, I've moved the module directory. Originally I had a Python
module named "DataFileTypes.py" in the "C:\DataFileTypes" folder. (the
complete path is therefore "C:\DataFileTypes\DataFileTypes.py"). To
access object types in this module from other Pyhton modules I
specified the following:


import sys
sys.path.append("C:\DataFileTypes")
from DataFileTypes import *


The above 3 lines of code are contained at the begining of a module
whose complete path is "C:\Python path test\Test.py". This folder
only contains this module and two other unrelated python files I've
written. At the moment this works fine i.e. Python is able to find the
DataFileTypes.py module at its location using the "sys.path.append /
import" statements above. However, now I've moved the "DataFileTypes"
folder containing the module so that the new complete path is:

"C:\Python25\Lib\site-packages\DataFileTypes\DataFileTypes.py"

At this point I print the sys.path from the Python shell:

IDLE 1.2.4
>>> import sys
>>> print sys.path

['C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip',
'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-
win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\
\site-packages', 'C:\\Python25\\lib\\site-packages\\wx-2.8-msw-
unicode']
>>>


"...\site-packages" is indeed listed on the path. I now remove:

import sys
sys.path.append("c:\DataFileTypes")
from DataFileTypes import *

from the invoking module and replace it with:

from DataFileTypes import *

When I attempt to run the Test.py program after these changes I get
error messages claiming:

"except DataFileError:
NameError: name 'DataFileError' is not defined"

I get an error again claiming that my custom exception "DataFileError"
declared in "DataFileTypes" is not defined. Why does this work fine
when I have my module in the root directory and append the sys.path,
but stops working when I put the module in another directory already
resident in the sys.path?



 
Reply With Quote
 
Lionel
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 11:24*am, Lionel <(E-Mail Removed)> wrote:
> On Feb 21, 12:37*am, "Gabriel Genellina" <(E-Mail Removed)>
> wrote:
>
>
>
>
>
> > En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <(E-Mail Removed)> *
> > escribió:

>
> > > Okay, moving the wx example into the same directory containing the
> > > first example that was working fixed it. This directory only contains
> > > these two modules and nothing else. The old directory which contained
> > > the example that wasn't working did not contain a module with the same
> > > name as the one I was trying to import, so i don't know why this "fix"
> > > worked.

>
> > Just play safe:

>
> > - Don't use "from xxx import *", least from two places at the same time.. *
> > The wx package has a short name on purpose - use "import wx" and then *
> > wx.Frame, etc.

>
> > - Don't play with sys.path if you don't have to; you can put your own *
> > modules in a place already listed (like Lib\site-packages). Or, use a .pth *
> > file if you want to add a copmletely separate directory like *
> > c:\DataFileTypes

>
> > --
> > Gabriel Genellina

>
> Okay, I've moved the module directory. Originally I had a Python
> module named "DataFileTypes.py" in the "C:\DataFileTypes" folder. (the
> complete path is therefore "C:\DataFileTypes\DataFileTypes.py"). To
> access object types in this module from other Pyhton modules I
> specified the following:
>
> import sys
> sys.path.append("C:\DataFileTypes")
> from DataFileTypes import *
>
> The above 3 lines of code are contained at the begining of a module
> whose complete path is "C:\Python path test\Test.py". *This folder
> only contains this module and two other unrelated python files I've
> written. At the moment this works fine i.e. Python is able to find the
> DataFileTypes.py module at its location using the "sys.path.append /
> import" statements above. However, now I've moved the "DataFileTypes"
> folder containing the module so that the new complete path is:
>
> "C:\Python25\Lib\site-packages\DataFileTypes\DataFileTypes.py"
>
> At this point I print the sys.path from the Python shell:
>
> IDLE 1.2.4>>> import sys
> >>> print sys.path

>
> ['C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip',
> 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-
> win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\
> \site-packages', 'C:\\Python25\\lib\\site-packages\\wx-2.8-msw-
> unicode']
>
>
>
> "...\site-packages" is indeed listed on the path. I now remove:
>
> import sys
> sys.path.append("c:\DataFileTypes")
> from DataFileTypes import *
>
> from the invoking module and replace it with:
>
> from DataFileTypes import *
>
> When I attempt to run the Test.py program after these changes I get
> error messages claiming:
>
> "except DataFileError:
> NameError: name 'DataFileError' is not defined"
>
> I get an error again claiming that my custom exception "DataFileError"
> declared in "DataFileTypes" is not defined. Why does this work fine
> when I have my module in the root directory and append the sys.path,
> but stops working when I put the module in another directory already
> resident in the sys.path?- Hide quoted text -
>
> - Show quoted text -


Taking "DataFileTypes.py" module out of the "...\site-packages
\DataFileTypes" folder and placing it directly into the "site-
packages" folder seems to have cleared it up. Some problem between
package and module usage I suppose.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To be not, or not to be not? Ruby Freak Ruby 2 09-23-2008 08:04 AM
Why not 'foo = not f' instead of 'foo = (not f or 1) and 0'? Kristian Domke Python 11 01-23-2008 07:27 PM
'' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long. rote ASP .Net 2 01-23-2008 03:07 PM
Cisco 3640 3620 3600 not detecting, not enabling, not working: NM-2FE2W Taki Soho Cisco 0 09-22-2004 07:28 AM
maintaining control with cookies (not strictly an ASP or even server side question. But not not either) Stephanie Stowe ASP General 2 04-07-2004 04:23 PM



Advertisments