Path in the PYTHONPATH not in django path

六眼飞鱼酱① 提交于 2021-01-28 20:48:54

问题


I am writing a web based app based on python and django. I have a source code folder containing LIBS directory that has a file named utils.py. When I want to install my app a new line is added to ~/.profile file like export PYTHONPATH=$PYTHONPATH:/home/test/src/LIBS (The path is added based on the installation path) When I run the below code in the interpreter the path is OK:

import sys
sys.path 

['', '/usr/lib/python2.7', '/home/test/src/LIBS', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']

Unfortunately, when i want to load the home page of my app the line that imports utils raises an exception

What i do wrong? Thanks in advanced.


回答1:


Your ~/.profile only adds the dir to the PYTHONPATH in the current shell environment. It's not globally available. When django loads, it uses the wsgi.py and another project path.

The easiest way to add a globally available path is to add a .pth file. It should be in your python dist-packages directory (depends on the OS):

$ sudo nano /usr/lib/python2.7/dist-packages/
/home/test/src/LIBS

And save the file.

Now the app will be available for all python instances on your machine.

If you want to add this path only to a specific django project, in the wsgi.py add:

import sys
sys.path.append("/home/test/src/LIBS")


来源:https://stackoverflow.com/questions/35548720/path-in-the-pythonpath-not-in-django-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!