"import" not working?

L

Lionel

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
 
E

Emile van Sebille

Lionel said:
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
 
C

Chris Rebert

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
 
L

Lionel

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

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

Matimus

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
 
S

Steve Holden

Chris said:
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.
2

regards
Steve
 
L

Lionel

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.
 
G

Gabriel Genellina

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
 
L

Lionel

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

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['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?
 
L

Lionel

En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <[email protected]>  
escribió:
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

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
['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.
 
G

Gabriel Genellina

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.

You're right. To import module "foo", the directory containing the
"foo.py" file must be listed in sys.path.
To import package "foo", the directory containing the "foo" directory must
be listed in sys.path; the foo directory itself must contain an
"__init__.py" file.
 
R

Rhodri James

sys.path.append("C:\DataFileTypes")

Just so that we're clear, this is a *really* *bad* habit to get
into. Not appending to sys.path, though that isn't often a good
idea, but failing to escape your backslashes. This works because
'\D' happens not to be a valid escape sequence: if your directory
had instead been called "newtypes" then "C:\newtypes" would not
have had the result you were expecting at all. If you really
mean a backslash to be in any literal string, you should always
double it:

sys.path.append("C:\\DataFileTypes")

IMHO, Python is somewhat inconsistent in not producing a
compile-type error (or at least an annoying compile-time
warning) when presented with invalid escape sequences.
What it does, even though it's well-documented and usually
the right guess, is to encourage bad habits.
 
A

Aahz

Just so that we're clear, this is a *really* *bad* habit to get into.
Not appending to sys.path, though that isn't often a good idea, but
failing to escape your backslashes. This works because '\D' happens
not to be a valid escape sequence: if your directory had instead been
called "newtypes" then "C:\newtypes" would not have had the result you
were expecting at all. If you really mean a backslash to be in any
literal string, you should always double it:

sys.path.append("C:\\DataFileTypes")

My preference:

sys.path.append(r"C:\DataFileTypes")

This doesn't work if you need to add a trailing backslash, though.
 
L

Lie Ryan

Scott said:
Also my preference (except, due to aging eyes and bad fonts, I prefer
single quotes unless doubles are needed). I solve the trailing
backslash problem as so:
sys.path.append(r'C:\DataFileTypes' '\\')

Why not: r'C:\DataFileTypes\ '
 
L

Lie Ryan

Steve said:
Because that's the file named " " that lives in C:\DataFileTypes?

Is whitespace name possible? I tried:

os.mkdir(r'C:\somedir\ ')

and it gives the error:
WindowsError: [Error 183] Cannot create a file when that file already
exists: 'C:\\somedir\\ '

Every single way I can think of to try to make a name with purely space
failed. Why not make os modules special case single whitespace folder?
(give better reason than to quote the Zen)

anyway, you could always use r'C:\DataFileTypes\ '[:-1]
 
G

Gabriel Genellina

Steve said:
Because that's the file named " " that lives in C:\DataFileTypes?

Is whitespace name possible? I tried:

os.mkdir(r'C:\somedir\ ')

and it gives the error:
WindowsError: [Error 183] Cannot create a file when that file already
exists: 'C:\\somedir\\ '

Every single way I can think of to try to make a name with purely space
failed. Why not make os modules special case single whitespace folder?
(give better reason than to quote the Zen)

py> path = r'C:\DataFileTypes\ '
py> os.path.join(path, "hello.txt")
'C:\\DataFileTypes\\ \\hello.txt'
anyway, you could always use r'C:\DataFileTypes\ '[:-1]

Yes, and it's one of the alternatives listed in the FAQ entry:
http://www.python.org/doc/faq/general/#id54
 

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

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top