问题描述
我正在使用 PyCharm 对没有 IDE 开发的 Python 程序进行一些维护.该程序导入了一个我称之为 zzz 的包.包 zzz 位于名为 zzz 的目录中,该目录位于我称为 xxx 的目录中.目录 zzz 包含一个 __init__.py 文件,但没有 zzz.py 文件.
I'm using PyCharm to do some maintenance on a Python program that was developed without an IDE. The program imports a package that I'll call zzz. Package zzz is in a directory named zzz, which is in a directory that I'll call xxx. Directory zzz contains an __init__.py file, but no zzz.py file.
包 zzz 不在 PYTHONPATH 中,但程序可以像这样访问它:
Package zzz is not in PYTHONPATH, but the program gains access to it like this:
sys.path.append(<absolute path of directory xxx>)
sys.path.append('zzz/zzz')
import zzz
导入后,我可以将光标悬停在包 zzz 的名称上,可以看到它的路径名是目录 zzz 的绝对路径名.
After the import I can float the cursor over the name of package zzz and see that its pathname is the absolute pathname of directory zzz.
但是 PyCharm 将 import 语句标记为错误,大概是因为它在运行前的 PYTHONPATH 中找不到 zzz.
But PyCharm flags the import statement as an error, presumably because it can't find zzz in the pre-runtime PYTHONPATH.
我尝试将 zzz 的路径名添加到项目的内容文件夹(文件">设置"对话框、项目">项目结构"页面).我将其标记为源文件夹.PyCharm 仍将import zzz"标记为错误.
I tried adding the pathname of zzz to the project's content folders (Files > Settings dialog, Project > Project Structure page). I marked it as a source folder. PyCharm still flags "import zzz" as an error.
这使得很难判断程序何时包含真正的错误!无论如何,至少发现一个错误"图标(红色圆圈中的感叹号)出现在滚动条上方.我该怎么做才能让 PyCharm 将 zzz 识别为有效?
This makes it hard to tell when the program contains a real error! The "at least one error found" icon (an exclamation mark in a red circle) appears above the scroll bar, regardless. What can I do to make PyCharm recognize zzz as valid?
我应该注意到重新组织目录"不是一个可行的解决方案;永久更改 PYTHONPATH"也不是.我们有十几个程序使用 zzz 并以相同的方式加载它;我没有权力动它,有权力的人也不愿意动.程序的用户也不愿意改变他们的 PYTHONPATH 来帮助我让 PyCharm 开心!
I should note that "reorganize the directories" is not a viable solution; nor is "permanently change PYTHONPATH." We have a dozen or so programs that use zzz and load it the same way; I don't have the authority to move it, and the person who has the authority would not be willing to do so. Nor would the programs' users be willing to change their PYTHONPATH to help me make PyCharm happy!
推荐答案
Preferences->Project Interpreter,选择齿轮图标,然后选择 Show Path for Selected Interpreter 并添加所需的路径
Preferences->Project Interpreter, select the gear icon, then select Show Path for Selected Interpreter and add the desired path
这篇关于如何让 PyCharm 识别不在 PYTHONPATH 中的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!