首页 > 编程知识 正文

python创建虚拟环境命令,python无法创建虚拟环境

时间:2023-05-05 03:55:52 阅读:269332 作者:763

1 进入CMD, 使用命令python -m venv创建虚拟环境 C:Usersxupeng>python -m venv d:test_venvC:Usersxupeng>

就会在d:test_venv下创建虚拟环境。

2 进入Scripts目录,并执行命令activate.bat进行激活

activate.bat其实就位于Scripts目录下:

C:Usersxupeng>cd d:test_venvC:Usersxupeng>d:d:test_venv>cd Scriptsd:test_venvScripts>activate.bat(test_venv) d:test_venvScripts>

注意上面调用activate.bat后,下一行的命令提示前出现了(test_venv)这个标识,这就表示,之后所有的命令都是对这个虚拟环境test_venv进行操作了。这也是使用activate.bat的作用。activate.bat其实就位于Scripts下。

activate.bat只需要执行一次即可。之后进入到虚拟环境下中的Scripts目录,进行的所有操作就是对这个虚拟环境进行的操作。

3 在Scripts目录下,执行命令pip list查看当前虚拟环境中的安装包 (test_venv) d:test_venvScripts>pip listDEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.pip (9.0.1)setuptools (28.8.0)You are using pip version 9.0.1, however version 21.0.1 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.

显示当前虚拟环境中只预装了pip和setuptools这两个安装包。

4 依旧在当前目录下,使用命令pip install安装包 (test_venv) d:test_venvScripts>pip install zippCollecting zipp Using cached https://files.pythonhosted.org/packages/0f/8c/715c54e9e34c0c4820f616a913a7de3337d0cd79074dd1bed4dd840f16ae/zipp-3.4.1-py3-none-any.whlInstalling collected packages: zippSuccessfully installed zipp-3.4.1You are using pip version 9.0.1, however version 21.0.1 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.

安装成功后,会在Libsite-packages目录下保存相关文件:

再次使用命令pip list查看当前已安装包:

(test_venv) d:test_venvScripts>pip listDEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.pip (9.0.1)setuptools (28.8.0)zipp (3.4.1)You are using pip version 9.0.1, however version 21.0.1 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command.

可以看到zipp确实已经安装成功了。

5 重要提醒

注意,之后的操作一定要在虚拟目录中的Scripts下进行操作,否则操作的就不是当前虚拟环境,而是全局环境了。

非Scripts目录下操作结果:

D:test_venv>pip listPackage Version-------------------- ---------Appium-Python-Client 1.1.0argon2-cffi 20.1.0attrs 20.3.0backcall 0.2.0...webencodings 0.5.1Werkzeug 1.0.1

Scripts目录下操作结果:

D:test_venvScripts>pip listDEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.pip (9.0.1)setuptools (28.8.0)zipp (3.4.1)You are using pip version 9.0.1, however version 21.0.1 is available.You should consider upgrading via the 'python -m pip install --upgrade pip' command. 6 通过Pycharm来打开该虚拟环境,查看相关数据

打开工程后,使用快捷键Ctrl+Alt+S进入设置界面:


可以看到当前并未设置Python解释器,也无任何的已安装包。单击右侧的设置按钮,单机Add,进入设置Python解释器界面:点选Existing environment,下面会自动识别出解释器位置。单机OK即可。

再次回到Python解释器界面:


即可看到,当前虚拟环境的解释器就是其自身的解释器,同时已经安装的包也和前面CMD中的输出一致了。

7 Pycharm创建虚拟环境

也可以直接通过Pycharm创建虚拟环境,可参考Python Pycharm创建虚拟环境.

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。