How to change incorrect Django Path (django.__path__)? - python

I have pip installed django into my project and now it seems that my Django Project is confused about what Django installation should it be running.
When I run
python manage.py shell
and
import django
django.__path__
Shell returns incorrect path:
['D:\\project_2\\venv\\lib\\site-packages\\django']
Since I am working on project_1, I expect the following path:
['D:\\project_1\\venv\\lib\\site-packages\\django']
Is there a quick fix?

Are you sure you are using the right virtual environment ?
If using conda a better practice is to do environment by project,
https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html,
Because the Django path refers to the location where your environment is setup

Related

no python at "C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe" error in VsCode

I was running the Django project without any problems. Until I reinstalled Windows and then reinstalled vscode! Now that I am running the Django project, vscode shows the following error:
Error: no python at C:\Users\AccountName\AppData\Local\Programs\Python\Python38-32\python.exe
This might be occurs if the python directory still in the environment variables path list.
First remove python entry from the environment variables path if exists.
Also there will be a chance for virtual environments in your project. If the virtual environment is setup in the uninstalled python version then you will get the same error message.
So, remove the virtual environments which is created under the uninstalled python. It can be done by deleting the virtual environment folder from your system.
Edit pyvenv.cfg
home = C:\Users\UserName\AppData\Local\Programs\Python\Python38-32
include-system-site-packages = false
version = 3.8.3
When I edited this file after checking the file path on my computer, it worked for me.
Based on the information you provided, it is recommended that you check the Python environment variables.
Since the Windows system is reinstalled, the environment variables are restored to the default settings. Therefore, please add Python environment variables:
Or you can reinstall Python and check the "Add Python 3.8 to PATH" option, which will automatically add Python environment variables.
You could also refer to : Python environment variables.
It's very simple
Delete your dbsqite3 file from your project folder
Open your project in CMD
install virtual environment pip install virtaualenv
virtualenv .
\scripts\activate
python manage.py runserver
and play with your code
you shared.
This works for Pycharm, It did to me
From your projects folder, delete the 3 folders i.e. .ide,venv, and the third folder i think.. do not touch the projects or scripts you created. After wards, go to Pycharm and configure python interpreter again. This will now enable you to run yo scripts now with no prob
make sure to install virtualenv befor install python
check wheather django is installed (if your are using django framework).
pip install django

How to debug Django while using pipenv and VS Code?

I'm been using VS code and venv for my Django projects and have been able to debug using the tutorial provided by Microsoft here.
https://code.visualstudio.com/docs/python/tutorial-django
However, I have now switched to pipenv instead which I activate using the commmand pipenv shell.
When I now run using the Django debug configuration I get an error.
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 can't find any resources to guide me on this.
Do I need to change the launch.json file to make it work with pipenv?
you just need to choose an interpreter
in my case, all pipenv interpreters are located in the ~/.virtualenvs folder
here is the screenshot

how to fix "django-admin not recognized"

I just formatted my OS due some reason so installed python and django again. I tried to create the python project in the same environment as previous project but unlike that it does not recognizes the "django-admin" command please help me to solve this issue.
I tried reinstall it with pip and also created the environmental variable for django still not worked.
Screenshot:
Make sure you installed Django in the virtualenv you are currently using. Also make sure you added the django-admin.py file to your Python PATH.
If you want to start your project now and reinstall and fix the PATHs later, you can do:
<path_to_django-admin.py> startproject <project_name>
# An example
C:\Python3.7\Scripts\django-admin.py startproject myproject
Please try it on vs code powershell:
python -m django startproject Project
If You installed everything well and good you have pip and Django set, try this one, in your terminal
python -m django startproject NameofProject

Is django-admin part of python or django installation?

I am just starting out on using the django framework. One thing that bugs me is the django-admin utility. Is it part of the python installation or is it part of the django installation through pip?
The way I've been doing my projects is
Make a virtual Environment.
Create a django project in the environment through django-admin.
and then I just install the django framework using pip inside the environment.
My question arises because inside the virtual environment, I used django-admin to create the project before installing the framework.
https://docs.djangoproject.com/en/2.1/ref/django-admin/
django-admin is Django’s command-line utility for administrative
tasks.
django-admin utility comes with any Django installation. inorder to use django-admin command, the django-admin script should be on your system path if you installed Django via its setup.py utility. If it’s not on your path, you can find it in site-packages/django/bin within your Python installation.
use the link given above to find the instructions for windows as well as linux.
and to answer your question, django-admin is a part of django framework
I have ran into something similar. Where in a virtual enviroment I create for python 3 used my global python 2 installation. Try creating a new virtual environment just like before, and this time try using "python django-admin" instead of just "django-admin". A question for you, is this a Windows environment, I ask because that is the only environment I have witnessed this behavior in. I believe by using "python django-admin" it will force it to use the virtualenv's sandboxed python.

templates cannot be found when virtual environment is activated

I am facing a rather peculiar problem. I have built a Django application and only while deploying it, I created a virtual environment(I didn't use virtual environment while building and testing it locally). Now when virtual environment is activated and also when deactivated, the HTML templates cannot be found. Below is the screenshot.
Seeing this statement,
Python Executable: /home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
I went back to terminal and typed which python command. For that it shows
/home/shadowsaint/Programs/Django/zoomtail/venv/bin/python
Now when I deactivate virtual environment also the which python command gives the same thing.
But when I delete the venv file, and run which python it gives,
/usr/bin/python
And now the application runs fine, i.e., the HTML templates are being found.
I have three questions here.
When I activate venv, the python used from venv/bin is fine. But even when I deactivate virtual environment why the same python is being used?
What does python package(both the versions are same) have to do with templates being found?
Is the underlying principle here the same as that in here too - No module named PIL in heroku though it is installed
You have a misconfiguration at your Heroku environment, first check if your requirements was installed, use shell (python manage.py shell) and try import some of them to see if they throw errors... if passes ok then try to access any url from your project... if template breaks maybe the problem is in your Template_DIR at your settings
Obs.: check if your django commands like shell, migrate... works when running heroku run bash with your venv activated
Try follow some of this steps:
https://devcenter.heroku.com/articles/getting-started-with-python#introduction
http://docs.python-guide.org/en/latest/dev/virtualenvs/
Verify in project settings.py that INSTALLED_APPS list actually contains your app name.
INSTALLED_APPS = [
'your app',
'django.contrib...'
.....

Categories