一、前言
預處理建議仔細看完本文章之後在進行操作,避免失誤,本程式設計客棧環境可以用於生產環境,有利於生產環境python之間的環境隔離,互相不會產生環境衝突;pyenv和pyenv-virtualenv可以完美結合使用,具體使用情況看專案具體要求;
依賴環境要求
yum install readline readline-devel readline-static -y
yum install openssl openssl-devel openssl-static -y
yum install sqlite-devel -y
yum install bzip2-devel bzip2-libs -y
yum install libffi-devel -y
二、pyenv安裝
pyenv獲取
[root@tset ~]# git clone 程式設計客棧/pyenv.git ~/.pyenv
cloning into '/root/.pyenv'...
remote: enumerating objects: 17352, done.
remote: total 17352 (delta 0), reused 0 (delta 0), pack-reused 17352
receiving objects: 100% (17352/17352), 3.37 mib | 24.00 kib/s, done.
resolving deltas: 100% (11818/11818), done.
配置py程式設計客棧env系統環境
[root@tset ~]# vim ~/.bashrc
## add by liuchao at 20190806 //新增以下內容
export pyenv_root="$home/.pyenv"
export path="$pyenv_root/bin:$path"
eval "$(pyenv init -)"
//儲存退出
[root@tset ~]# source ~/.bashrc //執行配置生效
三、pyenv使用
pyenv 安裝python版本
個人建議:自己在python官網**想要安裝的版本;首先要在pyenv目錄下建立cache資料夾,用來存放python安裝包。(可以解決**安裝慢的問題)
[root@tset ~]# mkdir ~/.pyenv/cache //建立cache資料夾,將**的python安裝包存放,
[root@tset ~]# cd ~/.pyenv/cache/
[root@tset cac ls //python安裝包字尾名必須是.tar.xz
python-2.7.16.tar.xz python-3.7.3.tar.xz
pyenv install
[root@tset cache]# cd
[root@tset ~]# pyenv install 3.7.3 -v
/tmp/python-build.20190806095432.10104 ~
/tmp/python-build.20190806095432.10104/python-3.7.3 /tmp/python-build.20190806095432.10104 ~
installing python-3.7.3...
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking for python3.7... no
checking for python3... no
checking for python... python
checking for --enable-universalsd程式設計客棧k... no
checking for --with-universal-archs... no
checking machdep... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the c compiler works... yes
checking for c compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the gnu c compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept iso c89... none needed
checking how to run the c preprocessor... gcc -e
checking for grep that handles long lines and -e... /usr/bin/grep
checking for a sed that does not truncate output... /usr/bin/sed
checking for --with-cxx-main=... no
checking for g++... no
configure:
檢查是否安裝成功
[root@tset ~]# pyenv versions
* system (set by /root/.pyenv/version)
3.7.3
更新資料庫
[root@tset ~]# pyenv rehash
pyenv切換python版本
[root@tset ~]# pyenv global 2.7.8 // 全域性
[root@tset ~]# pyenv local 2.7.8 // 當前目錄下
[root@tset ~]# pyenv shell 2.7.8 // shell版本
[root@tset ~]# pyenv shell --unset // 取消當前的shell設定的版本
#建立墊片路徑(為所有已安裝的可執行檔案建立 shims,如:~/.pyenv/versions/*/bin/*,因此,每當你增刪了 python 版本或帶有可執行檔案的包(如 pip)以後,都應該執行一次本命令)
[root@tset ~]# pyenv rehash
安裝與解除安裝
[root@tset ~]# pyenv install -l # 檢視可安裝的版本
[root@tset ~]# pyenv install 2.7.8 # 安裝python2.7.8的版本
[root@tset ~]# pyenv rehash # 安裝模組後
[root@tset ~]# pyenv pyenv uninstall 2.7.8 # 解除安裝
四、pyenv-virtualenv
安裝pyenv-virtualenv
[root@tset ~]# git clone /pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
cloning into '/root/.pyenv/plugins/pyenv-virtualenv'...
remote: enumerating objects: 2064, done.
remote: total 2064 (delta 0), reused 0 (delta 0), pack-reused 2064
receiving objects: 100% (2064/2064), 580.31 kib | 17.00 kib/s, done.
resolving deltas: 100% (1413/1413), done.
環境配置
[root@tset ~]# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
[root@tset ~]# source ~/.bash_profile
使用方法
[root@tset ~]# pyenv virtualenv 2.7.10 env-2.7.10 //建立虛擬環境
[root@tset ~]# pyenv activate env-2.7.10 //啟用虛擬環境
[root@tset ~]# pyenv deactivate //退出虛擬環境
[root@tset ~]# pyenv uninstall env-2.7.10 //刪除虛擬環境
[root@tset ~]# rm -rf ~/.pyenv/versions/env-2.7.10 //刪除真實目錄
總結本文標題: python虛擬環境完美部署教程
本文位址:
Python虛擬環境部署
虛擬環境用於隔離python專案環境,為每個專案配置各自的執行環境。伺服器或本地開發環境均可配置多個虛擬環境 數量限制未親測 sudo virtualenv pyvenv python2.x 虛擬環境py2venv 其中 p為指定python直譯器路徑 mkdir py2prj 其中x為指定的版本號...
Python環境 虛擬環境怎麼配置(部署)
坦然,灑脫,就像名字一樣。以前在學校,我把python當草稿在寫。現在到單位上,確實需要把它作為project 專案 來對待了。託管也好,程式部署也好,自然需要規範一些。跟著it高手學,也進步了很多。部署python環境和程式,怎麼做?現在實施起來,很不現實啦。所以還是了解下怎麼標準化 正規化部署程...
Poetry虛擬環境部署
官方文件在此,poetry目前來說是取代pipenv的乙個工具,後者已經幾乎不維護了,被拋棄也是正常的。安裝過程不再贅述了,看官方文件就行了,記錄一下簡單的使用操作。會生存乙個toml檔案,這個檔案很關鍵,將會根據它來部署環境。定位到你的專案資料夾,執行 poetry init會有很多配置讓你輸入,...