Note 41- python virtual environment virtualenv

Foreword

If you are a beginner python, I do not recommend you do python virtual environment, I see a lot of python beginner students, use the latest version of pycharm, create a new project when the default is venu virtual environment.
Then use cmd inside pip install third-party package when the import is unsuccessful in engineering which life and death, doubts began to engage in life. (You tell him this is the virtual environment venu, he will be forced to look ignorant!)

Then it will use the virtual environment under what circumstances?

If you've used python to write automated test code for a project, and then you start writing the second item, third item. . . . When you write the project more and more time,
you will find that the use of third-party packages pip python installation will be more and more, and you'll step on pit when installing third-party packages, such as different versions of python matching different pytest package, or django1 and django2 difference is so big.
This time you will find at the same time I need to install two versions of the package, compare the difference, or the development of multiple projects at the same time, it is clear that an environment can not meet our needs.

So need multiple sets of python environment, is the need to learn to use a virtual environment! virtualenv it installed on windows and linux there are some differences.

windows installation virtualenv

virtualenv is used to create a virtual library of Python environment, virtual environment capable of independent existence in the real environment, and may have a plurality of mutually independent Python virtual environment at the same time,
each virtual environment can create a clean development environment for project dependence, version control has a very important role.

Virtual environment have any significance?

Draw an analogy, you are using django2.1 develop a project, this time you see an open source django project on github, but is based on django1.8 written, you will have problems running the native environment.
Now that I can not put uninstall, reinstall a bar, it will affect the code you develop, so this problem can be solved using a virtual environment.
Use virtualenv to create a project only to run the development environment, both to ensure the normal operation of the project, also facilitates the migration project later.

Installation virtualenv general Python libraries installed with the same operation, the direct use pip command on the line

pip install virtualenv

C:\Users\dell>pip install virtualenv
Collecting virtualenv
  Downloading https://files.pythonhosted.org/packages/f7/69/1ad2d17560c4fc60170056dcd0a568b83f3453a2ac91155af746bcdb9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB)
     |████████████████████████████████| 3.3MB 364kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.4

After installation is complete check the version using virtualenv --version

C:\Users\dell>virtualenv --version
16.7.4

Create a virtual environment

After the installation is successful, we can create a virtual environment: virtualenv project_name, I want to create a virtual environment such as the name of a python in soft disk directory d is py36_yoyo

virtualenv py36_yoyo

D:\soft>virtualenv py36_yoyo
Using base prefix 'e:\\python36'
New python executable in D:\soft\py36_yoyo\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

From the top to see the log run, it is a virtual environment based on my e disk inside python36 copy version, which is before the pip python environment installed, it is this virtual environment default python environment.

Activating virtual environment

Virtual environment has been created, it is how we use it to activate? Scripts directory under the cd to py36_yoyo, which has a direct input files activate.bat activate command activates

D:\soft\py36_yoyo>cd Scripts

D:\soft\py36_yoyo\Scripts>dir
2019/08/28  23:30    <DIR>          .
2019/08/28  23:30    <DIR>          ..
2019/08/28  23:30             2,299 activate
2019/08/28  23:30               872 activate.bat
2019/08/28  23:30             1,755 activate.ps1
2019/08/28  23:30             1,151 activate.xsh
2019/08/28  23:30             1,517 activate_this.py
2019/08/28  23:30               512 deactivate.bat
2019/08/28  23:30           102,775 easy_install-3.6.exe
2019/08/28  23:30           102,775 easy_install.exe
2019/08/28  23:30           102,757 pip.exe
2019/08/28  23:30           102,757 pip3.6.exe
2019/08/28  23:30           102,757 pip3.exe
2019/08/28  23:28           100,504 python.exe
2019/08/28  23:28            52,888 python3.dll
2019/08/28  23:28         3,555,992 python36.dll
2019/08/28  23:28            98,968 pythonw.exe
2019/08/28  23:30           102,753 wheel.exe

D:\soft\py36_yoyo\Scripts>activate

(py36_yoyo) D:\soft\py36_yoyo\Scripts>

When you see the front appeared (py36_yoyo)Description Enter the virtual environment, and then they can use inside the pip install third-party packages, such as the installation environment pytest

(py36_yoyo) D:\soft\py36_yoyo\Scripts>pip install pytest==3.6.3

Whether the installation is successful then you can write a pytest test code to test a success

pytest code runs

Test_h.py create a new file, write test scripts pytest

import pytest

# ** 作者:上海-悠悠 QQ交流群:717225969**
def test_one():
    print("正在执行----test_one")
    x = "this"
    assert 'h' in x

def test_two():
    print("正在执行----test_two")
    x = "hello"
    assert x

def test_three():
    print("正在执行----test_three")
    a = "hello"
    b = "hello world"
    assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_h.py"])

operation result

(py36_yoyo) D:\soft\py36_yoyo\Scripts>pytest -s D:\soft\test_h.py
================================================= test session starts =================================================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.8.0, pluggy-0.6.0
rootdir: D:\soft, inifile:
collected 3 items

..\..\test_h.py 正在执行----test_one
.正在执行----test_two
.正在执行----test_three
.

============================================== 3 passed in 0.06 seconds ===============================================

(py36_yoyo) D:\soft\py36_yoyo\Scripts>

Use deactivate exit the virtual environment

deactivate

When a project is developed, the required dependencies can be used for a freeze mentioned export dependencies python notes 40 environmental transport freeze generation requirements.txt

linux virtual environment

There is a little difference in virtual environments and window on linux, substantially similar. The default is the linux environment python2.7, python3.6 If you want to use a virtual environment, you can put inside a Script python3.6 pip3 Set the connection.
Then use pip3 installation virtualenv

pip3 install virtualenv

[root@yoyo ~]# pip3 install virtualenv
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting virtualenv
  Downloading http://mirrors.aliyun.com/pypi/packages/f7/69/1ad2d17560c4fc60170056dcd0a568b83f3453a2ac91155af746bcdb9a07/virtualenv-16.7.4-py2.py3-none-any.whl (3.3MB)
     |████████████████████████████████| 3.3MB 2.3MB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-16.7.4
[root@yoyo ~]# virtualenv --version
-bash: virtualenv: command not found

If you use virtualenv --version view the version number is displayed -bash: virtualenv: command not found, then find virtualenv, add a soft link

[root@yoyo ~]# find / -name virtualenv
/var/lib/docker/overlay2/458b85f365c18adb23606b6011c10c3af1e03babe3a7e7d839a12ee90f3d2bc2/diff/opt/rh/rh-python36/root/usr/bin/virtualenv
/usr/local/python3/bin/virtualenv

[root@yoyo ~]# ln /usr/local/python3/bin/virtualenv /usr/bin

Then create a virtual environment, venv is the name of the virtual environment, you can take any name

virtualenv --no-site-packages venv

[root@yoyo ~]# virtualenv --no-site-packages venv
Using base prefix '/usr/local/python3'
New python executable in /root/venv/bin/python3.6
Also creating executable in /root/venv/bin/python
Please make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.
Installing setuptools, pip, wheel...
done.

To activate the virtual environment cd / venv / bin / directory, execute source activateactivate the virtual environment

[root@yoyo ~]# cd ./venv/bin/
[root@yoyo bin]# source activate
(venv) [root@yoyo bin]# 

Exit virtual environments deactivate

(venv) [root@yoyo bin]# deactivate
[root@yoyo bin]# 

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11427362.html