简体   繁体   English

PyCharm安装包怎么使用?

[英]How do I use installed packages in PyCharm?

In PyCharm , I've added the Python environment /usr/bin/python .PyCharm中,我添加了 Python 环境/usr/bin/python However,然而,

from gnuradio import gr

fails as an undefined reference .作为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

Adding a Path添加路径

Go into File → Settings → Project Settings → Project Interpreter.进入文件→设置→项目设置→项目解释器。

Then press configure interpreter, and navigate to the "Paths" 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 添加到此处的路径中。

You should never need to modify the path directly, either through environment variables or sys.path .您永远不需要通过环境变量或sys.path直接修改路径。 Whether you use the os (ex. apt-get ), or pip in a virtualenv, packages will be installed to a location already on the path.无论您使用操作系统(例如apt-get )还是在 virtualenv 中使用pip ,软件包都将安装到路径上已经存在的位置。

In your example, GNU Radio is installed to the system Python 2's standard site-packages location, which is already in the path.在您的示例中,GNU Radio 安装到系统 Python 2 的标准site-packages位置,该位置已经在路径中。 Pointing PyCharm at the correct interpreter is enough;将 PyCharm 指向正确的解释器就足够了; if it isn't there is something else wrong that isn't apparent.如果不是,还有其他不明显的错误。 It may be that /usr/bin/python does not point to the same interpreter that GNU Radio was installed in;可能/usr/bin/python没有指向安装 GNU Radio 的同一个解释器; try pointing specifically at the python2.7 binary.尝试专门指向python2.7二进制文件。 Or, PyCharm used to be somewhat bad at detecting packages;或者,PyCharm 过去在检测包方面有些欠佳; File > Invalidate Caches > Invalidate and Restart would tell it to rescan. 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.这个答案将涵盖您应该如何设置项目环境、在不同场景中安装包以及配置 PyCharm。 I refer multiple times to the Python Packaging User Guide , written by the same group that maintains the official Python packaging tools.我多次参考Python Packaging User Guide ,由维护官方 Python 打包工具的同一组编写。


The correct way to develop a Python application is with a virtualenv. 开发 Python 应用程序的正确方法是使用 virtualenv。 Packages and version are installed without affecting the system or other projects.安装包和版本不会影响系统或其他项目。 PyCharm has a built-in interface to create a virtualenv and install packages. PyCharm 有一个内置的接口来创建一个 virtualenv 和安装包。 Or you can create it from the command line and then point PyCharm at it.或者您可以从命令行创建它,然后将 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 .在您的 PyCharm 项目中,转到File > Settings > Project > Project Interpreter If you used virtualenvwrapper or PyCharm to create the env, then it should show up in the menu.如果您使用virtualenvwrapper或 PyCharm 创建环境,那么它应该会显示在菜单中。 If not, click the gear, choose Add Local , and locate the Python binary in the env.如果没有,请单击齿轮,选择Add Local ,然后在 env 中找到 Python 二进制文件。 PyCharm will display all the packages in the selected env. PyCharm 将显示所选环境中的所有包。

选择一个环境

手动定位环境


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 ).在某些情况下,例如使用 GNU Radio,没有要使用pip安装的软件包,当您安装 GNU Radio 的其余部分(例如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.在这种情况下,您仍然应该使用 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 affecting the system.不幸的是它看起来有点乱,因为所有系统包现在都会出现在你的环境中,但它们只是链接,你仍然可以安全地安装或升级包而不影响系统。


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.py file to your package , which will be required anyway to properly distribute and deploy the package later.所有这一切都需要 在你的包中添加一个setup.py文件,无论如何都需要它来正确分发和部署包。

Minimal setup.py for 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:然后将其安装在您的第二个项目的环境中:

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

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

Add path in PyCharm 2017在 PyCharm 2017 中添加路径

File -> Settings (or Ctrl+Alt+S) -> Project -> Project Interpreter文件 -> 设置(或 Ctrl+Alt+S)-> 项目 -> 项目解释器

在此处输入图像描述 Show all显示所有

在此处输入图像描述 Select bottom icon on the right side选择右侧的底部图标

在此处输入图像描述 Click on the plus button to add new path to your module单击加号按钮为您的模块添加新路径

My version is PyCharm Professional edition 3.4, and the Adding a Path part is different.我的版本是 PyCharm Professional edition 3.4, Adding a Path部分不一样。

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.然后你可以添加一个路径。

DON'T change the interpreter path.不要更改解释器路径。

Change the project structure instead:改为更改项目结构:

File -> Settings -> Project -> Project structure -> Add content root文件 -> 设置 -> 项目 -> 项目结构 -> 添加内容根

In PyCharm 2020.1 CE and Professional, you can add a path to your project's Python interpreter by doing the following:在 PyCharm 2020.1 CE 和 Professional 中,您可以通过执行以下操作向项目的 Python 解释器添加路径:

1) Click the interpreter in the bottom right corner of the project and select 'Interpreter Settings' 1)点击项目右下角的解释器,选择“解释器设置”

选择解释器设置

2) Click the settings button to the right of the interpreter name and select 'Show All': 2)单击解释器名称右侧的设置按钮,然后选择“全部显示”:

选择显示所有口译员

3) Make sure your project's interpreter is selected and click the fifth button in the bottom toolbar, 'show paths for the selected interpreter': 3) 确保选择了项目的解释器,然后单击底部工具栏中的第五个按钮,“显示所选解释器的路径”:

显示所选 Python 解释器的路径

4) Click the '+' button in the bottom toolbar and add a path to the folder containing your module: 4) 单击底部工具栏中的“+”按钮,并添加包含模块的文件夹的路径:

在此处输入图像描述

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+insert”添加新路径。

As quick n dirty fix, this worked for me: Adding this 2 lines before the problematic import:作为快速的脏修复,这对我有用:在有问题的导入之前添加这 2 行:

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

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.它将在索引中被解析以进行代码完成以及其他项目。

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.我是 PyCharm(使用 2018.3.4 CE)和 Python 的新手,所以我死记硬背地尝试遵循上述每个建议来访问我知道在 system-site-packages 中的 PIL(Pillow)包。 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.我正要放弃一夜,突然在 Project Explorer 窗口中注意到我的项目下的 venv/pyvenv.cfg 文件。 I found the line "include-system-site-packages = false" in that file and so I changed it to "true".我在该文件中找到了“include-system-site-packages = false”行,因此将其更改为“true”。 Problem solved.问题解决了。

在我的 PyCharm 2019.3 中,选择项目,然后选择 File ---> Settings,然后 Project: YourProjectName,在 'Project Interpreter' 中,点击解释器或设置,---> Show all... ---> 选择当前解释器--->显示所选解释器的路径--->然后单击“添加”以添加您的库,在我的情况下,它是一个轮包

For me there is also another issue.对我来说还有另一个问题。 If you try to add a folder that in the past had a .idea folder, but your current project has it's own .idea folder your pycharm might get confused for some reason -- even if you have the right python/conda env.如果您尝试添加一个过去有一个.idea文件夹的文件夹,但您当前的项目有它自己的.idea文件夹,那么您的 pycharm 可能会由于某种原因感到困惑——即使您有正确的 python/conda env。 For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs.对我来说,删除另一个项目的.idea文件夹解决了它可以找到明显正确安装的 pkgs 的困惑。 Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.然后它能够​​在 pycharm 编辑器 GUI 中找到它们,snf 停止以红色隐藏它们。

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点击申请

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM