I'm a noob and almost have broken my brain trying to solve it. VSC can't find a python module, which perfectly works in a terminal (macos). I've checked 10 times, VSC uses the same interpreter.
The structure of the project is simple:
mainfolder/
projectfolder/
and projectfolder consists of:
pages/
webpages.py
__init__.py
tests/
tests.py
__init__.py
So tests.py tries to import and run webpages.py:
from pages.webpages import SignUpPage
and there's an error:
"Exception has occurred: ModuleNotFoundError
No module named 'pages'".
I've found this recommendation about PYTHONPATH:
https://code.visualstudio.com/docs/python/environments
This part: "Use of the PYTHONPATH variable".
So:
I've created .env file in the projectfolder/ directory with:
PYTHONPATH = /Users/blablabla/mainfolder/projectfolder/pages
I've created in the same projectfolder/ directory settings.json with:
{"env": {
"python.envFile": "${workspaceFolder}/.env" }}
All of the content of the .env and settings.json files I've written as it is.
And it doesn't work. I'm absolutely sure I'm doing smth wrong. In PyCharm it works, so I do appreciate the idea to work with PyCharm, but I hope there's a solution for VSC too...
CodePudding user response:
Choose the correct python interpreter from the status bar (lower right area). If the error still exists, restart VS Code.
CodePudding user response:
When vscode runs the file, the workspace is the root directory.
For example, my_pthon is my work space and I add your project in my workspace like the following picture:
I can add the path to tests.py by using the following code:
import sys
sys.path.append(".\mainfloder\projectfloder")
from pages.webpages import SignUpPage
PYTHONPATH = /Users/blablabla/mainfolder/projectfolder/pages
For the method you mentioned, it should be /projectfolder
instead of /projectfolder/pages
. You can alos refer to the document for more details about this way.