#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Import error - new to python


    I'm new to python and am trying to just get some basic stuff up and going.

    I have a very basic module called foo

    It's in the following directory on my machine

    C:\workspace\PyFoo\src\foo
    In that folder is __init__.py (created automatically) and foo.py

    foo.py looks like this

    Code:
    class foo(): 
        def __init__(self, name, number): 
            self.name = name 
            self.number = number 
        def getName(self): 
            return self.name 
        def getNumber(self): 
            return self.number
    If I open up command prompt and do following it works:

    C:\workspace\PyFoo\src\foo>python
    Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from foo import foo
    >>> f = foo(1,2)
    >>> f.getName()
    1
    >>>

    However, if I run this from C:\ I get the following

    C:\>python
    Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from foo import foo
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    ImportError: No module named 'foo'
    >>>

    I thought, well maybe it's a system error

    >>> import sys
    >>> print(sys.path)
    ['', 'C:\\Python33', 'C:\\Python33\\Lib', 'C:\\Python33\\DLLs', 'C:\\workspace', 'C:\\Windows\\system32\\python33.zip',
    'C:\\Python33\\lib\\site-packages']
    >>>

    PHP Code:
    C:\>echo %PYTHONPATH
    C:\Python33;C:\Python33\Lib;C:\Python33\DLLs;C:\workspace 
    However, that seems OK.

    Is there something I'm missing?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,851
    Rep Power
    481
    You really ought to start out your DOS commands with
    Code:
    A:> cd where\ever\I\put\my\python\module\this\is\a\path
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    610
    Rep Power
    65
    You could do ...
    sys.path.append('C:/workspace/PyFoo/src/foo' ) at the beginning of your program.
    Note, Windows can use '/' instead of '\'
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

IMN logo majestic logo threadwatch logo seochat tools logo