Struggling to get Django server to run in virtual environment - python

I'm new to using Django & Python and am having some difficulty trying to get my manage.py application to run in a virtual environment.
I am receiving the following errors at the top of my models.py file:
I initially thought that this was an issue with pylint not pointing at the right environment (when I switch my Python interpretter to the one outside of the virtual environment, the pylint errors disappear); however, whenever I try to run my server, I get the following error:
(.venv) tjmcerlean#LAPTOP-RKLCBQLO:/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce$ python manage.py runserverTraceback (most recent call last):
File "manage.py", line 10, in main
from django.core.management import execute_from_command_line
File "/mnt/c/Users/tjmce/Desktop/Git/CS50w/Project 2/commerce/.venv/lib/python3.8/site-packages/django/__init__.py", line 1, in <module>
from django.utils.version import get_version
ModuleNotFoundError: No module named 'django.utils'
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 12, 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?
This error appears to suggest that the virtual environment is not activated. However, I activated it using the location of the virtual environment (.venv/bin/activate) and saw that the bash terminal now displays (.venv):
Additionally, when using the (pip freeze) command, I can see that Django is installed within the virtual environment too:
I'm lost as to why the server won't run and would really appreciate any advice.
Thank you!

Calling django.__file__ in activated env reveals the real path:
python -c "import django; print(django.__file__)"
it should looks like /usr/local/lib/python3.8/site-packages/django/__init__.py
then re-check PYTHONPATH environment variable.
Maybe you've installed 2 Django in venv and system-wide but in some of them there is no db. And also remove all of *.pyc files in your working folder.

The problems occur when you don't include a path in virtualenv.
Try adding the path of python on your vscode.
Or, try to install and uninstall the Django projects.

Related

My django app is not activating after restarting my PC

When I start a new Django Project it works perfectly but after I shutdown or restart my PC it does not work. Even though I have activated my virtual environment it keeps giving me errors.
This what the error says:
Traceback (most recent call last):
File "C:\Users\SUCCESS-AKINYEMI\Desktop\studybud\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 "C:\Users\SUCCESS-AKINYEMI\Desktop\studybud\manage.py", line 22, in main()
File "C:\Users\SUCCESS-AKINYEMI\Desktop\studybud\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?
I tried it on another laptop and it worked, but it is not working on my own laptop.
make sure that python is added to the path
in windows try
windows logo + pause -> Advanced system settings -> Environment variables
if python it's not on the list add it.
for Value insert something like this
C:\Users\username\AppData\Local\Programs\Python\Python310\Scripts\
also make sure that you use the keyword py in your commands
for example :
py manage.py runserver

Django Error - Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable?

When I first ran the server everything was fine and the project was running properly. But after few days when I ran the server again, it gave me an error
Traceback (most recent call last):
File "C:\Users\Admin\Desktop\Django Project\first\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 "C:\Users\Admin\Desktop\Django Project\first\manage.py", line 22, in <module>
main()
File "C:\Users\Admin\Desktop\Django Project\first\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?
To be specific, I'm on Windows 10, using VS Code as my IDE (I've not setup any virtual environment).
Django version installed - 3.2.9
Python version installed - 3.10.0
PIP version installed - 21.2.3
Here is a screenshot of my Environment Variables
[
I don't know what the problem was. Thanks to everyone for their suggestions. I deleted the project and created a new one and it worked. I hope I won't face any such issue while working on a big project!

Django REST Framework - Django capitalized in `pip3 list`

I'm building an API using Django REST Framework with this tutorial. I'm getting a ModuleNotFoundError: No module named 'django' error when I try to test python3 manage.py. I've been going through the long list of issues it could be (reinstall django, need to add an __init__.py in the root, etc) and someone recommended checking my modules list with pip3 list.
Here's what I see:
Package Version
------------------- -------
asgiref 3.3.1
dj-database-url 0.5.0
Django 3.1.4
django-filter 2.4.0
django-heroku 0.3.1
djangorestframework 3.12.2
gunicorn 20.0.4
Is there a reason why Django's capitalized? Could that be why it isn't finding it in manage.py? If so, how do I go about fixing it?
Here's the full error message I'm getting, but it's really general. The exception is hardcoded:
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 17, 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?
While working with a large framework as Django one must always work in a virtual environment. Please check if you have your virtual environment activated. If not, then follow the steps:
cd your_working_directory
virtualenv environment_name where environment_name can be any name you want.
environment_name/Scripts/activate if on Windows
Or environment_name/bin/activate if on MacOS
Then after activating the environment you can install all the dependencies using pip. i.e pip install django

Using GnuRadio Companion under a virtual environment

I have created a virtual environment containing packages I need for some python out of tree blocks. When I activate the virtual environment and attempt to run the flowgraph from companion, it complains that the special packages I included in my virtual environment cannot be found.
Interestingly, I can successfully run from the command line the .py version of the flowgraph automatically generated by GnuRadio Companion.
How can I get this to work under Gnu Radio Companion?
As a specific example, I created a virtual environment containing the pandas package and attempted to import pandas in a no_block type custom python block originally created with gr_modtool. I did the install with the virtual environment activated. While running the flowgraph from the command line works fine, I get the following error when attempting to run the same flowgraph from GRC (which was opened from the command line with the virtual environment activated):
Traceback (most recent call last):
File "/home/my_name/devel/gr-my_oot_module/examples/my_flowgraph.py", line 35, in <module>
import my_oot_module
File "/home/my_name/devel/gnuradio3_8/lib/python3.6/dist-packages/my_oot_module/__init__.py", line 39, in <module>
from .my_noblock_block import my_noblock_block
File "/home/my_name/devel/gnuradio3_8/lib/python3.6/dist-packages/my_oot_module/my_noblock_block.py", line 25, in <module>
import pandas
ModuleNotFoundError: No module named 'pandas'
You have to realize that this is standard python under the hood. If you don't start gnuradio-companion from within your activated environment, Python can't find the modules therein. That's the whole idea of virtual environments.

ImportError with VirtualEnv

I'm trying to use VirtualEnv for a Flask app that I'm creating since everyone has recommended me to do so. After creating my virtual environment, I installed the libraries that I needed using pip while the environment was activated. I'm running into ImportError problems with this script. The code works fine when I'm not in the virtual environment.
My script:
#!/usr/bin/python
import sc2reader
...
...
When I try to run it I get this:
(flaskapp)xxxx#xxxx-VirtualBox:~/flaskapp/bin$ ./test.py
Traceback (most recent call last):
File "./test.py", line 3, in <module>
import sc2reader
ImportError: No module named sc2reader
I've tried changing the shebang to reflect my VirtualEnv path for Python, but that didn't fix anything. The library is found in my site-packages folder in my virtual environment, so I'm not sure why I'm getting the ImportError.
I've never used VirtualEnv before so I'm assuming I configured it wrong so it's not seeing my site-packages.
Try using
#!/usr/bin/env python
as the shebang.
If that does not work, try seeing what is the output of which python.

Categories

Resources