简体   繁体   English

Python为什么找不到我的路? (django的)

[英]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. 顺便说一下,已经设置了settings.py,并且manage.py syncdb可以正常工作。

You want sys.path.append('/home/myuser/svn-repos') instead. 您需要sys.path.append('/home/myuser/svn-repos') Then when you import myproject , it looks in svn-repos for the myproject folder, and looks in that for settings . 然后,当您import myproject ,它将在svn-repos查找myproject文件夹,并在其中查找settings

Alternatively, leave it as is and just import settings . 或者,保持原样,仅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. 您可能还需要考虑sys.path.insert(0, 'yourpath')因为python在该字典的开头开始并向后工作,因此您放在前面的任何内容都具有优先权,可以解决上述settings问题。

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. 如果可以避免,它不是一个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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