日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Pipenv管理Python虚拟环境

發布時間:2024/4/11 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pipenv管理Python虚拟环境 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Python 環境搭建

pipenv 安裝使用

快速入門Python 最新最流行的pipenv虛擬環境
Python Dev Workflow for
Pipenv & Virtual Environments
virtualenv-burrito

Pipenv管理Python虛擬環境

一、pipenv介紹

簡單說,pipenv就是把pip和virtualenv包裝起來的一個便攜工具,它會自動為您的項目創建和管理virtualenv,以及在安裝/卸載軟件包時添加/刪除Pipfile中的軟件包。

使用pipenv管理python環境虛擬環境的優勢:

  • 不再需要分開使用pip和virtualenv,合并到了一起
  • 不再需要管理requirements.txt,pipenv install xxx安裝包時自動生成版本號、Hash值、包依賴關系到Pipfile和Pipfile.lock,并且可以手工編輯以區分開發環境和生產環境
  • 使用Hash校驗安裝的包,有利于主動暴露安全問題
  • 使用最新版本的依賴項,以最大限度地降低過時組件帶來的安全風險
  • 可以圖形化顯示包之間的依賴關系(pipenv graph)
  • 通過加載.env文件簡化開發工作流程

二、安裝pipenv

首先,確保你已經正確安裝python3和pip3

[root@shiyanlou ~]# python3 -V Python 3.7.2 [root@shiyanlou ~]# pip3 -V pip 19.0.1 from /usr/local/python3/lib/python3.7/site-packages/pip (python 3.7) [root@shiyanlou ~]#

然后,Linux下pip3 install pipenv,Mac下brew install pipenv

[root@shiyanlou ~]# pip3 install pipenv Looking in indexes: http://mirrors.aliyun.com/pypi/simple/ Collecting pipenvDownloading http://mirrors.aliyun.com/pypi/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB)100% |████████████████████████████████| 5.2MB 4.0MB/s Requirement already satisfied: certifi in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (2018.11.29) Requirement already satisfied: setuptools>=36.2.1 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (40.6.2) Requirement already satisfied: pip>=9.0.1 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (18.1) Requirement already satisfied: virtualenv in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (16.2.0) Requirement already satisfied: virtualenv-clone>=0.2.5 in /usr/local/python3/lib/python3.7/site-packages (from pipenv) (0.4.0) Installing collected packages: pipenv Successfully installed pipenv-2018.11.26 [root@shiyanlou ~]#

pipenv依賴psutil, virtualenv-clone, pew, certifi, urllib3, chardet, requests, mccabe, pyflakes, pycodestyle, flake8等第三方模塊,pipenv有很漂亮的彩色終端界面。

三、Pipfile和Pipfile.lock

  • Pipfile, 簡明地顯示項目環境和依賴包
[[source]] name = "pypi" url = "https://mirrors.aliyun.com/pypi/simple/" # 指定國內pip源,不然會慢死人的 verify_ssl = true[dev-packages] # 開發環境[packages] # 生產環境 requests = "*" # *表示最新版本 django = "*"[requires] python_version = "3.7" # python版本 Pipfile.lock, 詳細記錄環境依賴,并且利用了Hash算法保證了它完整對應關系 {"_meta": {"hash": {"sha256": "d737a1ef2f8b591f86cd0fa6a8cb48918f742d60df2a394ebc3d23aff4748999"},"pipfile-spec": 6,"requires": {"python_version": "3.7"},"sources": [{"name": "pypi","url": "https://pypi.org/simple","verify_ssl": true}]},"default": {"certifi": {"hashes": ["sha256:47f9c83ef4c0c621eaef743f133f09fa8a74a9b75f037e8624f83bd1b6626cb7","sha256:993f830721089fef441cdfeb4b2c8c9df86f0c63239f06bd025a76a7daddb033"],"version": "==2018.11.29"},"chardet": {"hashes": ["sha256:84ab92ed1c4d4f16916e05906b6b75a6c0fb5db821cc65e70cbd64a3e2a5eaae","sha256:fc323ffcaeaed0e0a02bf4d117757b98aed530d9ed4531e3e15460124c106691"],"version": "==3.0.4"},"django": {"hashes": ["sha256:a32c22af23634e1d11425574dce756098e015a165be02e4690179889b207c7a8","sha256:d6393918da830530a9516bbbcbf7f1214c3d733738779f06b0f649f49cc698c3"],"index": "pypi","version": "==2.1.5"},"idna": {"hashes": ["sha256:c357b3f628cf53ae2c4c05627ecc484553142ca23264e593d327bcde5e9c3407","sha256:ea8b7f6188e6fa117537c3df7da9fc686d485087abf6ac197f9c46432f7e4a3c"],"version": "==2.8"},"pytz": {"hashes": ["sha256:32b0891edff07e28efe91284ed9c31e123d84bea3fd98e1f72be2508f43ef8d9","sha256:d5f05e487007e29e03409f9398d074e158d920d36eb82eaf66fb1136b0c5374c"],"version": "==2018.9"},"requests": {"hashes": ["sha256:502a824f31acdacb3a35b6690b5fbf0bc41d63a24a45c4004352b0242707598e","sha256:7bf2a778576d825600030a110f3c0e3e8edc51dfaafe1c146e39a2027784957b"],"index": "pypi","version": "==2.21.0"},"urllib3": {"hashes": ["sha256:61bf29cada3fc2fbefad4fdf059ea4bd1b4a86d2b6d15e1c7c0b582b9752fe39","sha256:de9529817c93f27c8ccbfead6985011db27bd0ddfcdb2d86f3f663385c6a9c22"],"version": "==1.24.1"}},"develop": {} }

