創(chuàng)建Python虛擬環(huán)境
為什么需要虛擬環(huán)境
根據(jù)實(shí)際開發(fā)需求,我們會(huì)不斷的更新或卸載項(xiàng)目中依賴的Python類庫(kù),直接對(duì)我們的Python環(huán)境操作會(huì)讓我們的開發(fā)環(huán)境和項(xiàng)目造成很多不必要的麻煩,并且當(dāng)我們同時(shí)開發(fā)多個(gè)項(xiàng)目的時(shí)候,可能每個(gè)項(xiàng)目依賴的同一個(gè)Python庫(kù)的版本還不一樣,就會(huì)造成版本沖突,管理相當(dāng)混亂。而虛擬環(huán)境獨(dú)立于真實(shí)環(huán)境存在,并且可以同時(shí)擁有多個(gè)虛擬環(huán)境,每個(gè)虛擬環(huán)境都可以安裝不同的類庫(kù)、不同版本的類庫(kù),對(duì)項(xiàng)目的依賴和版本的控制有著非常重要的作用。
Windows上的Anaconda創(chuàng)建虛擬環(huán)境
通過(guò)Windows的開始菜單,打開Anaconda Prompt(anaconda3)??梢钥吹矫钐崾痉懊娴?base)符號(hào),說(shuō)明當(dāng)前是Anaconda的base虛擬環(huán)境。
通過(guò)命令:
conda env list
可以列出當(dāng)前系統(tǒng)中擁有的虛擬環(huán)境。從響應(yīng)結(jié)果知道,當(dāng)前只有一個(gè)base環(huán)境,其目錄就是anaconda的安裝目錄。
conda 命令
Anaconda提供的conda是一個(gè)用來(lái)管理和部署應(yīng)用、環(huán)境和包的工具,通過(guò)輸入conda直接回車可以打印出所有可用的命令以及說(shuō)明信息。
(base) C:\Users\wux_labs>conda
usage: conda-script.py [-h] [-V] command ...
conda is a tool for managing and deploying applications, environments and packages.
Options:
positional arguments:
command
clean Remove unused packages and caches.
compare Compare packages between conda environments.
config Modify configuration values in .condarc. This is modeled after the git config command. Writes to the user .condarc file (C:\Users\wux_labs\.condarc) by default. Use the --show-sources flag to display all identified configuration locations on your computer.
create Create a new conda environment from a list of specified packages.
info Display information about current conda install.
init Initialize conda for shell interaction.
install Installs a list of packages into a specified conda environment.
list List installed packages in a conda environment.
package Low-level conda package utility. (EXPERIMENTAL)
remove Remove a list of packages from a specified conda environment.
rename Renames an existing environment.
run Run an executable in a conda environment.
search Search for packages and display associated information.The input is a MatchSpec, a query language for conda packages. See examples below.
uninstall Alias for conda remove.
update Updates conda packages to the latest compatible version.
upgrade Alias for conda update.
notices Retrieves latest channel notifications.
optional arguments:
-h, --help Show this help message and exit.
-V, --version Show the conda version number and exit.
conda commands available from other packages:
build
content-trust
convert
debug
develop
env
index
inspect
metapackage
pack
render
repo
server
skeleton
token
verify
(base) C:\Users\wux_labs>
常用的命令有:
- clean,清理不需要使用的包和緩存
- compare,比較兩個(gè)虛擬環(huán)境的包信息
- config,用來(lái)配置Anaconda的配置信息,默認(rèn)配置在文件
.condarc
中。 修改后的配置在用戶目錄下的.condarc
文件中,比如C:\Users\wux_labs\.condarc
- create,基于一些特定的包創(chuàng)建一個(gè)虛擬環(huán)境
- info,顯示當(dāng)前Anaconda的安裝信息
- init,初始化Anaconda的Shell配置
- install,在指定的虛擬環(huán)境中安裝一些包
- list,列出虛擬環(huán)境中已經(jīng)安裝了的包
- remove,從一個(gè)虛擬環(huán)境中移除一些包
- rename,重命名一個(gè)已存在的虛擬環(huán)境
- run,在一個(gè)虛擬環(huán)境中運(yùn)行可執(zhí)行程序
- search,搜索包并顯示相關(guān)信息
- uninstall ,
conda remove
的一個(gè)別名,從一個(gè)虛擬環(huán)境中移除一些包 - update,將Anaconda的包更新到兼容的最新版本
- upgrade,
conda update
的別名
同時(shí),從其他包中還提供了一些其他的命令。
conda env 命令
用來(lái)管理系統(tǒng)中的虛擬環(huán)境。
conda env 命令的使用方法為:
usage: conda-env-script.py [-h] {create,export,list,remove,update,config} ...
positional arguments:
{create,export,list,remove,update,config}
create Create an environment based on an environment definition file. If using an environment.yml file (the default), you can name the environment in the first line of the file with 'name:envname' or you can specify the environment name in the CLI command using the -n/--name argument. The name specified in the CLI will override the name specified in the environment.yml file. Unless you are in the directory containing the environment definition file, use -f to specify the file path of the environment definition file you want to use.
export Export a given environment
list List the Conda environments
remove Remove an environment
update Update the current environment based on environment file
config Configure a conda environment
optional arguments:
-h, --help Show this help message and exit.
常用的有:
- create 創(chuàng)建虛擬環(huán)境
- list 列出已有的虛擬環(huán)境
- remove 移出虛擬環(huán)境
要?jiǎng)?chuàng)建一個(gè)新的虛擬環(huán)境,可以直接通過(guò)命令進(jìn)行創(chuàng)建:
conda create --name env_name python=x.x
--name
可簡(jiǎn)寫為-n
即:
conda create -n env_name python=x.x
創(chuàng)建虛擬環(huán)境
通過(guò)命令創(chuàng)建一個(gè)虛擬環(huán)境。
conda create -n PythonBasic python=3.9
輸入y
確認(rèn)需要安裝的包,繼續(xù)完成虛擬環(huán)境的創(chuàng)建。
虛擬環(huán)境創(chuàng)建完成后,再次通過(guò)命令查看當(dāng)前系統(tǒng)中的虛擬環(huán)境信息,*
表示當(dāng)前激活的虛擬環(huán)境,當(dāng)前是base環(huán)境。
切換虛擬環(huán)境
虛擬環(huán)境創(chuàng)建完成后,可以通過(guò)以下命令激活虛擬環(huán)境:
conda activate PythonBasic
虛擬環(huán)境激活以后,命令提示符前面的環(huán)境符號(hào)會(huì)變成PythonBasic
。
并且通過(guò)命令可以看到當(dāng)前激活的是PythonBasic環(huán)境。
如果要退出當(dāng)前虛擬環(huán)境,可以使用命令:
conda deactivate
驗(yàn)證虛擬環(huán)境
通過(guò)命令conda list
或者pip list
可以查看當(dāng)前虛擬環(huán)境中安裝好的包。
兩者的區(qū)別在于:
- conda list,會(huì)列出當(dāng)前虛擬環(huán)境中安裝的包,以及關(guān)聯(lián)虛擬環(huán)境中安裝的包
- pip list,僅會(huì)列出當(dāng)前虛擬環(huán)境中安裝的包
使用python命令進(jìn)入Python解釋器環(huán)境,編寫代碼執(zhí)行驗(yàn)證。
print("Hello Python Basic")
Linux上的Anaconda創(chuàng)建虛擬環(huán)境
創(chuàng)建虛擬環(huán)境
在Linux系統(tǒng)上創(chuàng)建虛擬環(huán)境的命令與Windows系統(tǒng)上的命令一致。
conda create -n PythonBasic python=3.9
創(chuàng)建過(guò)程為:
(base) wux_labs@wux-labs-vm:~$ conda create -n PythonBasic python=3.9
Collecting package metadata (current_repodata.json): done
Solving environment: done
==> WARNING: A newer version of conda exists. <==
current version: 22.9.0
latest version: 23.1.0
Please update conda by running
$ conda update -n base -c defaults conda
## Package Plan ##
environment location: /home/wux_labs/anaconda3/envs/PythonBasic
added / updated specs:
- python=3.9
The following packages will be downloaded:
package | build
---------------------------|-----------------
ca-certificates-2023.01.10 | h06a4308_0 120 KB
certifi-2022.12.7 | py39h06a4308_0 150 KB
libffi-3.4.2 | h6a678d5_6 136 KB
ncurses-6.4 | h6a678d5_0 914 KB
openssl-1.1.1s | h7f8727e_0 3.6 MB
pip-22.3.1 | py39h06a4308_0 2.7 MB
python-3.9.16 | h7a1cb2a_0 25.0 MB
readline-8.2 | h5eee18b_0 357 KB
setuptools-65.6.3 | py39h06a4308_0 1.1 MB
sqlite-3.40.1 | h5082296_0 1.2 MB
tzdata-2022g | h04d1e81_0 114 KB
xz-5.2.10 | h5eee18b_1 429 KB
zlib-1.2.13 | h5eee18b_0 103 KB
------------------------------------------------------------
Total: 35.9 MB
The following NEW packages will be INSTALLED:
_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main None
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu None
ca-certificates pkgs/main/linux-64::ca-certificates-2023.01.10-h06a4308_0 None
certifi pkgs/main/linux-64::certifi-2022.12.7-py39h06a4308_0 None
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1 None
libffi pkgs/main/linux-64::libffi-3.4.2-h6a678d5_6 None
libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1 None
libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1 None
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1 None
ncurses pkgs/main/linux-64::ncurses-6.4-h6a678d5_0 None
openssl pkgs/main/linux-64::openssl-1.1.1s-h7f8727e_0 None
pip pkgs/main/linux-64::pip-22.3.1-py39h06a4308_0 None
python pkgs/main/linux-64::python-3.9.16-h7a1cb2a_0 None
readline pkgs/main/linux-64::readline-8.2-h5eee18b_0 None
setuptools pkgs/main/linux-64::setuptools-65.6.3-py39h06a4308_0 None
sqlite pkgs/main/linux-64::sqlite-3.40.1-h5082296_0 None
tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0 None
tzdata pkgs/main/noarch::tzdata-2022g-h04d1e81_0 None
wheel pkgs/main/noarch::wheel-0.37.1-pyhd3eb1b0_0 None
xz pkgs/main/linux-64::xz-5.2.10-h5eee18b_1 None
zlib pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0 None
Proceed ([y]/n)? y
Downloading and Extracting Packages
tzdata-2022g | 114 KB | ########################################################################################################### | 100%
readline-8.2 | 357 KB | ########################################################################################################### | 100%
pip-22.3.1 | 2.7 MB | ########################################################################################################### | 100%
python-3.9.16 | 25.0 MB | ########################################################################################################### | 100%
xz-5.2.10 | 429 KB | ########################################################################################################### | 100%
sqlite-3.40.1 | 1.2 MB | ########################################################################################################### | 100%
ncurses-6.4 | 914 KB | ########################################################################################################### | 100%
openssl-1.1.1s | 3.6 MB | ########################################################################################################### | 100%
libffi-3.4.2 | 136 KB | ########################################################################################################### | 100%
setuptools-65.6.3 | 1.1 MB | ########################################################################################################### | 100%
ca-certificates-2023 | 120 KB | ########################################################################################################### | 100%
zlib-1.2.13 | 103 KB | ########################################################################################################### | 100%
certifi-2022.12.7 | 150 KB | ########################################################################################################### | 100%
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
# $ conda activate PythonBasic
#
# To deactivate an active environment, use
#
# $ conda deactivate
Retrieving notices: ...working... done
(base) wux_labs@wux-labs-vm:~$
創(chuàng)建完成后,通過(guò)命令可以查看當(dāng)前虛擬環(huán)境列表。
切換虛擬環(huán)境
使用命令切換虛擬環(huán)境。
conda activate PythonBasic
驗(yàn)證虛擬環(huán)境
首先還是看看當(dāng)前虛擬環(huán)境中安裝的包。
conda list
pip list
由于操作系統(tǒng)不一樣,在Linux系統(tǒng)上預(yù)安裝的包與Windows操作系統(tǒng)上的包會(huì)有一些差異。
然后進(jìn)入Python解釋器環(huán)境,編寫代碼驗(yàn)證一下。
print("Hello Python Basic")
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-778160.html
總結(jié)
Python的虛擬環(huán)境可以起到環(huán)境隔離的作用,當(dāng)我們同時(shí)開發(fā)多個(gè)項(xiàng)目、需要使用同一個(gè)依賴庫(kù)的不同版本時(shí),虛擬環(huán)境非常有用。創(chuàng)建好虛擬環(huán)境后,就可以安裝自己需要的包,開發(fā)項(xiàng)目了。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-778160.html
到了這里,關(guān)于【數(shù)據(jù)分析:語(yǔ)言篇】Python(03)創(chuàng)建Python虛擬環(huán)境的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!