ImportError: cannot import name - newbie

Q

qwejohn

Hello,

I am quite a newbie to Python.
I am working on Linux Fedora Core 3.
I have wrote a small program named box.py which has only a constructor:


"""box.py"""

class box:
def __init__(self):
print "in box"

This program passes running "python box.py".

I had put this program under /work/dev/mytests/new

Now I want to use it from a second python program, which
resides in a totally different path.

I had tried , in a program named test.py,
"""test.py"""
sys.path = [ '/work/dev/mytests' ] + sys.path
from new import box

class test:
def __init__(self):
print "in test"

Running python test.py
I get the following error:

Traceback (most recent call last):
File "test.py", line 6, in ?
from new import box
ImportError: cannot import name box

Any idea ?

Regards,
John
 
S

Steven Bethard

"""box.py"""

class box:
def __init__(self):
print "in box"

This program passes running "python box.py".

I had put this program under /work/dev/mytests/new

Now I want to use it from a second python program, which
resides in a totally different path.

I had tried , in a program named test.py,
"""test.py"""
sys.path = [ '/work/dev/mytests' ] + sys.path
from new import box

When you say "from new import box", you're saying something like "from
the package new, import the module box". Which means that you need to
indicate that the "new" directory is a package. To do this, place an
empty file called "__init__.py" in the "new" directory (along with
"box.py"). Python should then be able to identify "new" as a package,
and find the "box" module inside of it.

HTH,

STeVe
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top