四、虛擬環境管理

4.1 創建虛擬環境

進入項目的文件夾,直接pipenv install xxx安裝你需要的包,就會在當前文件夾下創建虛擬環境,生成Pipfile和Pipfile.lock

[root@shiyanlou ~]# cd myproject/ [root@shiyanlou myproject]# pipenv install requests Creating a virtualenv for this project… Pipfile: /root/myproject/Pipfile Using /usr/local/python3/bin/python3.7 (3.7.2) to create virtualenv… ? Creating virtual environment...Already using interpreter /usr/local/python3/bin/python3.7 Using base prefix '/usr/local/python3' New python executable in /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python3.7 Also creating executable in /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python Installing setuptools, pip, wheel... done. ? Successfully created virtual environment! Virtualenv location: /root/.local/share/virtualenvs/myproject-zKJX12a5 Creating a Pipfile for this project… Installing requests… ? Installation Succeeded Pipfile.lock not found, creating… Locking [dev-packages] dependencies… Locking [packages] dependencies… ? Success! Updated Pipfile.lock (444a6d)! Installing dependencies from Pipfile.lock (444a6d)…▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 5/500:00:04 To activate this project's virtualenv, run pipenv shell. Alternatively, run a command inside the virtualenv with pipenv run. [root@shiyanlou myproject]# ll -al total 12 drwxr-xr-x. 2 root root 39 Jan 31 14:09 . dr-xr-x---. 17 root root 4096 Jan 31 14:08 .. -rw-r--r--. 1 root root 153 Jan 31 14:09 Pipfile -rw-r--r--. 1 root root 1914 Jan 31 14:09 Pipfile.lock [root@shiyanlou myproject]#

如果需要指定python版本的話

$ pipenv --three # 泛指python的版本 $ pipenv --python 3.7 # 或者,特指某個python版本 $ pipenv --python <path/to/python> # 或者,特指某個位置的python 4.2 進入/退出/刪除虛擬環境 [root@shiyanlou myproject]# pipenv shell # 進入虛擬環境 Launching subshell in virtual environment…. /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/activate [root@shiyanlou myproject]# . /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/activate (myproject) [root@shiyanlou myproject]# exit # 退出虛擬環境 exit [root@shiyanlou myproject]# pipenv --rm # 刪除整個環境 Removing virtualenv (/root/.local/share/virtualenvs/myproject-zKJX12a5)[root@shiyanlou myproject]#

有幾點需要注意:

平時安裝和卸載包的時候不需要先進入或退出虛擬環境,直接在項目文件下pipenv install/uninstall xxx就行了
退出虛擬環境不要deactivate,請使用exit,否則下次pipenv進入的時候就會報如下錯

[root@shiyanlou myproject]# pipenv shell Shell for UNKNOWN_VIRTUAL_ENVIRONMENT already activated. No action taken to avoid nested environments. [root@shiyanlou myproject]# pipenv --rm并不會刪除生成的Pipfile和Pipfile.lock

4.3 管理開發環境

通常有一些Python包只在你的開發環境中需要,而不是在生產環境中,例如單元測試包。 Pipenv使用–dev標志區分兩個環境。也就是說一個–dev參數,幫你在同一個虛擬環境中又區分出了開發和非開發環境。

