这篇文章将为大家详细讲解有关python建立venv虚拟环境的方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。


创建虚拟环境

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

$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-packagesdirtothevirtualenvironment.--symlinksTrytousesymlinksratherthancopies,whensymlinksarenotthedefaultfortheplatform.--copiesTrytousecopiesratherthansymlinks,evenwhensymlinksarethedefaultfortheplatform.--clearDeletetheenvironmentdirectoryifitalreadyexists.Ifnotspecifiedandthedirectoryexists,anerrorisraised.--upgradeUpgradetheenvironmentdirectorytousethisversionofPython,assumingPythonhasbeenupgradedin-place.--without-pipSkipsinstallingorupgradingpipinthevirtualenvironment(pipisbootstrappedbydefault)激活虚拟环境

在Posix标准平台下:

$source<venv>/bin/activate

在Windows cmd下:

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

在Windows PowerShell下:

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

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

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

$pipinstallnumpy

当前安装的numpy包与系统中的不会冲突,下面进行测试:

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

如果输出了numpy的包路径就表示一切正常。


关于python建立venv虚拟环境的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。