Article Directory
-
- Referer
- What is a virtual environment?
- Why do I need to use a virtual environment?
- When do I need to use a virtual environment?
- Why is it better to use `pipenv` in a virtual environment?
- pipenv install
- Create a virtual environment
- Install and remove third-party libraries
- Terminal activates the virtual environment
- Exit the virtual environment in Terminal
- Delete virtual environment
- Install third-party packages from the mirror source
- Don't know but commonly used
- Batch install third-party dependency packages
- Errata
Referer
What is a virtual environment?
- Known from Baidu Encyclopedia : With the patented real-time dynamic program behavior modification and simulation algorithm, directly use the local OS to simulate a virtual machine with a compatible OS with this machine (under Vista, it can simulate Vista, XP, and Windows 7) It can simulate Windows 7, Vista, XP), also known as "virtual environment"
- Function: Each environment is equivalent to a new Python environment. You can install libraries, run code, etc. in this new environment
Why do I need to use a virtual environment?
- As we all know, the power of Python lies in its compatibility, its strong community, and so on. Because there are many third-party libraries, the uneven levels cause many third-party libraries to be incompatible
- The real environment and the virtual environment are relatively related, not absolutely related, and can be created in the virtual environment.
- Isolation in the virtual environment makes it easier for us to deploy and go online
When do I need to use a virtual environment?
E.g:
- When the project depends on different versions
- When the required package conflicts with other packages (not required for this project, but exist)
- and so on
Why is the virtual environment pipenv
better?
Pipenv is a tool designed to introduce the best products in all packaging fields (strapping machine, composer, npm, goods, yarn, etc.) into the Python world. Windows is the first-class citizen in our world.
It will automatically create and manage virtualenv for your project, and Pipfile
add/remove packages from your packages when you install/uninstall packages. It also generates very important ones Pipfile.lock
for generating deterministic builds.
The main purpose of Pipenv is to provide a simple way for application users and developers to set up the working environment. Information on the differences between libraries and applications and the use setup.py
vs Pipfile
the definition of dependent difference terms , see ☤Pipfile vs setup.py .
The problems that Pipenv is trying to solve are multifaceted:
- You no longer need to use
pip
andvirtualenv
separate. They work together. - There
requirements.txt
may be problems with managing files, so Pipenv usesPipfile
andPipfile.lock
separates the abstract dependency declaration from the last tested combination. - Hash values can be seen everywhere. Safety. Automatically disclose security vulnerabilities.
- It is strongly recommended to use the latest version of the dependencies to minimize the security risks caused by outdated components.
- Give you an in-depth understanding of dependency graphs (for example).
$ pipenv graph
.env
Simplify the development workflow by loading files.
pipenv install
It is recommended to use pip3
, generally there will be a python2.x
version for Linux on mac or server . In most cases in this scenario, pip
pointing python2.x
, notpython3.x
pip3 install --upgrade pip
# 推荐使用pip来安装
pip3 install pipenv
Several other installation methods
# If you’re on MacOS, you can install Pipenv easily with Homebrew:
brew install pipenv
# Or, if you’re using Fedora 28:
sudo dnf install pipenv
# if you're using centos
sudo yum install -y pipenv
The installation is the same anyway, whether you use other package management tools or pip, you can
Create a virtual environment
# python3 环境创建
pipenv --python 3.x
pipenv --three
pipenv install
# 创建完成后,虚拟环境的pip。并不是你真实环境的pip版本,如果有需要,需要升级一下pip 的版本
python3 -m pip install --upgrade pip
It is worth mentioning that the corresponding Python version must be installed first to create a virtual environment. Maybe it's cute. For example, for example, there is only python3.7 in my computer environment, and I want to create a python2.7, which cannot be created.
The pipenv installed by Pip3 can only be used by python3
Install and remove third-party libraries
pipenv install packageName
# 安装多个包,中间以空格分隔即可
pipenv install packageName-1 packageName-2 packageNama-3
pipenv uninstall packageName
Terminal activates the virtual environment
pipenv shell
- At this time, the terminal will be displayed at the top
(xxx)
, and xxx is generally the project file name. Prove that the exit was successful- You don't need to care about the specific location of the virtual environment, just need to be in the current directory. There
Pipfile
can be
Exit the virtual environment in Terminal
Exit in the virtual environment of other packages may be used deactivate
, used in conda, and used conda deactivate
directly in pipenv
exit
# 即可退出
If you use deactivate and then use it
pipenv shell
, the terminal page will fail to enter the virtual environment. Please refer to Errata 2
Delete virtual environment
Just one command in this project directory
pipenv --rm
Install third-party packages from the mirror source
I believe you, like me, have encountered or tried your best to download the third-party package failure package is very slow, or simply timeout
cause the third-party package download failure, then let us use the domestic mirror source to install the third-party package. The speed is whoosh~
Single installation
# 以requests 为栗子
pipenv install requests --pypi-mirror https://pypi.tuna.tsinghua.edu.cn/simple/
Modify the configuration file
After using pipenv to create a virtual environment Pipfile
, the files will be generated in the project directory .
# Pipfile
[[source]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
verify_ssl = true
name = "pypi"
[packages]
requests = "*"
[dev-packages]
[requires]
python_version = "3.8"
Get package dependencies
We can use commands to clearly show the currently installed Python package versions and their dependencies. The commands are as follows:
pipenv graph
~ ProjectNote % pipenv graph
lxml4.6.2
requests2.25.1
- certifi [required: >=2017.4.17, installed: 2020.12.5]
- chardet [required: >=3.0.2,<5, installed: 4.0.0]
- idna [required: >=2.5,❤️, installed: 2.10]
- urllib3 [required: >=1.21.1,<1.27, installed: 1.26.2]
Generate Pipfile.lock
Sometimes the Pipfile.lock file may not exist or be deleted. At this time, we can use the following command to generate:
pipenv lock
The above is the basic use of pipenv, but it is not enough. Let's dive into it next
Don't know but commonly used
Usage: pipenv [OPTIONS] COMMAND [ARGS]...
Options:
--where Output project home information.
--venv Output virtualenv information.
--py Output Python interpreter information.
--envs Output Environment Variable options.
--rm Remove the virtualenv.
--bare Minimal output.
--completion Output completion (to be executed by the
shell).
--man Display manpage.
--support Output diagnostic information for use in
GitHub issues.
--site-packages / --no-site-packages
Enable site-packages for the virtualenv.
[env var: PIPENV_SITE_PACKAGES]
--python TEXT Specify which version of Python virtualenv
should use.
--three / --two Use Python 3/2 when creating virtualenv.
--clear Clears caches (pipenv, pip, and pip-tools).
[env var: PIPENV_CLEAR]
-v, --verbose Verbose mode.
--pypi-mirror TEXT Specify a PyPI mirror.
--version Show the version and exit.
-h, --help Show this message and exit.
Usage Examples:
Create a new project using Python 3.7, specifically:
$ pipenv --python 3.7
Remove project virtualenv (inferred from current directory):
$ pipenv --rm
Install all dependencies for a project (including dev):
$ pipenv install --dev
Create a lockfile containing pre-releases:
$ pipenv lock --pre
Show a graph of your installed dependencies:
$ pipenv graph
Check your installed dependencies for security vulnerabilities:
$ pipenv check
Install a local setup.py into your virtual environment/Pipfile:
$ pipenv install -e .
Use a lower-level pip command:
$ pipenv run pip freeze
Commands:
check Checks for PyUp Safety security vulnerabilities and against PEP
508 markers provided in Pipfile.
clean Uninstalls all packages not specified in Pipfile.lock.
graph Displays currently-installed dependency graph information.
install Installs provided packages and adds them to Pipfile, or (if no
packages are given), installs all packages from Pipfile.
lock Generates Pipfile.lock.
open View a given module in your editor.
run Spawns a command installed into the virtualenv.
scripts Lists scripts in current environment config.
shell Spawns a shell within the virtualenv.
sync Installs all packages specified in Pipfile.lock.
uninstall Uninstalls a provided package and removes it from Pipfile.
update Runs lock, then sync.
Use the package and run in the virtual environment in the real environment
The scenario is as follows:
Assume that there is a clean warehouse in the formal environment, with and only the initial package
There are required third-party packages in the virtual environment.
How to use third-party packages in virtual environments and run them in real environments
pipenv run python xxx.py
Generate Pipfile.lock
Sometimes the Pipfile.lock file may not exist or have been deleted. At this time, we can use the following command to generate:
pipenv lock
Batch install third-party dependency packages
When deploying, you only need to execute this command to install all dependent packages. It relies on Pipfile.lock
pipenv sync
Example: Use pipenv to connect to docker or other deployments
Don't just talk about fake handles, just don't talk about silly handles. After doing so much, let's take a case to realize the integration with the project. Here is docker deployment as a chestnut
First of all, we also use the virtual environment to develop in the project, of course, it is also recommended to do so. Yes, I am teaching you how to do things. Hehe~
The project at this time should be almost as follows, at this time two must exist Pipfile
, one is the project file (folder). deploy
, Dockerfile
For later realization
Writing Dockerfile, deploying docker without Dockerfile is not very versatile
The Dockerfile is implemented as follows and can be modified as needed
FROM python:3.8
COPY . /code
WORKDIR /code
RUN sh deploy.sh
CMD ["pipenv", "run", "python3", "testfile.py"]
Deploy.sh is as follows
In fact, the contents of deploy can be directly placed in the dockerfile, which I like. Be more clear. Ah, such a clear logic and structure, invincible~. I'm teaching you to do things again, big brother don't kill me
# 保持pip版本为最新版,及安装pipenv
python3 -m pip install --upgrade pip && pip3 install pipenv
# 创建虚拟环境
pipenv --python 3.8
# 安装环境依赖(第三方包)
pipenv update
The thing to note here is that I suggest you use it
pipenv update
, which is more secure.What, why do you not use sync?
Since you have asked sincerely, then I will tell you compassionately. Haha~
pipenv update
Equivalent to executingpipenv lock
andpipenv sync
two commandsIf you use it
pipenv sync
,Pipfile.lock
wouldn’t it be embarrassing if you don’t have it at this time
Docker build execution results are as follows
Sending build context to Docker daemon 99.33kB
Step 1/5 : FROM python:3.8
---> d1bfb3dd9268
Step 2/5 : WORKDIR /code
---> Running in 74cda17b1483
Removing intermediate container 74cda17b1483
---> ecfd46d28538
Step 3/5 : COPY . /code
---> 8a89f329a4f9
Step 4/5 : RUN pip install pipenv && sh deploy.sh
---> Running in cea95051d481
Collecting pipenv
Downloading pipenv-2020.11.15-py2.py3-none-any.whl (3.9 MB)
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.8/site-packages (from pipenv) (51.0.0)
Requirement already satisfied: pip>=18.0 in /usr/local/lib/python3.8/site-packages (from pipenv) (20.3.3)
Collecting virtualenv-clone>=0.2.5
Downloading virtualenv_clone-0.5.4-py2.py3-none-any.whl (6.6 kB)
Collecting certifi
Downloading certifi-2020.12.5-py2.py3-none-any.whl (147 kB)
Collecting virtualenv
Downloading virtualenv-20.2.2-py2.py3-none-any.whl (5.7 MB)
Collecting appdirs<2,>=1.4.3
Downloading appdirs-1.4.4-py2.py3-none-any.whl (9.6 kB)
Collecting distlib<1,>=0.3.1
Downloading distlib-0.3.1-py2.py3-none-any.whl (335 kB)
Collecting filelock<4,>=3.0.0
Downloading filelock-3.0.12-py3-none-any.whl (7.6 kB)
Collecting six<2,>=1.9.0
Downloading six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six, filelock, distlib, appdirs, virtualenv-clone, virtualenv, certifi, pipenv
Successfully installed appdirs-1.4.4 certifi-2020.12.5 distlib-0.3.1 filelock-3.0.12 pipenv-2020.11.15 six-1.15.0 virtualenv-20.2.2 virtualenv-clone-0.5.4
Creating a virtualenv for this project...
Pipfile: /code/Pipfile
Using /usr/local/bin/python3.8 (3.8.6) to create virtualenv...
⠦ Creating virtual environment...created virtual environment CPython3.8.6.final.0-64 in 1079ms
creator CPython3Posix(dest=/root/.local/share/virtualenvs/code-_Py8Si6I, clear=False, no_vcs_ignore=False, global=False)
seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/root/.local/share/virtualenv)
added seed packages: pip==20.3.1, setuptools==51.0.0, wheel==0.36.1
activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
✔ Successfully created virtual environment!
Virtualenv location: /root/.local/share/virtualenvs/code-_Py8Si6I
Installing dependencies from Pipfile.lock (d2a522)...
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
All dependencies are now up-to-date!
Removing intermediate container cea95051d481
---> 5bc79a1c17b6
Step 5/5 : CMD ["pipenv", "run", "python3", "testfile.py"]
---> Running in d86af926715c
Removing intermediate container d86af926715c
---> a64b6bc63353
Successfully built a64b6bc63353
Successfully tagged test:1
(ProjectNote) stringle-004@zhixiankeji-004s-MacBook-Pro ProjectNote % docker run test:1
<Response [200]>
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus=autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 clabg s_btn" autofocus></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=https://www.hao123.com name=tj_trhao1 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.bacom name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tlogin class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (windowocation.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');
</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> <tml>
<Element html at 0x7fbfe776d100>
Docker build notes:
-
# Pipfile文件 [requires] python_version = "3.8" # dockerfile FROM python:3.8 这两个后面跟的版本号必须一致,否则将会构建失败
-
Use
pipenv update
insteadpipenv sync
, why, I won't tell you. Just look at the above
Errata
1. The pip version is too low causing the installation to fail
I still remember that I was on the server before, no matter how I installed it, I couldn't install it, whether it was other packages or pipenv
. Later, I couldn't find any reason. until. . .
# 查看pip 版本
pip3 -V
# or
pip3 --version
The output is similar to the following
~ % pip3 -V pip 20.3.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)
If the version of pip is not version 20, you need to upgrade
# 推荐
python3 -m pip install --upgrade pip [--user]
# 或者
python3 -m pip install -U pip [--user]
# 不推荐
pip3 install --upgrade pip
pip3 install -U pip
Where [–user] is an optional parameter, it is best to add it. Normally it’s fine if you don’t add it
-m
:run library module as a script (terminates option list) Run library module as a script (terminates option list)
2. Virtual environment overload error
When using other python virtual environment tools before, use deactivate
and exit the virtual environment. But pipenv
, it's not like this
Use deactivate
, after exiting the virtual environment, use `pipenv shell, enter the virtual environment, the result shows...
Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated.
No action taken to avoid nested environments.
# 先exit 一下即可,然后使用
pipenv shell
# 即可重新进入虚拟环境
-
Need to pay attention to when creating a virtual environment
pipenv does not support nested virtual environments, the pipfile in the parent directory of this file is used by default
For example: there are files src-1, pipfile, src-2 in projectfile, if you enter src-1 and execute
pipenv install
orpipenv --python 3.8
orpipenv tree
, any one of the commands in projectfile/pipfile will be used first