无法在 VSCode 中导入内部模块
Cannot Import internal modules in VSCode
我创建了遵循如下结构的内部模块-
project
└── src
└── rogers
└──__init__.py
└── cd
├── __init__.py
├── preprocessor
└── dataOps
└── __init__.py
└── verint.py
我得到的错误-
Traceback (most recent call last):
File "main_scripts/PreprocessingStep/verint_preprocessing.py", line 11, in <module>
from rogers.cd.dataOPs import Verint
ImportError: No module named rogers.cd.dataOPs
dataops中的init.py是这样的
from rogers.cd.dataOPs.boldchat import *
from rogers.cd.dataOPs.verint import *
这是我的 setup.py 的样子(没有显示整个文件,只显示其中的一部分)
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
install_requires = external_packages,
package_dir={"": "src"},
packages= setuptools.find_namespace_packages(where="src"),
package_data={'': ['*.yaml']},
include_package_data=True,
python_requires=">=3.7",
我最近从 PyCharm 迁移到 VSCode。该代码 运行 在 PyCharm 中很好,但不适用于 VSCode。
setup.py在VSCode中不起作用,可以通过(terminal.integrated.env.*) and/or within an .env file
:
修改VSCode中的PYTHONPATH
The PYTHONPATH environment variable specifies additional locations
where the Python interpreter should look for modules. In VS Code,
PYTHONPATH can be set through the terminal settings
(terminal.integrated.env.*) and/or within an .env file.
When the terminal settings are used, PYTHONPATH affects any tools that
are run within the terminal by a user, as well as any action the
extension performs for a user that is routed through the terminal such
as debugging. However, in this case when the extension is performing
an action that isn't routed through the terminal, such as the use of a
linter or formatter, then this setting will not have an effect on
module look-up.
When PYTHONPATH is set using an .env file, it will affect anything the
extension does on your behalf and actions performed by the debugger,
but it will not affect tools run in the terminal.
If needed, you can set PYTHONPATH using both methods.
An example of when to use PYTHONPATH would be if you have source code
in a src folder and tests in a tests folder. When running tests,
however, those tests can't normally access modules in src unless you
hard-code relative paths.
To solve this problem, you could add the path to src to PYTHONPATH by
creating an .env file within your VS Code workspace.
PYTHONPATH=src
首先,在 rogers
文件夹中添加 __init__.py
文件。
然后,您需要将您的项目目录路径添加到sys.path
或$PYTHONPATH
。那么只有python可以识别包并导入。这样肯定能解决问题。
我创建了遵循如下结构的内部模块-
project
└── src
└── rogers
└──__init__.py
└── cd
├── __init__.py
├── preprocessor
└── dataOps
└── __init__.py
└── verint.py
我得到的错误-
Traceback (most recent call last):
File "main_scripts/PreprocessingStep/verint_preprocessing.py", line 11, in <module>
from rogers.cd.dataOPs import Verint
ImportError: No module named rogers.cd.dataOPs
dataops中的init.py是这样的
from rogers.cd.dataOPs.boldchat import *
from rogers.cd.dataOPs.verint import *
这是我的 setup.py 的样子(没有显示整个文件,只显示其中的一部分)
classifiers=[
"Programming Language :: Python :: 3",
"Operating System :: OS Independent",
],
install_requires = external_packages,
package_dir={"": "src"},
packages= setuptools.find_namespace_packages(where="src"),
package_data={'': ['*.yaml']},
include_package_data=True,
python_requires=">=3.7",
我最近从 PyCharm 迁移到 VSCode。该代码 运行 在 PyCharm 中很好,但不适用于 VSCode。
setup.py在VSCode中不起作用,可以通过(terminal.integrated.env.*) and/or within an .env file
:
The PYTHONPATH environment variable specifies additional locations where the Python interpreter should look for modules. In VS Code, PYTHONPATH can be set through the terminal settings (terminal.integrated.env.*) and/or within an .env file.
When the terminal settings are used, PYTHONPATH affects any tools that are run within the terminal by a user, as well as any action the extension performs for a user that is routed through the terminal such as debugging. However, in this case when the extension is performing an action that isn't routed through the terminal, such as the use of a linter or formatter, then this setting will not have an effect on module look-up.
When PYTHONPATH is set using an .env file, it will affect anything the extension does on your behalf and actions performed by the debugger, but it will not affect tools run in the terminal.
If needed, you can set PYTHONPATH using both methods.
An example of when to use PYTHONPATH would be if you have source code in a src folder and tests in a tests folder. When running tests, however, those tests can't normally access modules in src unless you hard-code relative paths.
To solve this problem, you could add the path to src to PYTHONPATH by creating an .env file within your VS Code workspace.
PYTHONPATH=src
首先,在 rogers
文件夹中添加 __init__.py
文件。
然后,您需要将您的项目目录路径添加到sys.path
或$PYTHONPATH
。那么只有python可以识别包并导入。这样肯定能解决问题。