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

python如何建立venv虚拟环境

发布时间: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自学网。

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12