$ pipenv install --dev pytest

在虛擬環境中運行命令,使用run參數,提供要運行的命令:

$ pipenv run python manage.py runserver

這將使用當前虛擬環境關聯的Python解釋器,執行命令?;蛘吆唵蔚膱绦心_本:

$ pipenv run python your_script.py

如果你不想每次運行Python時都輸入這么多字符,可以在shell中設置一個別名,例如:

$ alias prp="pipenv run python"

凍結Pipfile,相當于將項目所使用的第三方庫列表進行打包輸出,類似于virtualenv中生成requirements.txt文件。通過更新Pipfile.lock來凍結庫名稱及其版本,以及其依賴關系的列表。需要使用lock參數:

[root@shiyanlou myproject]# pipenv lock Locking [dev-packages] dependencies… Locking [packages] dependencies… ? Success! Updated Pipfile.lock (748999)! [root@shiyanlou myproject]#

定位項目路徑、虛擬環境、Python解釋器,顯示依賴關系,卸載包

[root@shiyanlou myproject]# pipenv --where # 定位項目路徑 /root/myproject [root@shiyanlou myproject]# pipenv --venv # 定位虛擬環境 /root/.local/share/virtualenvs/myproject-zKJX12a5 [root@shiyanlou myproject]# pipenv --py # 定位Python解釋器 /root/.local/share/virtualenvs/myproject-zKJX12a5/bin/python [root@shiyanlou myproject]# pipenv graph # 顯示依賴關系 atomicwrites==1.2.1 attrs==18.2.0 Django==2.1.5- pytz [required: Any, installed: 2018.9] more-itertools==5.0.0- six [required: >=1.0.0,<2.0.0, installed: 1.12.0] py==1.7.0 requests==2.21.0- certifi [required: >=2017.4.17, installed: 2018.11.29]- chardet [required: >=3.0.2,<3.1.0, installed: 3.0.4]- idna [required: >=2.5,<2.9, installed: 2.8]- urllib3 [required: >=1.21.1,<1.25, installed: 1.24.1] [root@shiyanlou myproject]# pipenv uninstall --all # 卸載所有包 Un-installing all [dev-packages] and [packages]… Found 12 installed package(s), purging… Uninstalling Django-2.1.5:Successfully uninstalled Django-2.1.5 ...... Environment now purged and fresh! [root@shiyanlou myproject]#

4.4 有個大坑等你跳

pipenv有個缺點,lock不穩定而且時間非常長,所以安裝包的時候記得加上–skip-lock,在最后開發完成要提交到倉庫的時候再pipenv lock。希望將來的版本能夠優化。

[root@shiyanlou myproject]# pipenv install django --skip-lock Installing django… ? Installation Succeeded Installing dependencies from Pipfile…▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 2/2 — 00:00:02 [root@shiyanlou myproject]#

五、常用命令匯總

pipenv具有下列的選項:

$ pipenv Usage: pipenv [OPTIONS] COMMAND [ARGS]... Options:--where 顯示項目文件所在路徑--venv 顯示虛擬環境實際文件所在路徑--py 顯示虛擬環境Python解釋器所在路徑--envs 顯示虛擬環境的選項變量--rm 刪除虛擬環境--bare 最小化輸出--completion 完整輸出--man 顯示幫助頁面--three / --two 使用Python 3/2創建虛擬環境(注意本機已安裝的Python版本)--python TEXT 指定某個Python版本作為虛擬環境的安裝源--site-packages 附帶安裝原Python解釋器中的第三方庫--jumbotron An easter egg, effectively.--version 版本信息-h, --help 幫助信息

pipenv可使用的命令參數:

Commands:check 檢查安全漏洞graph 顯示當前依賴關系圖信息install 安裝虛擬環境或者第三方庫lock 鎖定并生成Pipfile.lock文件open 在編輯器中查看一個庫run 在虛擬環境中運行命令shell 進入虛擬環境uninstall 卸載一個庫update 卸載當前所有的包,并安裝它們的最新版本

六、在線練習

Pipenv playground是官方的在線練習網站:https://rootnroll.com/d/pipenv/,非常棒!

使用Cookiecutter安裝Django開發環境

2.自定義用戶模型
3.優化使用通用視圖類
4.在系統環境變量中保存敏感信息
5.為不同環境分別配置settings.py文件
6.測試用例,為視圖和模型編寫測試用例

總結

以上是生活随笔為你收集整理的Pipenv管理Python虚拟环境的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。