简体   繁体   中英

django, python, postgres and web deployment

This error has been haunting me. I have actually developed fixes and I document it and I successfully run the server on the below IP address. I try to reproduce my success after reinstalling the Ubuntu OS... with the same errors the same fixes won't do it anymore... now I need outside help. I have combed the internet for solutions. I try to add django to the PYTHONPATH and it IS added but when I exit the python interpreter the appended django path disappears!

Another item of detail that may help is that my website www.doman.com/admin is directed to the django admin login screen. I tried to log in using username and password but it fails which makes me think django is not working properly.

$ sudo python3 manage.py runserver 192.165.4.3:80

I get the error:

Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    ) from exc
ImportError: Couldn't import Django. Are you sure it's installed and
available on your PYTHONPATH environment variable? Did you forget to
activate a virtual environment?

Django is installed within my environment:

$ django-admin --version
2.0.6

Python path attempt:

$ PYTHONPATH=/home/kap/dev_env/lib/python3.6/site-packages/django python
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> sys.path
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined
>>> import sys
>>> sys.path
['', '/home/kap/dev_env/lib/python3.6/site-packages/django', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/kap/dev_env/lib/python3.6/site-packages']
>>> quit()

Immediately confirm django is added but it disappeared:

$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/home/kap/dev_env/lib/python3.6/site-packages']

sudo bypasses your virtual-environment it runs as a root user and django is not installed in root account. That is why it is throwing error .you can run without sudo or you can install django in your root account by

sudo python3 -m pip install django

Try this:

sudo python3 -m pip install django

Or if you have a requirements.txt

sudo python3 -m pip install -r requirements.txt

It is better to manage port 80 by nginx.

I actually solved it before anyone posted:

`pip install django`     

is what I used. More specifically I used 'pip install django psycopg2 and gunicorn' since installing all three together seemed to fix another issue. I was using pip3 instead of pip. There are so many 'sensitivities' so that the order of installation, specific flags and package versions of installations can break or make the whole thing. I'm thinking of making a tutorial of the whole thing and putting a video online. Maybe I can set a link on this page.

Consider pipenv - git repo is here , with more information. Better control of packages, ease of deployment, and better control of virtual environments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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