I have been through numerous similar questions and I still don't understand how are we activating virtual environment for Django projects. Please mention explanation for how each command works and one more questions is that why do we not need to install python in Django's virtual environment, I am getting confused. Thank you in advance, please help this newbie.
CodePudding user response:
Benefits
- You can use any version of python you want for a specific environment without having to worry about collisions (shoutout to my python 2.7 mac users!)
- You can organize your packages much better and know exactly the packages you need to run your code incase someone else needs to run it on their machine
- Your main python package directory does not get FLOODED with unnecessary python packages
To create virtual environment
step 1 install environment package (virtualenv) using pip
pip install virtualenv
step 2 create virtualenv
virtualenv env_name #<- env_name is virtualenv name you can set any
step 3 Activate Virtual env
env_name\Scripts\activate #<- for window
step 4 Install pakages you wan to install in virtual env
cmd(env_name): pip install django
Note that python is install in your virtual env automaticaty the version is same as in your local machine
CodePudding user response:
There is no difference between activating virtual environment for Django or for other purposes. Django on it's own does not differ from any Python library out there.
Python virtual environment allows you to separate your system Python and it's libraries and create self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.
On Linux, assuming you already have Python 3 and pip3 installed:
# install virtualenv package (skip if you have it already)
pip3 install virtualenv
# create virtual environment in directory "tutorial-env"
python3 -m venv tutorial-env
# activate virtual environment
source tutorial-env/bin/activate
Upon activation below command should give path to new Python binary:
which python3
Similarly with pip3
which pip3
As long as your environment is activate you can run pip3 install $package_name
and it will install it inside virtual environment.
To deactivate your virtual environment:
deactivate
For more info and commands for Windows: https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/ https://docs.python.org/3/tutorial/venv.html