Home >Backend Development >Python Tutorial >Everyone should pay attention to the landmines encountered by Django
I’ve been messing with Django’s WSGI application recently. Although Django’s own runserver is very convenient, it can’t do anything for complex functions.
First I encountered a pitfall on Windows, then on Ubuntu that comes with Windows 10, and finally solved it on a virtual machine.
It can be seen from the previous article Django Notes "Django Learning Notes (2) First Web Page" that I use cmd in the windows10 system to operate Django, because the gunicorn application is in the unix system When running, an error occurs when it is forced to run on the win platform.
1. Windows encounters a pit:
First enter in cmd: pip3 install gunicorn. After the installation is successful, switch to the project directory (G:/Django/hello). The specific operation is to switch first Go to the G drive, enter G: directly, and then enter cd Django\hello. G:\Django\hello> will appear. Then run gunicorn. In the Django project, the format is: gunicorn yourproject.wsgi [-b 127.0.0.1.8000]. The fields in brackets are optional. For other command parameters, you can send gunicorn -h to get the help documentation. My project here is hello, so my command operation is: gunicorn hello.wsgi. Then, an error occurs: ModuleNotFoundError: No module named 'pwd', there is no pwd module, okay, I can't find it. The found code is posted below. We put the found pwd.py into the python3 installation directory D:\Program Files\Python\Python36\Lib. Then we ran gunicorn hello.wsgi and the result was another error: AttributeError: module 'socket' has no attribute 'AF_UNIX' , and then I looked for it online. Because it was gunicorn in the unix system, I couldn't find the sock.py file in liunx, so I switched to the unix system and gave up on windows.
G:\Django\hello>gunicorn hello.wsgi Traceback (most recent call last): File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec) File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals) File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module> File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 12, in <module>from gunicorn import util File "d:\program files\python\python36\lib\site-packages\gunicorn\util.py", line 13, in <module>import pwd ModuleNotFoundError: No module named 'pwd'
ModuleNotFoundError: No module named 'pwd'
from os import * from pwd import * def get_username():return getpwuid(getuid())[0]
pwd.py
G:\Django\hello>gunicorn hello.wsgi Traceback (most recent call last): File "d:\program files\python\python36\lib\runpy.py", line 193, in _run_module_as_main"__main__", mod_spec) File "d:\program files\python\python36\lib\runpy.py", line 85, in _run_codeexec(code, run_globals) File "D:\Program Files\Python\Python36\Scripts\gunicorn.exe\__main__.py", line 5, in <module> File "d:\program files\python\python36\lib\site-packages\gunicorn\app\wsgiapp.py", line 10, in <module>from gunicorn.app.base import Application File "d:\program files\python\python36\lib\site-packages\gunicorn\app\base.py", line 13, in <module>from gunicorn.arbiter import Arbiter File "d:\program files\python\python36\lib\site-packages\gunicorn\arbiter.py", line 18, in <module>from gunicorn import sock, systemd, util File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 101, in <module>class UnixSocket(BaseSocket): File "d:\program files\python\python36\lib\site-packages\gunicorn\sock.py", line 103, in UnixSocket FAMILY = socket.AF_UNIX AttributeError: module 'socket' has no attribute 'AF_UNIX'
AttributeError: module 'socket' has no attribute 'AF_UNIX'
2. The pitfalls encountered by Ubuntu that comes with the win10 platform
Start the Linux system on win10: First, go to win10 settings-->Security and updates-->For developers-->Development Personnel mode-->tick, then go to Control Panel-->Program Features-->Turn Windows features on or off-->Windows Subsystem for Linux (Bata)-->tick, and finally Run shell as administrator--> Enter cmd--> Enter bash--> Follow the instructions to download and install Linux.
After the installation is completed, enter cmd with shell as administrator and enter bash to enter the Linux system. The system has python2.7 and python3.5 installed by default. The default startup of python is python2. You can set the default python to python3:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 100 sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 200
Follow the routine to install pip3 first (if you install pip directly, it will be installed in python2 by default. ): sudo apt-get install python3-pip, then use pip to install Django: sudo pip3 install Django, and finally use pip to install gunicorn: sudo pip3 install gunicorn (the automatically installed version now is 19.7.1). After the environment is installed, switch to the project address: cd /mnt/g/Django/hello, and then enter gunicorn hello.wsgi, and an error occurs again. OSError: [Errno 92] Protocol not available. Later, after uninstalling pip, I found that gunicorn depends on python-gunicorn (19.4.5), so I uninstalled gunicorn (19.7.1): pip3 uninstall gunicorn, and then installed gunicorn (19.4.5) :pip3 install gunicorn==19.4.5. Then the command gunicorn hello.wsgi started successfully, Failed to find application did not appear, and 127.0.0.1:8000/admin/ was successfully accessed.
Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi [2017-07-16 15:16:25 +0800] [428] [INFO] Starting gunicorn 19.7.1Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 44, in set_options sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) OSError: [Errno 92] Protocol not available During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/bin/gunicorn", line 11, in <module>sys.exit(run()) File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/wsgiapp.py", line 74, in run WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 203, in run super(Application, self).run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/app/base.py", line 72, in run Arbiter(self).run() File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 198, in run self.start() File "/usr/local/lib/python3.5/dist-packages/gunicorn/arbiter.py", line 157, in start self.LISTENERS = sock.create_sockets(self.cfg, self.log, fds) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 180, in create_sockets sock = sock_type(addr, conf, log) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 32, in __init__self.sock = self.set_options(sock, bound=bound) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 89, in set_optionsreturn super(TCPSocket, self).set_options(sock, bound=bound) File "/usr/local/lib/python3.5/dist-packages/gunicorn/sock.py", line 46, in set_optionsif err[0] not in (errno.ENOPROTOOPT, errno.EINVAL): TypeError: 'OSError' object is not subscriptable
OSError: [Errno 92] Protocol not available
Lee@Kein:/mnt/g/Django/hello$ gunicorn hello.wsgi [2017-07-16 15:22:16 +0800] [470] [INFO] Starting gunicorn 19.4.5[2017-07-16 15:22:16 +0800] [470] [INFO] Listening at: http://127.0.0.1:8000 (470) [2017-07-16 15:22:16 +0800] [470] [INFO] Using worker: sync [2017-07-16 15:22:17 +0800] [473] [INFO] Booting worker with pid: 473Not Found: /static/admin/css/base.css Not Found: /static/admin/css/login.css
3. Install the ubuntu system on the virtual machine and follow the above routine to successfully start the service in one go. I also thought about the problems encountered on win10 and successfully filled in pit 2.
Some other problems are:
1. Django cannot install mysqlclient in Linux. If you install PyMySQL at this time, an error will be reported when running the django project. You only need to add a sentence to a file.
In the project root directory, find __init__.py in the corresponding app directory and add this sentence:
import pymysql
pymysql.install_as_MySQLdb()
Example:
My project is eagle, under eagle there is the file manage.py, and there is also your own app, under this app there is the file view.py.
2. Note the static format in nginx. Loading using location /static/ and location /static is different. It must be consistent according to the format in html.
3. It took me a day to understand a problem. Most tutorials say to open uwsgi --ini eagle.ini to link uwsgi and django, and then open nginx. However,
If you There is no log path added to eagle.ini and it will not be run in the background. At this time, uwsgi can only be closed,
and then open nginx. When you enter the address in the browser, a 502 error is found. Check /var/ log/nginx/myweb_error.log, found the error message, congratulations on getting into the trap.
The reason is that uwsgi was shut down just now and you did not restart it. Many people on the Internet do not know this reason. . .
What a rip-off, such a messy answer. According to my method, there are two methods, one is to open nginx and then run
uwsgi --ini eagle.ini, the other is to add the log path to eagle.ini and it will automatically run in the background
The above is the detailed content of Everyone should pay attention to the landmines encountered by Django. For more information, please follow other related articles on the PHP Chinese website!