溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python中怎么部署虛擬環境

發布時間:2021-06-17 16:51:38 來源:億速云 閱讀:180 作者:Leah 欄目:開發技術

今天就跟大家聊聊有關python中怎么部署虛擬環境,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。

一、前言

預處理

建議仔細看完本文章之后在進行操作,避免失誤,本環境可以用于生產環境,有利于生產環境python之間的環境隔離,互相不會產生環境沖突;pyenvpyenv-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 https://github.com/yyuu/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.

配置pyenv系統環境

[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 cache]# 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-universalsdk... 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=<compiler>... 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 https://github.com/yyuu/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中怎么部署虛擬環境有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女