When activating virtualenv still global dependencies are installed - django

I am having issues with virtualenv installations on a mac.
First change to the directory and activate virtualenv
cd my-project/
virtualenv venv
source venv/bin/activate
Second...my terminal changes to the virtualenv and install Django version 3.1.7
(venv) andrescamino#Robertos-MacBook-Pro WJKTM % pip install Django==3.1.7
To make sure the installation is on the virtualenv i make a pip freeze and these are the results
(venv) andrescamino#Robertos-MacBook-Pro WJKTM % pip freeze
asgiref==3.3.1
Django==3.1.7
pytz==2021.1
sqlparse==0.4.1
Then I start the project
(venv) andrescamino#Robertos-MacBook-Pro WJKTM % django-admin startproject bandsite
However when I go to the editor and check the settings file...it still shows the version installed globally which is the 3.1.2
Generated by 'django-admin startproject' using Django 3.1.2.
Am i missing something?

That's because vscode is using your global python installation. You can see and change which python environement vscode uses from the status bar in the bottom.

Related

Django Error on django-admin startproject

I try this command:
$ virtualenv .venv
$ sourse .venv/bin/activate
(.venv) $ pip3 install django
The last command installed the django3 on the whole system while the virtual machine was active and should only be installed on the virtual machine. why??
I tried django-admin stratproject mysite, but I received this error:
Command 'django-admin' not found, but can be installed with:
sudo apt install python-django-common
so, I try sudo apt install python-django-common. Then again try django-admin stratproject mysite and resived error: Cannot find installed version of python-django or python3-django.
how I can solve this problem??
Did you add a Python path. If you have, you can enter the Python terminal when you write Python. You can see the installed libraries by typing pip freeze.
The Django is not added in your python path , if you still want to run your code go to python directory and then run the django-admin command.
Go to the folder where your python is installed and open terminal there
after that run your command
django-admin startproject test
Try using pipinstall django instead of pip3 install django in virtualenv.
By running virtualenv .venv you have created local python environment with current default system version of python (i.e. on older distributions this might be python 2.7)
While activating virtual environment, it replaces some ENV variables to allow python to address this local environment, but not system one.
pip is one of them. pip inside virtualenv points to virtualenv's pip. Virtualenv might not redefine pip3 command - this is only needed system-wide, cause you may have many different versions of python in the system at the same time. And as far as virtualenv has only one version of python / packages inside - where is no need for it to redefine pip3.
Running django-admin startproject test inside virtualenv uses virtualenv's python packages, and if there is none, it may actually use system-wide packages, but it depends on the options virtualenv was created with (use system pacakges), PATH variables etc.
Also, try not to install or rely on system-wide packages - use virtuelnv.
Using virtualenv is good, and there is a lot of helpers to ease - pipenv, virtualenwrapper etc
Just run this in the command line.
apt-get install python3-django
Hope this will solve your problem.

Django 2.1.1 Import Errors

i use vscode on windows10. I just created a Django project+app like this:
In the vscode powershell terminal:
python -m venv venv #createing a virtual environment called venv
.\scripts\activate #activate the virtual environment
pip install django==2.1.1 #install the newest version of Django
python -m pip install --upgrade pip #upgrading pip
pip install requests #install requests for api-requests
django-admin startproject api_order . #creates a django project called api_order
python manage.py migrate #create database
python manage.py runserver #starting the server
because of the server running in my powershell terminal i take a second powershell terminal and continue with activating the venv and creating a app.
.\scripts\activate
python manage.py startapp api_order_app #creating a app called api_order_app
When i now open the models.py python is reporting an problem "E0401:Unable to import 'django.db'" Same for all other imports in every other script.
What did go wrong?
I did this twice always the same problem.
This is how it looks like in the IDE
<img src="https://i.stack.imgur.com/JTmvb.jpg">
Thanks in Advance for helping me out.
Ben
I figured it out... I wasnt on the right virtualenvironment. But Django was only installed at the venv.
In Visual Studio Code you can choose the environment u want to use in the bottom left corner.
Now everything works just fine.

