简体   繁体   中英

Why can't Python find my path? (django)

import sys
sys.path.append('/home/myuser/svn-repos/myproject')
from myproject.settings import *

But, it says module not found when I run the script? By the way, settings.py has been set up and manage.py syncdb works.

You want sys.path.append('/home/myuser/svn-repos') instead. Then when you import myproject , it looks in svn-repos for the myproject folder, and looks in that for settings .

Alternatively, leave it as is and just import settings . This is less good because it's less specific and you may end up importing something other than what you intend.

You may also want to consider sys.path.insert(0, 'yourpath') because python starts at the beginning of that dict and works backwards, so whatever you put at the front takes precedence, solving the aforementioned settings problem.

Try:

import sys
sys.path.append('/home/myuser/svn-repos/myproject')
from settings import *

Note that

from settings import *

makes it difficult to track down where imported variables come from. It is not a good practive if you can avoid it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM