Python PyCharm 导入外部库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24197970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:11:33  来源:igfitidea点击:

PyCharm import external library

pythonimportpycharm

提问by GeorgeWilliams993

I am using PyCharm as an editor for python code in Houdini. Whenever I try to import the main Houdini library (hou) I get an error flagged in PyCharm. If I include the code snippet:-

我在 Houdini 中使用 PyCharm 作为 python 代码的编辑器。每当我尝试导入主 Houdini 库 (hou) 时,我都会在 PyCharm 中标记一个错误。如果我包含代码片段:-

try:  
   import hou  
except ImportError:  
    # Add $HFS/houdini/python2.6libs to sys.path so Python can find the  
    # hou module.  
    sys.path.append(os.environ['HFS'] + "/houdini/python%d.%dlibs" % sys.version_info[:2])  
    import hou 

my code executes, without problem, from both Houdini and my selected interpreter.

我的代码可以从 Houdini 和我选择的解释器中毫无问题地执行。

My problem is with PyCharm itself. The editor flags 'import hou' as an error and any subsequent files that import this file flag modules imported by this file as errors as well. Hence I loose type ahead functionality and get an over abundance of error messages that make it hard to spot the real problems.

我的问题是 PyCharm 本身。编辑器将“import hou”标记为错误,任何后续导入此文件的文件也将由此文件导入的模块标记为错误。因此,我放弃了提前输入功能,并收到了大量错误消息,这使得很难发现真正的问题。

How do I get PyCharm to recognize the path to the hou module.

如何让 PyC​​harm 识别 hou 模块的路径。

I have tried, for a couple of days, to Google a solution to this problem but they all seem to refer to tabs and settings that are not in my version of PyCharm (Community Edition 3.4.1). E.G. my 'Project Interpreter' setting only has a list of Packages and has no 'path' tab as stated in many 'fixes' to closely related problems.

几天来,我已经尝试在 Google 上找到解决此问题的方法,但它们似乎都指的是我的 PyCharm 版本(社区版 3.4.1)中没有的选项卡和设置。例如,我的“项目解释器”设置只有一个包列表,没有“路径”选项卡,如许多对密切相关问题的“修复”所述。

回答by alexanderlukanin13

Update (2018-01-06):This answer is obsolete. Modern versions of PyCharm provide Paths via Settings ? Project Interpreter ? ? ? Show All ? Show paths button.

更新 (2018-01-06):此答案已过时。现代版本的 PyCharm 通过设置提供路径?项目口译员 ? ? ? 显示所有 ?显示路径按钮。



PyCharm Professional Edition has the Pathstab in Python Interpreterssettings, but Community Edition apparently doesn't have it.

PyCharm 专业版PathsPython Interpreters设置中有选项卡,但社区版显然没有。

As a workaround, you can create a symlink for your imported library under your project's root.

作为解决方法,您可以在项目根目录下为导入的库创建符号链接。

For example:

例如:

myproject
    mypackage
        __init__.py
    third_party -> /some/other/directory/third_party

回答by GeorgeWilliams993

Since PyCharm 3.4 the path tab in the 'Project Interpreter' settings has been replaced. In order to add paths to a project you need to select the cogwheel, click on 'More...' and then select the "Show path for the selected interpreter" icon. This allows you to add paths to your project as before.

从 PyCharm 3.4 开始,“项目解释器”设置中的路径选项卡已被替换。为了向项目添加路径,您需要选择齿轮,单击“更多...”,然后选择“显示所选解释器的路径”图标。这允许您像以前一样向项目添加路径。

My project is now behaving as I would expect.

我的项目现在表现得如我所料。

These are the windows you would see while following the instructions

这些是您按照说明操作时会看到的窗口

回答by Hghowe

Answer for PyCharm 2016.1 on OSX: (This is an update to the answer by @GeorgeWilliams993's answer above, but I don't have the rep yet to make comments.)

OSX 上 PyCharm 2016.1 的答案:(这是对上述@GeorgeWilliams993 答案的更新,但我还没有代表发表评论。)

Go to Pycharm menu --> Preferences --> Project: (projectname) --> Project Interpreter

转到 Pycharm 菜单 --> Preferences --> Project: (projectname) --> Project Interpreter

At the top is a popup for "Project Interpreter," and to the right of it is a button with ellipses (...) - click on this button for a differentpopup and choose "More" (or, as it turns out, click on the main popup and choose "Show All").

顶部是“项目解释器”的弹出窗口,其右侧是一个带有省略号 (...) 的按钮 - 单击此按钮以显示不同的弹出窗口并选择“更多”(或者,事实证明,单击主弹出窗口并选择“全部显示”)。

This shows a list of interpreters, with one selected. At the bottom of the screen are a set of tools... pick the rightmost one:

这将显示一个解释器列表,其中一个被选中。屏幕底部是一组工具...选择最右边的一个:

Show path for the selected interpreter button

显示所选解释器按钮的路径

Now you should see all the paths pycharm is searching to find imports, and you can use the "+" button at the bottom to add a new path.

现在您应该看到 pycharm 正在搜索以查找导入的所有路径,您可以使用底部的“+”按钮添加新路径。

I think the most significant difference from @GeorgeWilliams993's answer is that the gear button has been replaced by a set of ellipses. That threw me off.

我认为与@GeorgeWilliams993 的答案最显着的区别是齿轮按钮已被一组椭圆取代。那让我失望了。

回答by Let Me Tink About It

In my case, the correct menu path was:

就我而言,正确的菜单路径是:

File > Default settings > Project Interpreter

文件 > 默认设置 > 项目解释器

回答by ArtificiallyIntelligence

updated on May 26-2018

2018 年 5 月 26 日更新

If the external library is in a folder that is under the project then

如果外部库位于项目下的文件夹中,则

File-> Settings-> Project-> Project structure-> select the folder and Mark as Sources!

文件->设置->项目->项目结构-> 选择文件夹并标记为来源!

If not, add content root, and do similar things.

如果没有,添加内容根,并做类似的事情。

回答by Darth Gabous

In order to reference an external library in a project File -> Settings -> Project -> Project structure -> select the folder and mark as a source

为了在项目中引用外部库文件 -> 设置 -> 项目 -> 项目结构 -> 选择文件夹并标记为源