"import" not working?

Discussion in 'Python' started by Lionel, Feb 20, 2009.

  1. Lionel

    Lionel Guest

    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
    Lionel, Feb 20, 2009
    #1
    1. Advertising

  2. 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
    Emile van Sebille, Feb 20, 2009
    #2
    1. Advertising

  3. Lionel

    Chris Rebert Guest

    On Fri, Feb 20, 2009 at 3:33 PM, Lionel <> 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
    Chris Rebert, Feb 20, 2009
    #3
  4. Lionel

    Lionel Guest

    On Feb 20, 3:52 pm, Chris Rebert <> wrote:
    > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <> 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?
    Lionel, Feb 20, 2009
    #4
  5. Lionel

    Matimus Guest

    On Feb 20, 3:56 pm, Lionel <> wrote:
    > On Feb 20, 3:52 pm, Chris Rebert <> wrote:
    >
    >
    >
    > > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <> 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
    Matimus, Feb 21, 2009
    #5
  6. Lionel

    Steve Holden Guest

    Chris Rebert wrote:
    > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <> 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/
    Steve Holden, Feb 21, 2009
    #6
  7. Lionel

    Lionel Guest

    On Feb 20, 4:15 pm, Matimus <> wrote:
    > On Feb 20, 3:56 pm, Lionel <> wrote:
    >
    >
    >
    >
    >
    > > On Feb 20, 3:52 pm, Chris Rebert <> wrote:

    >
    > > > On Fri, Feb 20, 2009 at 3:33 PM, Lionel <> 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.
    Lionel, Feb 21, 2009
    #7
  8. En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <>
    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
    Gabriel Genellina, Feb 21, 2009
    #8
  9. Lionel

    Lionel Guest

    On Feb 21, 12:37 am, "Gabriel Genellina" <>
    wrote:
    > En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <>  
    > 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, Feb 23, 2009
    #9
  10. Lionel

    Lionel Guest

    On Feb 23, 11:24 am, Lionel <> wrote:
    > On Feb 21, 12:37 am, "Gabriel Genellina" <>
    > wrote:
    >
    >
    >
    >
    >
    > > En Fri, 20 Feb 2009 22:40:03 -0200, Lionel <>  
    > > 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.
    Lionel, Feb 23, 2009
    #10
  11. En Mon, 23 Feb 2009 18:10:07 -0200, Lionel <>
    escribió:

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

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 23, 2009
    #11
  12. Lionel

    Rhodri James Guest

    On Mon, 23 Feb 2009 19:24:46 -0000, Lionel <> wrote:

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

    --
    Rhodri James *-* Wildebeeste Herder to the Masses
    Rhodri James, Feb 24, 2009
    #12
  13. Lionel

    Aahz Guest

    In article <>,
    Rhodri James <> wrote:
    >
    >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.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "All problems in computer science can be solved by another level of
    indirection." --Butler Lampson
    Aahz, Mar 12, 2009
    #13
  14. Lionel

    Lie Ryan Guest

    Scott David Daniels wrote:
    > Aahz wrote:
    >> In article <>,
    >> Rhodri James <> wrote: ...
    >>> 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.

    >
    > 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\ '
    Lie Ryan, Mar 13, 2009
    #14
  15. Lionel

    Steve Holden Guest

    Lie Ryan wrote:
    > Scott David Daniels wrote:
    >> Aahz wrote:
    >>> In article <>,
    >>> Rhodri James <> wrote: ...
    >>>> 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.

    >>
    >> 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\ '


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

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Want to know? Come to PyCon - soon! http://us.pycon.org/
    Steve Holden, Mar 13, 2009
    #15
  16. Lionel

    Lie Ryan Guest

    Steve Holden wrote:
    > Lie Ryan wrote:
    >> Scott David Daniels wrote:
    >>> Aahz wrote:
    >>>> In article <>,
    >>>> Rhodri James <> wrote: ...
    >>>>> 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.
    >>> 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\ '

    >
    > 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]
    Lie Ryan, Mar 13, 2009
    #16
  17. En Fri, 13 Mar 2009 14:27:27 -0200, Lie Ryan <> escribió:
    > Steve Holden wrote:
    >> Lie Ryan wrote:
    >>> Scott David Daniels wrote:
    >>>> Aahz wrote:
    >>>>> In article <>,
    >>>>> Rhodri James <> wrote: ...
    >>>>>> 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.
    >>>> 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\ '

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

    --
    Gabriel Genellina
    Gabriel Genellina, Mar 13, 2009
    #17
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul Opal
    Replies:
    12
    Views:
    926
    Paul Opal
    Oct 11, 2004
  2. Joel Hedlund
    Replies:
    2
    Views:
    488
    Joel Hedlund
    Nov 11, 2006
  3. Joel Hedlund
    Replies:
    0
    Views:
    294
    Joel Hedlund
    Nov 11, 2006
  4. John Boy
    Replies:
    1
    Views:
    302
    John Machin
    Mar 10, 2008
  5. Peng Yu
    Replies:
    0
    Views:
    316
    Peng Yu
    Oct 31, 2009
Loading...

Share This Page