"import" not working?

Lionel lionel.keene at gmail.com
Mon Feb 23 15:10:07 EST 2009


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



More information about the Python-list mailing list