开发者

How do I access my classes from the python console on MAC OSX?

开发者 https://www.devze.com 2022-12-19 09:10 出处:网络
I\'m trying to access my cla开发者_开发技巧sses 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 ca

I'm trying to access my cla开发者_开发技巧sses 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.


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号