Python 如何在 pycharm 中导入模块?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19885821/
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 14:53:18  来源:igfitidea点击:

How do I import modules in pycharm?

pythonpycharmgnuradio

提问by smurff

In PyCharm, I've added the Python environment /usr/bin/python. However,

PyCharm 中,我添加了 Python 环境/usr/bin/python。然而,

from gnuradio import gr

fails as an undefined reference. However, it works fine in the Python interpreter from the command line.

作为未定义的引用失败。但是,它在命令行的 Python 解释器中运行良好。

GNURadio works fine with python outside of Pycharm. Everything is installed and configured how I want it.

GNURadio 在 Pycharm 之外与 python 一起工作得很好。一切都按照我想要的方式安装和配置。

Gnuradio is located at /usr/local/lib/python2.7/site-packages/gnuradio

Gnuradio 位于 /usr/local/lib/python2.7/site-packages/gnuradio

Also:

还:

PYTHONPATH=/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages/gnuradio

PYTHONPATH=/usr/local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages/gnuradio

采纳答案by Brigand

Adding a Path

添加路径

Go into File → Settings → Project Settings → Project Interpreter.

进入文件 → 设置 → 项目设置 → 项目解释器。

Then press configure interpreter, and navigate to the "Paths" tab.

然后按配置解释器,并导航到“路径”选项卡。

pycharm path tab

pycharm 路径选项卡

Press the + button in the Paths area. You can put the path to the module you'd like it to recognize.

按路径区域中的 + 按钮。您可以输入您希望它识别的模块的路径。

But I don't know the path..

但我不知道路径..

Open the python interpreter where you can import the module.

打开 python 解释器,您可以在其中导入模块。

>> import gnuradio
>> gnuradio.__file__
"path/to/gnuradio"

Most commonly you'll have a folder structure like this:

最常见的是,您将拥有这样的文件夹结构:

foobarbaz/
  gnuradio/
    __init__.py
    other_file.py

You want to add foobarbaz to the path here.

您想将 foobarbaz 添加到此处的路径中。

回答by chenlian

My version is PyCharm Professional edition 3.4, and the Adding a Pathpart is different.

我的版本是PyCharm Professional 3.4版,添加路径部分不一样。

You can go to "Preferences" --> "Project Interpreter". Choose the tool button at the right top corner.

您可以转到“首选项”-->“项目解释器”。选择右上角的工具按钮。

Then choose "More..." --> "Show path for the selected interpreter" --> "Add". Then you can add a path.

然后选择“更多...”-->“显示所选解释器的路径”-->“添加”。然后你可以添加一个路径。

回答by michaelsnowden

For me, it was just a matter of marking the directory as a source root.

对我来说,这只是将目录标记为源根目录的问题。

回答by davidism

You should never need to modify the path directly, either through environment variables or sys.path. Whether you use the os (ex. apt-get), or pipin a virtualenv, packages will be installed to a location already on the path.

您永远不需要直接修改路径,无论是通过环境变量还是sys.path. 无论您使用 os(例如apt-get),还是pip在 virtualenv 中,软件包都将安装到路径上已有的位置。

In your example, GNU Radio is installed to the system Python 2's standard site-packageslocation, which is already in the path. Pointing PyCharm at the correct interpreter is enough; if it isn't there is something else wrong that isn't apparent. It may be that /usr/bin/pythondoes not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restartwould tell it to rescan.

在您的示例中,GNU Radio 安装到系统 Python 2 的标准site-packages位置,该位置已经在路径中。将 PyCharm 指向正确的解释器就足够了;如果不是,还有其他不明显的错误。它可能/usr/bin/python没有指向安装 GNU Radio 的同一个解释器;尝试专门指向python2.7二进制文件。或者,PyCharm 过去在检测包方面有些糟糕;File > Invalidate Caches > Invalidate and Restart会告诉它重新扫描。

This answer will cover how you should set up a project environment, install packages in different scenarios, and configure PyCharm. I refer multiple times to the Python Packaging User Guide, written by the same group that maintains the official Python packaging tools.

这个答案将介绍您应该如何设置项目环境、在不同场景中安装包以及配置 PyCharm。我多次参考Python Packaging User Guide,该指南由维护官方 Python 打包工具的同一组编写。



The correct way to develop a Python application is with a virtualenv.Packages and version are installed without effecting the system or other projects. PyCharm has a built-in interface to create a virtualenv and install packages.Or you can create it from the command line and then point PyCharm at it.

开发 Python 应用程序的正确方法是使用 virtualenv。安装包和版本不会影响系统或其他项目。 PyCharm 有一个内置接口来创建 virtualenv 和安装包。或者您可以从命令行创建它,然后将 PyCharm 指向它。

$ cd MyProject
$ python2 -m virtualenv env
$ . env/bin/activate
$ pip install -U pip setuptools  # get the latest versions
$ pip install flask  # install other packages

In your PyCharm project, go to File > Settings > Project > Project Interpreter. If you used virtualenvwrapperor PyCharm to create the env, then it should show up in the menu. If not, click the gear, choose Add Local, and locate the Python binary in the env. PyCharm will display all the packages in the selected env.

