如何在Pycharm中使用已安装的软件包?

发布于 2025-02-06 23:03:27 字数 459 浏览 4 评论 0原文

在Pycharm中,我添加了Python环境/usr/bin/python,而GNU广播在Pycharm之外正常工作。但是,导入它在pycharm中失败,而

from gnuradio import gr

GNU无线电位于/usr/local/lib/python2.7/site-packages/gnuradio,我将其添加到PythonPath

我的PythonPath/usr/local/lib/python2.7/site-packages:/usr/local/lib/lib/python2.7/site-packages/gnuradio

In PyCharm I've added the Python environment /usr/bin/python and GNU Radio works fine outside of PyCharm. However, importing it fails in PyCharm with an undefined reference.

from gnuradio import gr

GNU Radio is located at /usr/local/lib/python2.7/site-packages/gnuradio and I've added it to the PYTHONPATH.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

灵芸 2025-02-13 23:03:28

我的版本是Pycharm Professional Edition 3.4,添加路径零件是不同的。

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

然后选择“更多...” - > “选定解释器的展示路径” - > “添加”。然后,您可以添加一条路径。

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

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.

北方的韩爷 2025-02-13 23:03:28

不要更改解释器路径。

改为更改项目结构:

文件 - >设置 - >项目 - >项目结构 - >添加内容root

DON'T change the interpreter path.

Change the project structure instead:

File -> Settings -> Project -> Project structure -> Add content root

悍妇囚夫 2025-02-13 23:03:28

在Pycharm 2020.1 CE和Professional中,您可以通过执行以下操作为项目的Python解释器添加路径:

1)单击项目右下角的解释器,然后选择“解释器设置”

i.sstatic.net/148bw.jpg“ rel =“ nofollow noreferrer”>

”选择“

3)确保选择了项目的解释器,然后单击“底部工具栏”中的第五个按钮,'show路径'选择的解释器':

“显示选定的Python解释器的路径”

4)单击底部工具栏中的'+'按钮,然后在包含模块的文件夹中添加路径:

In PyCharm 2020.1 CE and Professional, you can add a path to your project's Python interpreter by doing the following:

1) Click the interpreter in the bottom right corner of the project and select 'Interpreter Settings'

Select Interpreter Settings

2) Click the settings button to the right of the interpreter name and select 'Show All':

Select Show All Interpreters

3) Make sure your project's interpreter is selected and click the fifth button in the bottom toolbar, 'show paths for the selected interpreter':

Show paths for the selected Python interpreter

4) Click the '+' button in the bottom toolbar and add a path to the folder containing your module:

enter image description here

清醇 2025-02-13 23:03:28

对于Pycharm社区版,2016.3.2是:

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

然后在右侧应该有一个包装图标。徘徊时,应该说“显示选定解释器的路径”。单击它。

然后单击“添加”按钮或按“ ALT+INSERT”以添加新路径。

For PyCharm Community Edition 2016.3.2 it is:

"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.

城歌 2025-02-13 23:03:28

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

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

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

import sys
sys.path.append('C:\\Python27\\Lib\site-packages')
最美不过初阳 2025-02-13 23:03:28

在Project Explorer上,您可以右键单击包含模块的文件夹并将其设置为“源”。

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

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.

流绪微梦 2025-02-13 23:03:28

我是Pycharm的新手(使用2018.3.4 CE)和Python,所以我旋转地尝试遵循上述每个建议,以访问我知道的PIL(枕头)包(枕头)包,这些软件包是在系统站点包装中的。没有工作。当我碰巧注意到项目Explorer窗口中的项目下的Venv/pyvenv.cfg文件时,我正准备放弃夜晚。我在该文件中发现了“ Include-Simit-packages = false”行,因此我将其更改为“ True”。问题解决了。

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.

淡淡の花香 2025-02-13 23:03:28

在我的Pycharm 2019.3中,选择该项目,然后选择文件--->设置,然后项目:YourProjectName,在“项目解释器”中,单击解释器或设置,---->显示全部... --->选择当前的解释器--->显示选定解释器的路径--->然后单击“添加”以添加您的库

