简体   繁体   English

Django找不到本地安装的Python模块

[英]Django can't find locally-installed Python modules

I'm running a Django-based website that's hosted on a shared server (ie I have only a limited account, no admin privileges). 我正在运行一个基于Django的网站,该网站托管在共享服务器上(即,我只有一个受限帐户,没有管理员权限)。 The server's administrator installed the Django modules for Python 2.7, but now I want to add a 3rd-party Django library I found called django-bleach. 服务器的管理员为Python 2.7安装了Django模块,但现在我想添加一个名为django-bleach的第三方Django库。 So I installed it locally, using 所以我使用

pip install --user django-bleach

Then, following the instructions in the django-bleach documentation , I added 'django-bleach' to my INSTALLED_APPS list in settings.py . 然后,按照django-bleach文档中的说明 ,我在settings.py INSTALLED_APPS列表中添加了“ django-bleach”。 However, when I touched wsgi.py to get Apache to reload my code, I got an ImportError in my Apache log. 但是,当我触摸wsgi.py以使Apache重新加载代码时,我的Apache日志中出现了ImportError。 Usually this means that the module I'm trying to import isn't in the PYTHONPATH, so I put a print sys.path statement in wsgi.py . 通常,这意味着我要导入的模块不在PYTHONPATH中,因此我在wsgi.py放入了一条print sys.path语句。 This is what happens: 这是发生了什么:

[Mon Nov 24 13:44:58 2014] [error] sys.path is: ['/extra/www/html/quotes/quotes_django', '/opt/rh/python27/root/usr/lib64/python2.7/site-packages', 
'/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', 
'/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages', '/users/ejt64/.local/lib/python2.7/site-packages',
'/users/ejt64/.local/lib/python2.7/site-packages', '/users/ejt64/.local/lib/python2.7/site-packages']
[Mon Nov 24 13:44:58 2014] [error] mod_wsgi (pid=29372): Target WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py' cannot be loaded as Python module.
[Mon Nov 24 13:44:58 2014] [error] mod_wsgi (pid=29372): Exception occurred processing WSGI script '/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py'.
[Mon Nov 24 13:44:58 2014] [error] Traceback (most recent call last):
[Mon Nov 24 13:44:58 2014] [error]   File "/extra/www/html/quotes/quotes_django/quotes_django/wsgi.py", line 17, in <module>
[Mon Nov 24 13:44:58 2014] [error]     application = get_wsgi_application()
[Mon Nov 24 13:44:58 2014] [error]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Mon Nov 24 13:44:58 2014] [error]     django.setup()
[Mon Nov 24 13:44:58 2014] [error]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/django/__init__.py", line 21, in setup
[Mon Nov 24 13:44:58 2014] [error]     apps.populate(settings.INSTALLED_APPS)
[Mon Nov 24 13:44:58 2014] [error]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/django/apps/registry.py", line 85, in populate
[Mon Nov 24 13:44:58 2014] [error]     app_config = AppConfig.create(entry)
[Mon Nov 24 13:44:58 2014] [error]   File "/opt/rh/python27/root/usr/lib64/python2.7/site-packages/django/apps/config.py", line 87, in create
[Mon Nov 24 13:44:58 2014] [error]     module = import_module(entry)
[Mon Nov 24 13:44:58 2014] [error]   File "/usr/lib64/python2.7/importlib/__init__.py", line 37, in import_module
[Mon Nov 24 13:44:58 2014] [error]     __import__(name)
[Mon Nov 24 13:44:58 2014] [error] ImportError: No module named django_bleach

Note that the /users/ejt64/.local/lib/python2.7/site-packages/ directory, which appears three times in sys.path, is my user-local Python location where django-bleach is installed. 请注意,在sys.path中出现3次的/users/ejt64/.local/lib/python2.7/site-packages/目录是我的django-bleach安装在用户本地的Python位置。 I made this directory world-readable, in case the Apache process was running into a permissions problem accessing my directory. 如果Apache进程在访问我的目录时遇到权限问题,那么我将该目录设置为全球可读。 So how can I be getting an ImportError? 那么,如何获取ImportError?

