import

D

David Jackson

Hi all,

I'm a real beginner with python but have what I think is a simple question.
I am writing some simple modules and would like to place them into a
subdirectory. But then I cannot seem to import them. I have tried the
following.

I wrote a module called fibo.py with some definitions in it (one called
fibo). In the same directory, I wrote another file (test.py) and began with
import fibo. This worked fine and I was able to use the function fibo as
fibo.fibo. Then, I made a directory called test and placed the file fibo.py
in this directory. I also placed a blank file called _init_.py into this
directory. I went back to the original directory and tried to import
test.fibo but this fails. I get the following error message:

Traceback (innermost last)
File "...test.py", line 1, in ?
import test.fibo
File "...test.py", line 1, in ?
import test.fibo
ImportError: No module named fibo

Any help would be greatly appreciated. If it makes any difference, I'm
working on a Mac, OSX 10.3.9

Thanks,
David
 
F

faulkner

that should be __init__.py [TWO underscores].
and you might want to import sys and check sys.path [the list of
directories searched by the import mechanism].
 
A

Anand

David said:
Hi all,

I'm a real beginner with python but have what I think is a simple question.
I am writing some simple modules and would like to place them into a
subdirectory. But then I cannot seem to import them. I have tried the
following.

I wrote a module called fibo.py with some definitions in it (one called
fibo). In the same directory, I wrote another file (test.py) and began with
import fibo. This worked fine and I was able to use the function fibo as
fibo.fibo. Then, I made a directory called test and placed the file fibo.py
in this directory. I also placed a blank file called _init_.py into this
directory. I went back to the original directory and tried to import
test.fibo but this fails. I get the following error message:

Traceback (innermost last)
File "...test.py", line 1, in ?
import test.fibo
File "...test.py", line 1, in ?
import test.fibo
ImportError: No module named fibo

python is trying to import fibo from test.py not test directory. since
you have both test.py and test directory, it is using the first one.
try to rename your directory name to something else and try. that
should work.

Also you should do
from test import fibo


Anand
 
D

David Jackson

Indeed you are correct...that is indeed TWO underscores and everything works
fine now. Thanks for pointing out the obvious...I thought it was a simple
problem. --DJ

faulkner said:
that should be __init__.py [TWO underscores].
and you might want to import sys and check sys.path [the list of
directories searched by the import mechanism].


David said:
Hi all,

I'm a real beginner with python but have what I think is a simple
question.
I am writing some simple modules and would like to place them into a
subdirectory. But then I cannot seem to import them. I have tried the
following.

I wrote a module called fibo.py with some definitions in it (one called
fibo). In the same directory, I wrote another file (test.py) and began
with
import fibo. This worked fine and I was able to use the function fibo as
fibo.fibo. Then, I made a directory called test and placed the file
fibo.py
in this directory. I also placed a blank file called _init_.py into this
directory. I went back to the original directory and tried to import
test.fibo but this fails. I get the following error message:

Traceback (innermost last)
File "...test.py", line 1, in ?
import test.fibo
File "...test.py", line 1, in ?
import test.fibo
ImportError: No module named fibo

Any help would be greatly appreciated. If it makes any difference, I'm
working on a Mac, OSX 10.3.9

Thanks,
David
 

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,981
Messages
2,570,187
Members
46,730
Latest member
AudryNolan

Latest Threads

Top