开发者

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

import sys
sys.path.append('开发者_C百科/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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