How to create a django project in ubuntu

I'm following a tutorial on Django and I'm suppose to create a folder in Ubuntu in terminal
$ django-admin startproject mysite
This above line shows
"Cannot find installed version of python-django or python3-django."
After installing all the required stuffs.
The recommended way of starting a django project in Ubuntu is using a virtualenv so first install it.
Then run
virtualenv -p python3 env
This will create an env named folder. Active the virtualenv by running
source ./env/bin/activate
Then install django with pip. now django will be installed in activated virtualenv
pip install django
and then create your project
django-admin startproject mysite

Django not installed in venv?

Would anyone know possible reasons why Django is being installed in the global site package and not my venv's site package folder?
Here's my set up and what I did, this is a bit detailed since I'm new to Python/Django and not sure which information is important:
Python 3.3 is installed in c:\python33
I have virtualenv, pip, easy_install installed in C:\Python33\Scripts.
My venv is c:\users\username\projects\projB
This venv was created using pyvenv, not virtualenv.
I activated the venv.
I changed directory to C:\Python33\Scripts to run "pip install django".
Django was created inside C:\Python33\Lib\site-packages and not inside C:\users\username\projects\projB\Lib\site-packages.
Do I need to install pip inside my venv and use that to install Django?
You can specify in your virutalenv wich python version you whant:
$ virtualenv -p <PATH TO PYTHON VERSION> my_virtualenv
Then:
$ source my_virtualenv/bin/activate
$ pip install Django==1.5.2
This will install the good version of django in your virtualenv according to your python version.
Thanks to virtualanv, you will be able to save/freeze and install your environement on another machine:
$ pip freeze > requirement.txt
$ pip install -r requirement.txt
You will see in the requirement.txt file the django dependency.
Pip should be installed when you create the virtual environment. Don't change directory into C:\Python33\Scripts before running pip. It looks like that means you use the base install's pip instead of your virtual environment's pip.
You should be able to run pip from any other directory. However I'm not familiar with python on Windows, so I'm not certain that pip is added to the path when you activate the environment. If that doesn't work, you'll have to change directory into the bin directory of your virtual environment, then run pip.
What happened to me was that I was trying to install django from outside the environment directory/folder.
So make sure you are inside the environment directory and then use pip install django

How to install image processing just for a virtualenv project with pip for django

But I'm looking to install freetype, libjpeg, PIL build to add image processing to my django projects I've followed this installation http://dakrauth.com/blog/entry/python-and-django-setup-mac-os-x-leopard/ which installs it site wide but I can get it inside my virtualenv project.
Do I just cd into the working directory of the virtualenv (project) and install it there and will it just be available for that project or do I use pip? I couldn't find the packages in the pip repository. Can someone enlighten me please.
curl -O http://pypi.python.org/packages/source/d/distribute/distribute-0.6.21.tar.gz
tar -xzvf distribute-0.6.21.tar.gz
cd distribute-0.6.21
python distribute_setup.py
easy_install pip
pip install virtualenv
virtualenv --distribute --no-site-packages [myproject]
cd [myproject]
source bin/activate (this activates the sandbox that virtualenv created)
pip install django mysql-python
Go to the working directory of the virtualenv and then run
$ source bin/activate
This will set that virtual environment as your active one. So now that it's active, you can install what you want, either manually (by following those steps on the site you linked to) or with pip and it will automatically install it into your active virtualenv.
If you then, say, run python manage.py runserver while the same virtualenv is active, django will have access to your newly installed package. Once you want to unset that virtual environment as your active one, simply do deactivate.
I ran into something similar; what I did was install it into the default directory (e.g. Python27/Lib/site-packages) then cut and paste all the new files put there into the created environment's site-packages. Hacky, but works.
After that you can follow EEVIAC's instructions to actually get your server running.