Hello i have a django application which runs smoothly with python3 manage.py runserver. But to run it all time i need to use any process manager or manually create a service. I want to run it with pm2.
But problem occurs when i try to run the app with pm2 as a process. Below is my pm2 config file.
{
apps:
[{
name: "lab",
script: "/home/ubuntu/sample-django-app/manage.py",
args: ["runserver", "0.0.0.0:8080"],
exec_mode: "fork",
instances: "1",
wait_ready: true,
autorestart: true,
max_restarts: 5,
interpreter : "/home/ubuntu/venv/bin/python3"
}]
}
But the following error occurs
raise ImportError(
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?
Traceback (most recent call last):
File "/home/ubuntu/sample-django-app/manage.py", line 11, in main
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 "/home/ubuntu/sample-django-app/manage.py", line 22, in <module>
main()
File "/home/ubuntu/sample-django-app/manage.py", line 13, in main
raise ImportError(
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?
here I have installed Django and the app runs smoothly with
**`python3 manage.py runserver`** Any help with this? Is my config file okay or not?
Related
I don't know why but I can't run any comands in the vSCode Terminal.
For example, when I try python manage.py createsuperuser I get an error that can't find Python. So I do py manage.py createsuperuser and then I get a syntax error saying that manage.py doesn't exist or something like that.
Anyone know why this is and how I could fix it?
UPDATE:
I currently have the following error:
Traceback (most recent call last):
File "C:\XXXXX\argon-dashboard-django\manage.py", line 13, in main
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 "C:\XXXXX\argon-dashboard-django\manage.py", line 23, in <module>
main()
File "C:\XXXXX\argon-dashboard-django\manage.py", line 15, in main
raise ImportError(
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?
Here's my manage.py
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
So there seems to be an import error somewhere. And I tried it with my venv activated:
(env) C:\Users\valen\OneDrive\Bureau\DOCARET\Autres\argon-dashboard-django>py manage.py createsuperuser
Traceback (most recent call last):
File "C:\Users\valen\OneDrive\Bureau\DOCARET\Autres\argon-dashboard-django\manage.py", line 13, in main
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 "C:\Users\valen\OneDrive\Bureau\DOCARET\Autres\argon-dashboard-django\manage.py", line 23, in <module>
main()
File "C:\Users\valen\OneDrive\Bureau\DOCARET\Autres\argon-dashboard-django\manage.py", line 15, in main
raise ImportError(
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?
When I got the same issue in my windows machine..(probably occurred because I did some C drive repair)
I also can't able to run python manage.py runserver , I got the same error
ModuleNotFoundError: No module named 'Django'
I did the following steps.. to overcome my error:
On my main folder terminal Check If you have already a "requiremnts.txt" file skip this step else run this command: pip freeze > requirements.txt ,if it saves all your packages then only do other steps
Delete Venv folder from the main directory.
Again install virtual environments and activate Venv.
then run this command pip install -r requirements.txt this will install all your packages back.
Run again python manage.py runserver , It will work fine
apologies if this question has been asked a ton, I'm sure it has, and I've been trying for days to figure this out, but I can't wrap my head around it. I've been developing in Python for over 1.5 years, so I'm not a complete beginner.
I can't get Django to work in my command prompt. An example is, when I try to use py .\manage.py makemigrations this is what I get:
Traceback (most recent call last):
File ".\manage.py", line 11, in main
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 22, in <module>
main()
File ".\manage.py", line 13, in main
raise ImportError(
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?
Now, I can see what the error says. The thing is my virtual environment is activated and it has Django installed, as seen here. Moreover, even if I executed outside my venv, it still doesn't work, even though I have all necessary Python directories in my PATH, as seen here.
I have verified that django-admin is installed on both my venv and my system, as well as Django itself, since I can import it on my Python Console. The version is 3.1.7. I have also tried reinstalling Django and it didn't work.
Another error I get, when I attempt to run Django console through PyCharm is this:
Traceback (most recent call last):
File "<input>", line 6, in <module>
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\django\__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 82, in __getattr__
self._setup(name)
File "C:\Users\user\AppData\Roaming\Python\Python39\site-packages\django\conf\__init__.py", line 63, in _setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting LOGGING_CONFIG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I hope this helps you come up with any idea. Could anyone please share any insight on why I'm getting this? I would greatly appreciate it.
Edit: I have installed Django through PIP:
py -m pip install django
py -m pip install djangorestframework
py -m pip install django-admin
Also, Django is imported correctly from the Python Console within the venv:
>>> import django
>>> django
<module 'django' from 'C:\\Users\\user\\AppData\\Roaming\\Python\\Python39\\site-packages\\django\\__init__.py'>
in cmd to create a virtualenv
python -m venv virtualenv_name
to enable virtualenv
virtualenv_name\scripts\activate.bat
then
pip install django
and to create a project
django-admin startproject projectname
to run the project
cd projectname
python manage.py runserver
While I was deployed my django app in heroku, I run:
heroku run python3 manage.py migrate
and I get this messahe in my console:
File "manage.py", line 16
) from exc
^
SyntaxError: invalid syntax
my manage.py file is actually:
#!~/.virtualenvs/djangodev/bin/python
"""Django's command-line utility for administrative tasks."""
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myApp3.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise 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?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
When I run python => python3, the next message is displayed:
Traceback (most recent call last):
File "manage.py", line 10, in main
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 21, in <module>
main()
File "manage.py", line 16, in main
) 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?
You need to set up a virtual enviorment first. Then install django in the virtual environment. You can do this through a thing called pipenv. you need to add this to your project by doing. python install pipenv. Then install python in that virtual enviorment by doing pipenv install django. when django installs you can then do your migrations.
I've got a properly activated venv in zsh, because I can see (venv) at the beginning of the prompt, but when I try to run Django with the command python manage.py runserver I've 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'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
import django
ModuleNotFoundError: No module named 'django'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 17, in <module>
"Couldn't import Django. Are you sure it's installed and "
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?
If I run which which python I can see python: aliased to /usr/local/bin/python3.7 that it seems wrong, because I expect something like /Users/myuser/Python-Projects/test/venv/bin/python. In fact if I disable the venv the result of which python is the same.
I tried to the same steps with bash and everything works fine, so I guess I've got something wrong with zsh, but I don't know what.
As a workaround to run Django I must run the command venv/bin/python manage.py runserver, so it's like the root python in some way is broken. I tried to update zsh to latest version, but problem is still there.
Any ideas?
Problem found. It was an alias for python in .zshrc.
I just went to run my Django development server in the Django project that I'm working on and I got this...
(baseballwebenv) C:\DjangoProjects\BaseballWebsite\baseballweb>python manage.py runserver
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "C:\Users\Nick\Anaconda3\envs\baseballwebenv\lib\site-packages\django\__init__.py", line 1, in <module>
from django.utils.version import get_version
File "C:\Users\Nick\Anaconda3\envs\baseballwebenv\lib\site-packages\django\utils\version.py", line 4, in <module>
import subprocess
File "c:\users\nick\anaconda3\Lib\subprocess.py", line 178, in <module>
from _winapi import (CREATE_NEW_CONSOLE, CREATE_NEW_PROCESS_GROUP,
ImportError: cannot import name 'ABOVE_NORMAL_PRIORITY_CLASS'
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?
As you can see I am by my runserver command, I am in my virtual environment. I recently upgraded all of my Anaconda packages, thinking everything was protected in the virtual environment. I would suspect this problem is related to that, but I'm not sure. How do I get my project up and running again? If someone wants to see certain files, I can add those as well.