Django learning-the first lecture (on) virtual environment

1. Virtual environment

1.1 Why use a virtual environment

The libraries we installed before are basically installed directly into the global environment through pip, but if we need to use different libraries for different projects, and use different versions of the same library in different projects, it will lead to The environment is not compatible. Take django as an example: If you write a website with Django 1.10.x now, your leader tells you that there was an old project developed with Django 0.9 and let you maintain it, but Django 1.10 is no longer compatible with some syntax of Django 0.9. At this time, there will be a problem, how do I have both Django 1.10 and Django 0.9 environments on my computer? At this time, we can solve this problem through the virtual environment.

1.2 Types of virtual environments

1.2.1 virtualenv

  • 1. Install virtualenv

virtualenv is a software tool used to create a virtual environment, we can install it through pip or pip3

pip install virtualenv
pip3 install virtualenv

  • 2. Create a virtual environment

virtualenv [name of virtual environment]

Specify the Python interpreter
virtualenv -p C:\Python36\python.exe [virutalenv name] when creating a virtual environment

  • 3. Enter the virtual environment

After the virtual environment is created, you can enter the virtual environment, and then install some third-party packages. There are different ways to enter the virtual environment in different operating systems, generally divided into two types, the
first is Windows and the
second The kind is *nix:

Windows enters the virtual environment: Enter the Scripts folder of the virtual environment, and then execute activate.

*nix enter the virtual environment:
cd virtualenv/bin
source activate

Once you enter this virtual environment, you install and uninstall packages in this virtual environment and will not affect the outside environment.

  • 4. Exit the virtual environment

Exiting the virtual environment is very simple and can be done with a single command: deactivate.

1.2.2 virtualenvwrapper

The virtualenvwrapper software package allows us to manage virtual environments easier. No need to go to a certain directory to create a virtual environment through virtualenv, and also go to a specific directory to activate when activated.

  • 1. Install virtualenvwrapper

Linux:pip install virtualenvwrapper

Edit the .bashrc file under the home directory and add the following two lines.
export WORKON_HOME=$HOME/.virtualenvs
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh

Use source.bashrc to make it effective.

Windows:pip install virtualenvwrapper-win

  • 2. Create a virtual environment

mkvirtualenv [virutalenv name]
mkvirtualenv -p python3 [virutalenv name] # Specify Python version

  • 3. Switch to a virtual environment:

workon [virutalenv name]

    1. Exit the current virtual environment

deactivate

    1. List all virtual environments

lsvirtualenv

  • 6. Delete a virtual environment

rmvirtualenv [virutalenv name]

  • 7. Specify the Python version
    mkvirtualenv when creating a virtual environment --python==C:\Python36\python.exe [virutalenv name]

  • 8. Modify the default path of mkvirtualenv. It is
    installed by default to the Envs directory of the currently logged-in user on the C drive of the computer.

Add a parameter WORKON_HOME in My Computer -> Right Click -> Properties -> Advanced System Settings -> Environment Variables -> System Variables, and set the value of this parameter to the path you need.

1.2.3 pipenv

One, Windows install pipenv

pip install pipenv

Mac installation

brew install pipenv

Linux installation

pip install pipenv

Two, pipfile and pipfile.lock
Pipfile file content:

url # Specify the domestic pip source, otherwise downloading the library will be slow
dev-packages # Development environment
packages # Production environment
django = “*” # * means the latest version
requires # Python version

pipfile.lock, records environment dependencies in detail, and uses the Hash algorithm to ensure its complete correspondence

If you need to specify the Python version, the premise is that python2 and Python3 have been installed on the computer

pipenv --three # generally refers to the version of
Python3 pipenv --two # refers generally to the version of
Python2 pipenv --python 3.7 # Specify the Python version

Three, enter/exit/delete virtual environment

pipenv shell # Enter the virtual environment
exit # Exit the virtual environment
pipenv --rm # Deleting the entire environment will not delete the pipfile

Fourth, manage the development environment

  • 1. Install in the development environment

pipenv install --dev itchat

    1. To run commands in a virtual environment, use the run parameter

pipenv run python manage.py runserver

    1. Pipenv has a shortcoming, the lock is unstable and the time is very long, so remember to add --skip-lock when installing the package, and when the final development is completed, you need to submit it to the warehouse.

pipenv install django --skip-lock

Guess you like

Origin blog.csdn.net/scyllake/article/details/99721426