python import error

S

smilesonisamal

Hi,
I am a new bie in python I was trying to execute the python script aaa.py in fedora which imports different modules. Actually when I am executing the aaa.py I am getting the following error and PYTHON_PATH=/bin/python which has a symlink to python-2.7. Please help me in this regard? I am not getting any clue where I am wrong.


python -c "import sys; print sys.path"
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2..7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gst-0.10', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages', '/usr/lib/python2..7/site-packages/setuptools-0.6c11-py2.7.egg-info']


aaa.py
=======
Traceback (most recent call last):
File "aaa.py", line 5, in <module>
from ccc.ddd import sss
ImportError: No module named ccc.ddd

directory structure as follows:

ccc
|
ddd
|
aaa.py
sss.py


Regards
Pradeep
 
M

Michael Torrie

Traceback (most recent call last):
File "aaa.py", line 5, in <module>
from ccc.ddd import sss
ImportError: No module named ccc.ddd

directory structure as follows:

ccc
|
ddd
|
aaa.py
sss.py

This is because directories have no inherent meaning to python. A
module is normally a file. If you want to group a bunch of files
together you have to use a package. See the docs on how to make a
package. But the namespace structure within a package isn't based on
the directory tree either. It's usually set up by __init__.py within
the package which imports modules defined in the package into the
namespace.

http://docs.python.org/3/tutorial/modules.html#packages

Hope this helps.
 
J

John Gordon

In said:
File "aaa.py", line 5, in <module>
from ccc.ddd import sss
ImportError: No module named ccc.ddd
directory structure as follows:
ccc
|
ddd
|
aaa.py
sss.py

A python file isn't importable unless the directory also contains a file
named __init__.py .

Try making __init__.py files in the ccc and ddd directories. If you
don't know what to put in them, just leave them blank.
 
M

Michael Torrie

This is because directories have no inherent meaning to python. A
module is normally a file. If you want to group a bunch of files
together you have to use a package. See the docs on how to make a
package. But the namespace structure within a package isn't based on
the directory tree either. It's usually set up by __init__.py within
the package which imports modules defined in the package into the
namespace.

http://docs.python.org/3/tutorial/modules.html#packages

Hope this helps.

I didn't quite get that right. Follow John's advice on __init.py__ and
you'll have what you want I think.
 
S

smilesonisamal

A python file isn't importable unless the directory also contains a file

named __init__.py .



Try making __init__.py files in the ccc and ddd directories. If you

don't know what to put in them, just leave them blank.


It is having __init__.py as blank in ccc and ddd directories. But it still doesnot work.
--

John Gordon Imagine what it must be like for a real medical doctor to

(e-mail address removed) watch 'House', or a real serial killer to watch 'Dexter'.
 
B

Benjamin Kaplan

What directory are you in when you execute the script? Python doesn't
look up for packages so if you're in ddd and calling "python aaa.py",
Python doesn't know about the ccc package. You'd just import sss, or
add the parent directory of ccc to sys.path.
 
J

John Gordon

In said:
It is having __init__.py as blank in ccc and ddd directories. But it
still doesnot work.

What directory are you in when you run your python command? As written,
your import will only work if you're in the parent directory of ccc (or
that directory is in your PYTHONPATH.)
 

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,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top