I have my program set up using packages as followed:
-base
-init.py
-base_class.py
-test
-init.py
-test.py
When I do the import statement from base.base_class import BaseClass in the test.py I get this error when running it:
from base.base_class import BaseClass
ImportError: No module named base.base_class
How can I import this module?
-
1Made an edit, added init.py but the problem is still thereGreg Brown– Greg Brown2013年07月31日 16:51:08 +00:00Commented Jul 31, 2013 at 16:51
4 Answers 4
at the top of test.py add
import sys
sys.path.append("..")
base is not a folder on the path...once you change this it should work
or put test.py in the same folder as base. or move base to somewhere that is on your path
Comments
you nee to have an __init__.py file in each folder you import from
Comments
You have to create a file called "__init__.py" at python directories, then "the Python" will understand that directory as a Python package.
Comments
there are 3 things you can do:
add an init.py file to each folder
add sys.path.append("Folder") to the top
or use imp and do;
import imp
foo = imp.load_source('filename', 'File\Directory\filename.py')
then foo will be the name of the module for example foo.method()