django - 在 sys.path.append() 之后未导入模块
问题描述
我正在尝试部署我的 django 应用程序。像许多其他人一样,我面临“缺少” django 的问题,虽然它存在,但没有名为 django 的模块。
我必须安装版本 2 的 Django,而不是最新版本,它被安装到'/home/ivan/.local/lib/python3.5/site-packages/'
.
我配置了我的 apache,wsgi.py 看起来像这样:
"""
WSGI config for mysite project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""
import os
import sys
sys.path.append('/home/ivan/.local/lib/python3.5/site-packages/')
print(sys.path)
# import /home/ivan/.local/lib/python3.5/site-packages/django
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
application = get_wsgi_application()
当我在终端中键入 python3 并执行此操作时:
>>> import django
>>> django.__path__
['/home/ivan/.local/lib/python3.5/site-packages/django']
所以我得到了 django 模块的路径并附加了它,我也尝试了:
sys.path.insert(0, '/home/ivan/.local/lib/python3.5/site-packages/django')
我仍然得到 500 错误,烦人:
[Tue Apr 21 13:12:41.861348 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] Traceback (most recent call last):
[Tue Apr 21 13:12:41.861379 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] File "/var/www/mysite/mysite/wsgi.py", line 15, in <module>
[Tue Apr 21 13:12:41.861384 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] from django.core.wsgi import get_wsgi_application
[Tue Apr 21 13:12:41.861397 2020] [wsgi:error] [pid 29470] [remote 185.201.90.67:1131] ImportError: No module named 'django'
为什么wsgi不能正常工作?我在这里做错了什么?为什么解释器在我通过 sys.path.insert() 强迫他之后找不到模块?
非常感谢提前!
更新一
print(sys.path)
wsgi 内部返回:
['/var/www/mysite', '/usr/lib/python35.zip', '/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu', '/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages',
'/home/ivan/.local/lib/python3.5/site-packages/']
更新二
由于我正在尝试部署该应用程序,因此无论我如何解决它。感谢@KolaB 对 virtualenv 的建议。
我刚刚做到了,现在我有了这个结构:
├── mysite
├── requirements.txt
└── venvdjang
venvdjang 是一个带有虚拟解释器的目录。我还安装了 djangosource /venvdjang/bin/activate
pip install -r requirements.txt
现在,我如何告诉我的 wsgi 和 apache2 我想使用 virtualenv?
更新三
谢谢大家的帮助!
我在生产中有一个带有烧瓶应用程序的 VPS (www.example.com) 现在我必须在子域 - django 上部署另一个 Web 应用程序。(www.sub.example.com)
这是 django 的配置。
/etc/apache2/sites-available/mysite.conf
<VirtualHost *:80>
ServerName sub.example.com
ServerAlias www.sub.example.com
ServerAdmin sub.example.com@gmail.com
#DocumentRoot /var/www/mysite
ErrorLog ${APACHE_LOG_DIR}/mysite-error.log
CustomLog ${APACHE_LOG_DIR}/mysite-access.log combined
#DocumentRoot /var/www/mysite/index.html
WSGIDaemonProcess mysite processes=2 threads=25 python-path=/var/www/mysite
WSGIProcessGroup mysite
WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py
Alias /robots.txt /var/www/mysite/static/robots.txt
Alias /favicon.ico /var/www/mysite/static/favicon.ico
Alias /static/ /var/www/mysite/static/
Alias /static/ /var/www/mysite/media/
<Directory /var/www/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
<Directory /var/www/mysite/static>
Require all granted
</Directory>
<Directory /var/www/mysite/media>
Require all granted
</Directory>
</VirtualHost>
这是烧瓶的配置
/etc/apache2/sites-available/FlaskApp-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
ServerAdmin example.com@mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
ServerAlias example.com
SSLCertificateFile /etc/letsencrypt/live/example.com
--p1ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com--p1ai/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:80>
ServerName example.com
Redirect / https://www.example.com
ServerAdmin admin@mywebsite.com
WSGIScriptAlias / /var/www/FlaskApp/flaskapp.wsgi
<Directory /var/www/FlaskApp/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
Alias /static /var/www/FlaskApp/FlaskApp/static
<Directory /var/www/FlaskApp/FlaskApp/static/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} = example.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
#RewriteCond %{SERVER_PORT} !^443$
#RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</VirtualHost>
</IfModule>
操作系统 - 几年前安装了 Ubuntu 16.04 mod_wsgisudo apt-get install
更新四
我WSGIDaemonProcess python-home
按照@Alasdair 的说法进行了更改,python-home=/var/www/venvdjang
但没有成功。
现在如果我在终端运行source /var/www/venvdjang/bin/activate
(venvdjang) ivan@server:/var/www$ python
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> import sys
>>> sys.path
['', '/var/www/venvdjang/lib/python35.zip', '/var/www/venvdjang/lib/python3.5',
'/var/www/venvdjang/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/venvdjang/lib/python3.5/lib-dynload',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/var/www/venvdjang/lib/python3.5/site-packages']
>>>
但是我的 wsgi.py 在删除所有sys.path.append()
员工之后仍然返回print(sys.path)
:
['/var/www/venvdjang',
'/usr/lib/python35.zip',
'/usr/lib/python3.5',
'/usr/lib/python3.5/plat-x86_64-linux-gnu',
'/usr/lib/python3.5/lib-dynload',
'/usr/local/lib/python3.5/dist-packages',
'/usr/lib/python3/dist-packages']
就像他仍然去系统解释器一样......为什么我不能引用正确的 virtualenv ?
解决方案
推荐阅读
- javascript - 如何使用 Javascript 而不是 CSS 来检索元素(图像)的位置?
- algorithm - B-Tree保存在File中的好处不是就丢失了吗?
- docker - Docker 服务:由于不支持 IPv6,无法使用浏览器连接到 localhost
- filesystems - 修改原始磁盘会损坏文件系统吗?
- python - Qt 标准密钥在 Python 中停止工作
- typescript - 加载动画,直到数据加载到 Typescript (ionic)
- php - 如何使用 php 获取数据属性?
- python - 我们应该如何处理 name_scope 的自增呢?
- javascript - Javascript 音频在点击时播放并在点击时停止
- java - java删除dynamodb中的所有项目