[英]How to use own python modules without rewriting all paths in Django
I am making a website with django that uses my python codes.我正在使用我的 python 代码的 django 创建一个网站。 my directory is like this:
我的目录是这样的:
\mysite
\myApp
\myCodes
\common
\deploy
\src
misc.py
def Args():
# commands
\mySite
my codes in /myCodes/src
have a lines at the beginning like:我在
/myCodes/src
中的代码在开头有一行:
import sys
sys.path.append('../deploy')
sys.path.append('../common')
some codes in /common
and /deploy
also have the same lines and they work fine but when I use my codes in from the view.py
they can't find each other, I added \myCodes
as an app in settings.py
and when I manually change the import dir ( import myCodes.common.myFunction
) it is fine but it seams to be unnecessary to manually change all of the imports, is there away I can execute the codes as they are? /common
和/deploy
中的一些代码也有相同的行,它们工作正常,但是当我在view.py
中使用我的代码时,它们找不到彼此,我在settings.py
中添加了\myCodes
作为应用程序,当我手动更改了导入目录( import myCodes.common.myFunction
),这很好,但似乎没有必要手动更改所有导入,我可以按原样执行代码吗?
Ps: I also use directory as input and open and write files in my functions that don't work (the are all in \myCodes
directory) Ps:我也使用目录作为输入,并在我的函数中打开和写入不起作用的文件(都在
\myCodes
目录中)
I used os.path.dirname(__file__)
+'/rest of the dir/' to give full path and it worked我使用
os.path.dirname(__file__)
+'/rest of the dir/' 提供完整路径并且它有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.