在您的 PyCharm 项目中,转到File > Settings > Project > Project Interpreter. 如果您使用virtualenvwrapper或 PyCharm 创建环境,那么它应该显示在菜单中。如果没有,请单击齿轮,选择Add Local,然后在 env 中找到 Python 二进制文件。PyCharm 将显示所选环境中的所有包。

choose an env

选择一个环境

manually locate env

手动定位 env



In some cases, such as with GNU Radio, there is no package to install with pip, the package was installed system-wide when you install the rest of GNU Radio (ex. apt-get install gnuradio). In this case, you should still use a virtualenv, but you'll need to make it aware of this system package.

在某些情况下,例如对于 GNU Radio,没有要安装pip的软件包,当您安装 GNU Radio 的其余部分(例如apt-get install gnuradio)时,该软件包已在系统范围内安装。在这种情况下,您仍然应该使用 virtualenv,但您需要让它知道这个系统包。

$ python2 -m virtualenv --system-site-packages env

Unfortunately it looks a little messy, because all system packages will now appear in your env, but they are just links, you can still safely install or upgrade packages without effecting the system.

不幸的是它看起来有点乱,因为所有系统包现在都会出现在你的 env 中,但它们只是链接,你仍然可以安全地安装或升级包而不影响系统。



In some cases, you will have multiple local packages you're developing, and will want one project to use the other package. In this case you might think you have to add the local package to the other project's path, but this is not the case. You should install your package in development mode. All this requires is adding a setup.pyfile to your package, which will be required anyway to properly distribute and deploy the package later.

在某些情况下,您将有多个正在开发的本地包,并且希望一个项目使用另一个包。在这种情况下,您可能认为必须将本地包添加到其他项目的路径中,但事实并非如此。你应该在开发模式下安装你的包。所有这一切都需要向您的包中添加一个setup.py文件,无论如何都需要文件才能正确分发和部署包。

Minimal setup.pyfor your first project:

setup.py您的第一个项目最少:

from setuptools import setup, find_packages

setup(
    name='mypackage',
    version='0.1',
    packages=find_packages(),
)

Then install it in your second project's env:

然后将其安装在您的第二个项目的 env 中:

$ pip install -e /path/to/first/project

回答by davidism

On Project Explorer, you can right click on the folder where the module is contained and set as 'Source'.

在项目资源管理器上,您可以右键单击包含模块的文件夹并将其设置为“源”。

It will be parsed in the Index for code completion as well as other items.

它将在代码完成以及其他项目的索引中解析。

回答by Wesley

For PyCharm Community Edition 2016.3.2 it is:

对于 PyCharm 社区版 2016.3.2,它是:

"Project Interpreter" -> Top right settings icon -> "More".

“项目解释器”-> 右上角设置图标->“更多”。

Then on the right side there should be a packages icon. When hovering over it it should say "Show paths for selected interpreter". Click it.

然后在右侧应该有一个包图标。将鼠标悬停在它上面时,它应该说“显示所选解释器的路径”。点击它。

Then click the "Add" button or press "alt+insert" to add a new path.

然后单击“添加”按钮或按“alt+插入”添加新路径。

回答by Jmitsh

Download anaconda https://anaconda.org/

下载 anaconda https://anaconda.org/

once done installing anaconda...

一旦完成安装anaconda...

Go into Settings -> Project Settings -> Project Interpreter.

进入设置 -> 项目设置 -> 项目解释器。

Then navigate to the "Paths" tab and search for /anaconda/bin/python

然后导航到“路径”选项卡并搜索 /anaconda/bin/python

click apply

点击申请

enter image description here

在此处输入图片说明

回答by Dorin

Add path in PyCharm 2017

在 PyCharm 2017 中添加路径

File -> Settings (or Ctrl+Alt+S) -> Project -> Project Interpreter

文件 -> 设置(或 Ctrl+Alt+S) -> 项目 -> 项目解释器

enter image description hereShow all

在此处输入图片说明显示所有

enter image description hereSelect bottom icon on the right side

在此处输入图片说明选择右侧的底部图标

enter image description hereClick on the plus button to add new path to your module

在此处输入图片说明单击加号按钮为模块添加新路径

回答by rundekugel

As quick n dirty fix, this worked for me: Adding this 2 lines before the problematic import:

作为快速 n 脏修复,这对我有用:在有问题的导入之前添加这 2 行:

import sys
sys.path.append('C:\Python27\Lib\site-packages')

回答by user3397407

I'm new to PyCharm (using 2018.3.4 CE) and Python so I rotely tried to follow each of the above suggestions to access the PIL (Pillow) package which I knew was in system-site-packages. None worked. I was about to give up for the night when I happened to notice the venv/pyvenv.cfg file under my project in the Project Explorer window. I found the line "include-system-site-packages = false" in that file and so I changed it to "true". Problem solved.

我是 PyCharm(使用 2018.3.4 CE)和 Python 的新手,所以我死记硬背地尝试按照上述每个建议访问我知道在 system-site-packages 中的 PIL(枕头)包。没有一个工作。当我碰巧在项目资源管理器窗口中注意到我的项目下的 venv/pyvenv.cfg 文件时,我正准备放弃。我在该文件中找到了“include-system-site-packages = false”这一行,因此我将其更改为“true”。问题解决了。