简体   繁体   English

某些 python 库未加载到我的 Django 生产应用程序中,但在本地服务器中加载正常

[英]Certain python libraries are not loading in my Django application in production, but load fine in local server

In my views.py file of my Django application I'm trying to load the 'transformers' library with the following command:在我的 Django 应用程序的 views.py 文件中,我尝试使用以下命令加载“变形金刚”库:

from transformers import pipeline

This works in my local environment, but on my Linux server at Linode, when I try to load my website, the page tries to load for 5 minutes then I get a Timeout error.这适用于我的本地环境,但在我位于 Linode 的 Linux 服务器上,当我尝试加载我的网站时,该页面尝试加载 5 分钟,然后出现超时错误。 I don't understand what is going on, I know I have installed the library correctly.我不明白发生了什么,我知道我已经正确安装了库。 I have also run the same code in the python shell on my server and it loads fine, it's is just that if I load it in my Django views.py file, no page of my website loads.我还在我的服务器上的 python shell 中运行了相同的代码并且它加载正常,只是如果我将它加载到我的 Django views.py 文件中,我的网站没有页面加载。

My server: Ubuntu 20.04 LTS, Nanode 1GB: 1 CPU, 25GB Storage, 1GB RAM我的服务器:Ubuntu 20.04 LTS,Nanode 1GB:1 个 CPU,25GB 存储空间,1GB RAM

Library: transformers==3.0.2图书馆:变形金刚==3.0.2

I also have the same problem when I try to load tensorflow. All the other libraries are loading fine, like pytorch and pandas etc. I've been trying to solve this problem since more than a week, I've also changed hosts from GCP to Linode, but it's still the same.当我尝试加载 tensorflow 时,我也遇到了同样的问题。所有其他库都加载正常,如 pytorch 和 pandas 等。一个多星期以来我一直在尝试解决这个问题,我还从 GCP 更改了主机到 Linode,但它仍然是一样的。

Edit: I created a new server and installed everything from scratch and used a virtualenv this time, but still its the same problem.编辑:我创建了一个新服务器并从头开始安装了所有东西,这次使用了 virtualenv,但仍然是同样的问题。 Following are the installed libraries outputted from pip freeze :以下是pip freeze输出的已安装库:

asgiref==3.2.10
certifi==2020.6.20
chardet==3.0.4
click==7.1.2
Django==3.0.7
djangorestframework==3.11.0
filelock==3.0.12
future==0.18.2
idna==2.10
joblib==0.16.0
numpy==1.19.1
packaging==20.4
Pillow==7.2.0
pyparsing==2.4.7
pytz==2020.1
regex==2020.7.14
requests==2.24.0
sacremoses==0.0.43
sentencepiece==0.1.91
six==1.15.0
sqlparse==0.3.1
tokenizers==0.8.1rc1
torch==1.5.1+cpu
torchvision==0.6.1+cpu
tqdm==4.48.0
transformers==3.0.2
urllib3==1.25.10

I also know transformers library is installed because if I try to import some library that doesn't exist then I simply get an error, like I should.我也知道安装了 transformers 库,因为如果我尝试导入一些不存在的库,那么我只会得到一个错误,就像我应该的那样。 But in this case it just loads forever and doesn't output any error.但在这种情况下,它只是永远加载并且不会 output 任何错误。 This is so bizarre.这太奇怪了。

Maybe you just have to create or update your requirements.txt file.也许您只需要创建或更新您的requirements.txt文件。

Here is the command: pip freeze > requirements.txt这是命令: pip freeze > requirements.txt

Based on this answer基于这个答案

Some third party packages for Python which use C extension modules, and this includes scipy and numpy, will only work in the Python main interpreter and cannot be used in sub interpreters as mod_wsgi by default uses. Python 的一些第三方包使用 C 扩展模块,包括 scipy 和 numpy,只能在 Python 主解释器中使用,不能在子解释器中使用,因为 mod_wsgi 默认使用。

transformers library uses numpy, so you should force the WSGI application to run in the main interpreter of the process by changing apache config: transformers库使用 numpy,因此您应该通过更改 apache 配置强制 WSGI 应用程序在进程的主解释器中运行:

## open apache config
$ nano /etc/apache2/sites-enabled/000-default.conf

## add this line to apache config
 WSGIApplicationGroup %{GLOBAL}

## restart apache
$ systemctl restart apache2

now it works!!现在可以了!!

for more information visit the link above.欲了解更多信息,请访问上面的链接。

暂无
暂无

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

相关问题 登录 Python 和 Django 应用程序时本地服务器上出现错误 - Error coming on the Local Server while logging in the Python and Django Application 为什么我的 virtualenv python3 在我的本地机器上工作正常,但当我将 virtualenv 上传到服务器时却不行? - Why does my virtualenv python3 work fine on my local machine but not when I upload the virtualenv to the server? 如何通过python本地服务器包含库 - How to include libraries through the python local server Django表单不在生产服务器上呈现 - 使用testserver在本地工作正常,并且单元测试在生产服务器上传递 - Django forms not rendering on production server - work fine locally with testserver, and unit tests pass on production server Django:通过FTP将应用程序从本地服务器推送到生产服务器 - Django: Push app from local server to production server via FTP Django项目:生产服务器,使用python 3 - Django project : production server, use python 3 Django/Vue -> Heroku:静态文件未在生产服务器上加载 - Django/Vue -> Heroku: Static files not loading at production server django order_by 不在服务器上工作,但在本地机器上工作正常 - django order_by not working on server but works fine on local machine Python Django:最小的Django + Cassandra本地应用程序 - Python Django: Minimal Django + Cassandra local application Django-在本地服务器上一切正常,但在Postgres中过滤错误的查询 - Django - In local server all work fine but in Postgres is filtering the bad a query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM