7

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?

phrogg
9081 gold badge13 silver badges28 bronze badges
asked Jul 31, 2013 at 16:43
1
  • 1
    Made an edit, added init.py but the problem is still there Commented Jul 31, 2013 at 16:51

4 Answers 4

7

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

answered Jul 31, 2013 at 16:45
Sign up to request clarification or add additional context in comments.

Comments

2

you nee to have an __init__.py file in each folder you import from

answered Jul 31, 2013 at 16:45

Comments

1

You have to create a file called "__init__.py" at python directories, then "the Python" will understand that directory as a Python package.

answered Jul 31, 2013 at 16:46

Comments

1

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()

answered Jul 31, 2013 at 16:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.