首页 > 解决方案 > 我们可以为 python 创建一个 venv 并在 VSCode、Windows 10 的其他文件夹中使用该 venv 吗?

问题描述

1- 我的项目在这个文件夹中“D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2”

2-我在这个文件夹中有 app.py

3-我在 VSCode 中打开了这个文件夹。

4- 我打开 VSCode 终端并运行以下命令:D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2> pipenv 安装请求

5-所以我在这个目录中有一个 venv:“C:\Users\Acer.virtualenvs\PyCrawler2-RWWuk_HY”

6-我将 VSCode 中的 python 解释器更改为这个 venv。

7-我在我的 app.py 文件中编写了这段代码

import requests

8- 在 VSCode 终端中,我运行以下命令:

D:\darsy\Python\Learning\Code with Mosh - The Complete Python Programming Course for Beginners 2019-4\11- Popular Python Packages\code\7- Web Scraping\PyCrawler2> python app.py

9- 我收到此错误: ModuleNotFoundError: No module named 'requests' 。

为什么?我已经在我的 venv 中安装了 requests 包。

我认为它不是特定于这个包的。我安装并想要使用的任何软件包都会遇到此问题。

提前感谢您的帮助。

标签: pythonvisual-studio-codewindows-10modulenotfounderror

解决方案


In order for your app to use the packages installed in the virtual environment, the env needs to be activated.

Activate it by running:

source myenv/bin/activate # linux
python -m venv c:\path\to\myenv # windows

where myenv is replaced by the name of your virtual environment.

You can tell that an env is activated because it will show up at the beginning of each line in your terminal like this: (myenv) user@DESKTOP-001:

After that, when you run python run.py your app will automatically use the packages installed in the env.

You can also check out the virtual environment docs


推荐阅读