当前位置: 首页 > 图灵资讯 > 行业资讯> python如何建立venv虚拟环境

python如何建立venv虚拟环境

来源:图灵python
时间: 2025-02-10 13:33:17

创建虚拟环境

在当前目录中创建虚拟环境:

$python-mvenv.1

下面是”venv详细使用参数:

usage:venv[-h][--system-site-packages][--symlinks][--clear]
[--upgrade][--without-pip]ENV_DIR[ENV_DIR...]

CreatesvirtualPythonenvironmentsinoneormoretargetdirectories.

positionalarguments:
ENV_DIRAdirectorytocreatetheenvironmentin.

optionalarguments:
-h,--helpshowthishelpmessageandexit
--system-site-packagesGiveaccesstotheglobalsite-packagesdirtothe
virtualenvironment.
--symlinksTrytousesymlinksratherthancopies,whensymlinks
arenotthedefaultfortheplatform.
--copiesTrytousecopiesratherthansymlinks,evenwhen
symlinksarethedefaultfortheplatform.
--clearDeletetheenvironmentdirectoryifitalreadyexists.
Ifnotspecifiedandthedirectoryexists,anerroris
raised.
--upgradeUpgradetheenvironmentdirectorytousethisversion
ofPython,assumingPythonhasbeenupgradedin-place.
--without-pipSkipsinstallingorupgradingpipinthevirtual
environment(pipisbootstrappedbydefault)
激活虚拟环境

在Posix标准平台下:

$source<venv>/bin/activate

Windows cmd下:

C:><venv>/Scripts/activate.bat

Windows Powershel:

PSC:><venv>/Scripts/Activate.ps1
测试虚拟环境

激活虚拟环境后,命令行会提示当前虚拟环境的名称,表示激活成功。

在当前虚拟环境中安装numpy:

$pipinstallnumpy

目前安装的numpy包不会与系统发生冲突,以下测试如下:

$python
>>>importnumpy
>>>print(numpy)

如果输出numpy的包路径,说明一切正常。

参考官方文件

https://docs.python.org/3/library/venv.html


更多Python文章,请关注Python自学网。