pythonvisual-studio-codewindows-10modulenotfounderror

Can we create a venv for python and use that venv in other folders in VSCode, Windows 10?


1- My project is in this folder "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- I have app.py in this folder

3- I opened this folder in VSCode.

4- I opened VSCode terminal and run this command: 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 install requests

5- so I have a venv in this directory: "C:\Users\Acer.virtualenvs\PyCrawler2-RWWuk_HY"

6- I changed my python interpreter in VSCode to this venv.

7- I write this code in my app.py file

import requests

8- in VSCode terminal, I run this command:

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- I got this error: ModuleNotFoundError: No module named 'requests' .

why? I have installed requests package in my venv.

I think it's not specific to this package. any package I install and want to use I get this issue.

Thanks for your helps in advance.


Solution

  • Please select the virtual environment you created in the lower left corner of VS Code, and then use the shortcut key Ctrl+Shift+` to open a new VS Code terminal, it will automatically enter the selected environment:

    Then click the run button in the upper right corner of Vs code:

    enter image description here

    In addition, it is recommended that you use the green run button provided by the "Python" extension to run the code in 'Terminal'.