Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   "import" not working? (http://www.velocityreviews.com/forums/t671681-import-not-working.html)

Lionel 02-20-2009 11:33 PM

"import" not working?
 
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

Emile van Sebille 02-20-2009 11:48 PM

Re: "import" not working?
 
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




Chris Rebert 02-20-2009 11:52 PM

Re: "import" not working?
 
On Fri, Feb 20, 2009 at 3:33 PM, Lionel <lionel.keene@gmail.com> 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

Lionel 02-20-2009 11:56 PM

Re: "import" not working?
 
On Feb 20, 3:52*pm, Chris Rebert <c...@rebertia.com> wrote:
> On Fri, Feb 20, 2009 at 3:33 PM, Lionel <lionel.ke...@gmail.com> 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?

Matimus 02-21-2009 12:15 AM

Re: "import" not working?
 
On Feb 20, 3:56*pm, Lionel <lionel.ke...@gmail.com> wrote:
> On Feb 20, 3:52*pm, Chris Rebert <c...@rebertia.com> wrote:
>
>
>
> > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <lionel.ke...@gmail.com> 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

Steve Holden 02-21-2009 12:32 AM

Re: "import" not working?
 
Chris Rebert wrote:
> On Fri, Feb 20, 2009 at 3:33 PM, Lionel <lionel.keene@gmail.com> 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/


Lionel 02-21-2009 12:40 AM

Re: "import" not working?
 
On Feb 20, 4:15*pm, Matimus <mccre...@gmail.com> wrote:
> On Feb 20, 3:56*pm, Lionel <lionel.ke...@gmail.com> wrote:
>
>
>
>
>
> > On Feb 20, 3:52*pm, Chris Rebert <c...@rebertia.com> wrote:

>
> > > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <lionel.ke...@gmail.com> 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.

Gabriel Genellina 02-21-2009 08:37 AM

Re: "import" not working?
 
En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <lionel.keene@gmail.com>
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


Lionel 02-23-2009 07:24 PM

Re: "import" not working?
 
On Feb 21, 12:37*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <lionel.ke...@gmail.com> *
> 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?




Lionel 02-23-2009 08:10 PM

Re: "import" not working?
 
On Feb 23, 11:24*am, Lionel <lionel.ke...@gmail.com> wrote:
> On Feb 21, 12:37*am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
> wrote:
>
>
>
>
>
> > En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <lionel.ke...@gmail.com> *
> > 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.


All times are GMT. The time now is 04:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.