VSC可以找到一个Python模块,该模块完美地在终端和Pycharm中工作

发布于 2025-02-02 02:47:39 字数 1044 浏览 4 评论 0原文

我是个菜鸟,几乎打破了我的大脑试图解决它。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

大姐,你呐 2025-02-09 02:47:39

从状态栏(右下区域)中选择正确的Python解释器。如果错误仍然存​​在,请重新启动与代码。

Choose the correct python interpreter from the status bar (lower right area). If the error still exists, restart VS Code.

薯片软お妹 2025-02-09 02:47:39

当Vscode运行文件时,工作区是根目录。

例如,my_pthon是我的工作空间,我在工作区中添加您的项目,如以下图片:

“在此处输入图像描述”

我可以通过使用以下代码添加tests.py的路径:

import sys
sys.path.append(".\mainfloder\projectfloder")
from pages.webpages import SignUpPage

pythonpath =/users/blablabla/mainfolder/projectFolder/pages

对于您提到的方法,它应该是/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:

enter image description here

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文