It must be something to do with WSGI/Django, though, because if I just run Python in my Django app directory, I can do this: 不过,这一定与WSGI / Django有关,因为如果我只是在Django应用程序目录中运行Python,则可以这样做:

Python 2.7.5 (default, Dec  3 2013, 08:35:16) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django_bleach
>>> 

But my Django application somehow can't find the django_bleach module, even though it's looking in the right location. 但是,即使我的Django应用程序在正确的位置查找,它仍无法找到django_bleach模块。

Additional Info : My wsgi.py is just the standard one generated by django-admin startproject , plus a print statement: 附加信息 :我的wsgi.py只是django-admin startproject生成的标准django-admin startproject ,外加一条打印语句:

import os 
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "quotes_django.settings")

import sys
print >>sys.stderr, "sys.path is: " + str(sys.path)

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

Well, I finally solved my problem. 好吧,我终于解决了我的问题。 It turns out that although /users/ejt64/.local/lib/python2.7/site-packages/ was on the PYTHONPATH being searched by WSGI, I had forgotten to make one of the directories along that path world- executable , so the Apache user couldn't access it. 事实证明,尽管/users/ejt64/.local/lib/python2.7/site-packages/在WSGI搜索的PYTHONPATH上,但我忘了沿该路径将其中一个目录设置为世界可执行文件 ,因此Apache用户无法访问它。

Although all the directories and files were world- readable , a user can't read a world-readable file unless it has execute permissions on all the directories that contain it, and it turns out my home directory ( /users/ejt64 ) was world-readable but not world-executable. 尽管所有目录和文件都是世界可读的 ,但用户无法读取世界可读的文件,除非该文件对包含它的所有目录都具有执行权限,并且我的主目录( /users/ejt64 )是世界可读,但不可全局执行。 Once I made this directory world-executable, the ImportError went away. 一旦将此目录设置为世界可执行文件, ImportError就消失了。

It sure would be nice if I could make Apache/WSGI/Python print an error message when it tries to read a directory on the PYTHONPATH but can't access it due to permissions. 如果我能使Apache / WSGI / Python在尝试读取PYTHONPATH上的目录但由于权限而无法访问它时输出一条错误消息,那肯定会很好。

~/.local/lib/python2.7/site-packages/

Is relative to the user running apache. 是相对于用户运行的apache。 What user is running apache? 哪个用户正在运行apache? Typically apache runs as the default user 'nobody' or 'apache'. 通常,apache以默认用户“ nobody”或“ apache”运行。 Having a relative path to '~' will point to the home directory of the user running apache. 相对路径“〜”将指向运行apache的用户的主目录。

I'd recommend using a virtualenv or specifying the absolute path to your local pip packages. 我建议使用virtualenv或指定本地pip包的绝对路径。 Going into your site-packages directory and running pwd will let you know the absolute path. 进入您的site-packages目录并运行pwd将使您知道绝对路径。 You may need to alter the permissions of your pip packages and directories as well if the user running apache can't read/write/execute against them. 如果运行apache的用户无法对其进行读/写/执行,则可能还需要更改pip包和目录的权限。

It works fine when you drop into the Python REPL because you are executing everything as your user and '~' points to your home directory. 当您进入Python REPL时,它可以正常工作,因为您以用户身份执行所有操作,并且'〜'指向您的主目录。

If you have access to edit the apache config files on your server, I'd recommend taking a look at this Django documentation, https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#using-a-virtualenv Event if you don't use a virtualenv you will still need to set this accordingly to the absolute path of your local site-packages directory. 如果您有权编辑服务器上的apache配置文件,建议您阅读以下Django文档: https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#using -a-virtualenv事件,如果您不使用virtualenv,则仍需要将其相应地设置为本地site-packages目录的绝对路径。

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

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

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