In my PyCharm 2019.3, select the project, then File ---> Settings, then Project: YourProjectName, in 'Project Interpreter', click the interpreter or settings, ---> Show all... ---> Select the current interpreter ---> Show paths for the selected interpreter ---> then click 'Add' to add your library, in my case, it is a wheel package

何以畏孤独 2025-02-13 23:03:28

对我来说,还有另一个问题。如果您尝试添加一个过去的文件夹,该文件夹在过去有一个.idea文件夹,但是您当前的项目具有自己的.idea文件夹,您的pycharm可能会因为某种原因而感到困惑 - - 即使您拥有合适的Python/Conda Env。对我而言,删除另一个项目的.idea文件夹修复了可以找到明显正确安装的pkgs的混乱。然后,就可以在Pycharm编辑Gui SNF中找到它们,不再以红色为基础。

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. For me deleting the .idea folder of the other project fixed the confusion that it could find the obviously correctly installed pkgs. Then it was it was able to find them in the pycharm editor GUI snf stopped underlyinging them in red.

时常饿 2025-02-13 23:03:28

下载Anaconda
https://anaconda.org/

一旦完成安装Anaconda ...进入设置...

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

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

单击应用

Download anaconda
https://anaconda.org/

once done installing anaconda...

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

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

click apply

enter image description here

月寒剑心 2025-02-13 23:03:27

添加路径

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

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

“

按路径区域中的 +按钮。您可以将要识别的模块的路径置于通往模块的路径。

但是我不知道路径。

打开可以导入模块的python解释器。

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

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

foobarbaz/
  gnuradio/
    __init__.py
    other_file.py

您想在此处的路径上添加foobarbaz。

Adding a Path

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

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

pycharm path tab

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.

>> 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.

方觉久 2025-02-13 23:03:27

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

在您的示例中,将GNU收音机安装到系统Python 2的标准site-packages位置,该位置已经在路径中。将Pycharm指向正确的解释器就足够了。如果不是其他问题,那并不明显。可能是/usr/bin/python没有指向GNU无线电安装的同一解释器;尝试专门指向python2.7二进制。或者,pycharm曾经在检测包装方面有些不好。 file>无效的Caches>无效并重新启动会告诉扫描。

此答案将涵盖您应该如何设置项目环境,在不同情况下安装软件包,然后配置Pycharm。我多次将


开发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

在您的pycharm项目中,转到file>设置>项目>项目解释器。如果您使用 virtualenvwrapper或pycharm或pycharm创建env,然后应该出现在菜单中。如果不是,请单击齿轮,选择添加本地,然后在Env中找到Python二进制文件。 Pycharm将在选定的Env中显示所有软件包。

“手动定位env”


在某些情况下,例如使用GNU广播,没有包装可以使用pip安装,当您安装其余时间时,该软件包已安装在系统范围内GNU无线电(例如apt-get install gnuradio)在这种情况下,您仍然应该使用Virtualenv,但是您需要使其意识到此系统软件包。

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

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


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

第一个项目的最小setup.py

from setuptools import setup, find_packages

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

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

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

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 pip in a virtualenv, packages will be installed to a location already on the path.

In your example, GNU Radio is installed to the system Python 2's standard site-packages location, 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/python does not point to the same interpreter that GNU Radio was installed in; try pointing specifically at the python2.7 binary. Or, PyCharm used to be somewhat bad at detecting packages; File > Invalidate Caches > Invalidate and Restart would tell it to rescan.

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.


The correct way to develop a Python application is with a 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. Or you can create it from the command line and then point PyCharm at it.

$ 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 virtualenvwrapper or 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.

choose an env

manually locate 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.

$ 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.

Minimal setup.py for your first project:

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
宫墨修音 2025-02-13 23:03:27

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

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

国产ˉ祖宗 2025-02-13 23:03:27

添加Pycharm 2017

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


显示全部

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

”在此处输入图像描述”
单击“加号”按钮以在模块中添加新路径

Add path in PyCharm 2017

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

enter image description here
Show all

enter image description here
Select bottom icon on the right side

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文