繁体   English   中英

NameError:名称'hasattr'未定义 - Python3.6,Django1.11,Ubuntu16-17,Apache2.4,mod_wsgi

[英]NameError: name 'hasattr' is not defined - Python3.6, Django1.11, Ubuntu16-17, Apache2.4, mod_wsgi

我已经设置了我的Python / Django虚拟环境,并且在守护进程模式下设置了mod_wsgi,并且非常确定(之前完成此操作)它“ 大部分都是正确的 ”,除非我收到以下错误...

[Thu Jul 06 00:35:26.986363 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00493: SIGUSR1 received.  Doing graceful restart
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 00:35:27.194483 2017] [mpm_event:notice] [pid 11442:tid 140557758930432] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming normal operations
[Thu Jul 06 00:35:27.194561 2017] [core:notice] [pid 11442:tid 140557758930432] AH00094: Command line: '/usr/sbin/apache2'

我的django应用程序本身通过wsgi.py加载正常,但它似乎与核心python(我的设置错误)可能会出错: NameError: name 'hasattr' is not defined

在浏览器中 - 我得到一个简单的“服务器错误(500)”页面,而不是标准的Apache“内部服务器错误”页面。

离开我的VirtualHost和这里的步骤是我为自己整理的基本步骤,如果你能发现任何东西......(我已经尝试了所有不同的python包,而不仅仅是-venv)

Install Python 3.6 and virtualenv
    sudo apt-get update
    sudo apt-get install python3.6-venv
    sudo apt-get install virtualenv
(or find the latest and greatest python package that includes pip https://packages.ubuntu.com/ )


Install Apache2
    sudo apt-get install apache2 apache2-dev


Make and enter a folder for your project - then build a Virtual Environment in it
    mkdir ~/example.com
    cd ~/example.com
    virtualenv --python=/usr/bin/python3.6 py361ve

Enter your new Virtual Environment to install packages to it
    source py361ve/bin/activate

Install Django, mod_wsgi, and any other needed packages
    pip install django
    pip install mod_wsgi
    pip install ...
(no need for pip3 in virtual environment - django should be the latest release)


Run following command and place output in apache config file ( in /etc/apache2/ )
    mod_wsgi-express module-config


Exit your virtual environment
    deactivate
(You can re-enter your virtual environment any time using the source method in step 8)

这是当我停止/启动/重启apache2时会发生什么...

apache2 stop...

[Thu Jul 06 06:01:34.190940 2017] [mpm_event:notice] [pid 2015:tid 140157449797120] AH00491: caught SIGTERM, shutting down
_______________________________________________________________
apache2 start...

[Thu Jul 06 06:02:39.076741 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:02:39.076890 2017] [core:notice] [pid 2181:tid 140553545080320] AH00094: Command line: '/usr/sbin/apache2'
_______________________________________________________________
apache2 restart...

Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/home/jamin/www/dev.tir.com/py361ve/lib/python3.6/site-packages/PIL/Image.py", line 572, in __del__
NameError: name 'hasattr' is not defined
[Thu Jul 06 06:05:43.307877 2017] [mpm_event:notice] [pid 2181:tid 140553545080320] AH00491: caught SIGTERM, shutting down
[Thu Jul 06 06:05:43.492499 2017] [mpm_event:notice] [pid 2301:tid 140353155558912] AH00489: Apache/2.4.25 (Ubuntu) mod_wsgi/4.5.15 Python/3.6 configured -- resuming $
[Thu Jul 06 06:05:43.492705 2017] [core:notice] [pid 2301:tid 140353155558912] AH00094: Command line: '/usr/sbin/apache2'

这可能是由于在进程关闭时销毁Python解释器时代码仍在后台线程中运行。 在解释器销毁期间发生的事情是所有模块都被清除,并且事物的属性访问通常返回None作为后备。 在这种情况下,在PIL对象被破坏之前,看起来像builtins模块已被擦除,因此无法找到hasattr。

您是否可以确认只有在重新启动Apache并处理关闭时才会发生这种情况?

我也遇到了同样的问题。

[Wed Sep 12 16:30:19 2018] [notice] Apache/2.2.15 (Unix) DAV/2 mod_wsgi/4.6.4     
Python/3.6 configured -- resuming normal operations
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
Exception ignored in: <object repr() failed>
Traceback (most recent call last):
  File "/mnt/workshop/py36env/lib/python3.6/site-packages/PIL/Image.py", line 600, in __del__
NameError: name 'hasattr' is not defined
[Wed Sep 12 16:33:57 2018] [notice] caught SIGTERM, shutting down
[Wed Sep 12 16:33:58 2018] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)

它可能发生在你制作一个新目录但从未授权的时候。

我通过以下方法修复了它:

chmod -R 755 /mnt/workshop/your_project_path
chown -R apache:apache /mnt/workshop/your_project_path

尝试完全卸载PIL,然后重新安装Pillow包。

实际上PIL旧包和Pillow包中存在冲突,这与新版本的django相冲突。

您需要安装新的Pillow包。

首先卸载两个

sudo pip3 uninstall pil
sudo pip3 uninstall pillow

重新安装枕头

sudo pip3 install pillow

暂无
暂无

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

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