tags:

views:

55

answers:

3

I'm trying to access my classes via

from project import * 

But from the python console something seems to be off with the paths. How do I set the correct paths to my project so I can import classes?

My models are stored in:

/Users/username/project/project/model

from project import *

And the error reads:

ImportError: No module named project

Thanks.

+1  A: 

Most likely you will have to set the PYTHONPATH env variable, or change in the correct directory.

I assume you do not start your console from: /Users/username/project

You have several options now:

  1. Change to that directory
  2. Set the PYTHONPATH env variable to that directory (however that is done in MacOSX)
  3. Use the site module to add the path: python docs
ebo
+4  A: 

You have the following choices

  • Start your python session in the /User/username/project folder
  • Change your import line to from project.project import *
  • Set the PYTHONPATH environment variable to /User/username/project (setenv PYTHONPATH /User/username/project)
  • Append /User/username/project to sys.path

import sys

sys.path.append('/User/username/project')

Robert Christie
+1  A: 

This might be a silly suggestion, but do you have a __init__.py file in the module you're importing? if not, then create an empty one. You're also going to need to run from project import * from the /Users/name/project/ directory. ie: you'll need to start the python CLI from /Users/name/project/. If that isnt suitable thenas already suggested you can change where python looks for modules.

As a sidenote, using from module import * is commonly seen as bad form. Try to specify what you want imported.

brice