VSC可以找到一个Python模块,该模块完美地在终端和Pycharm中工作
我是个菜鸟,几乎打破了我的大脑试图解决它。 VSC找不到Python模块,该模块在终端(MACOS)中完美工作。我已经检查了10次,VSC使用了相同的解释器。
项目的结构很简单:
mainfolder/
projectfolder/
projectFolder包含:
pages/
webpages.py
__init__.py
tests/
tests.py
__init__.py
so tests.py尝试导入和运行WebPages.py:
from pages.webpages import SignUpPage
并且有一个错误:
"Exception has occurred: ModuleNotFoundError
No module named 'pages'".
我找到了有关PythonPath的建议:
https://code.visualstudio.com/docs/python/environments
This part: "Use of the PYTHONPATH variable".
SO:SO:
我在此中创建了.env文件projectFolder/ Directory with:
PYTHONPATH = /Users/blablabla/mainfolder/projectfolder/pages
我在同一ProjectFolder/ Directory settings.json中创建了:
{"env": {
"python.envFile": "${workspaceFolder}/.env" }}
.env and settings.json文件的所有内容。
而且它行不通。我绝对确定我做错了。在Pycharm中它有效,所以我很感谢与Pycharm合作的想法,但我希望VSC也有一个解决方案...
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从状态栏(右下区域)中选择正确的Python解释器。如果错误仍然存在,请重新启动与代码。
Choose the correct python interpreter from the status bar (lower right area). If the error still exists, restart VS Code.
当Vscode运行文件时,工作区是根目录。
例如,my_pthon是我的工作空间,我在工作区中添加您的项目,如以下图片:
我可以通过使用以下代码添加tests.py的路径:
对于您提到的方法,它应该是
/projectFolder
,而不是/projectfolder/pages
。您可以参考 document for有关这种方式的更多详细信息。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:
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.