ChatGPT解决这个技术问题 Extra ChatGPT

How do I use installed packages in PyCharm?

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

from gnuradio import gr

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

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

Gnuradio is located at /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

Ok, so how did you install gnuradio?
You can install gnuradio from the available repositories in PyCharm. PyCharm -> Preferences (Settings on Windows) -> Project -> Project Interpreter -> click on '+' sign in packages section and search for the required package. You may have to add repositories if you the required package isn't available in already added repositories. HTH.
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.

S
Stevoisiak

Adding a Path

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

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

https://i.imgur.com/Q5pcWDX.jpg

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.


@FakeRainBrigand Can't see the Paths tab in PyCharm4.
In PyCharm 4 CE: Go to the 'Project Interpreter'. Click the gear to the right of the interpreter's path. It will bring up a short drop-down menu, from which you should select "More..". On the right hand side of the new pop-up, there will be an icon with the mouse-over text of "Show paths for the selected interpreter". Click that button. This new 'Interpreter Paths' pop-up is where you can add paths. @AnkeshAnand
Nice solution. I found out that my interpreter was set to virtual environment that did not include numpy. I simply set it back to the standard interpretor and this solved my problem.
When I select 3.5.3, the Add/remove/up buttons in the package area are grayed out.
For Mac users, press the PyCharm at the top left then Preferences...->Project->Project Interpreter->Click the + sign to add a package
x
xjcl

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.

https://i.stack.imgur.com/gE7k8.png

https://i.stack.imgur.com/dLVhx.png

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

m
michaelsnowden

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


It worked for me, just right click on the directory and then Mark Directory As: Sources root. Thank you @michaelsnowden
Worked for me; the bizarre part is that this is like my 15th PyCharm project and the first time I've had to do this.
My local modules could be found at runtime, but showed with red squiggles as if they could not be seen. "mark directory as sources root" took the squiggle off, though not sure that's the correct method.
do you know what is the default source root for pycharm, if it is not specified?
@KimMiller I had exactly the same situation 3 years after you. PyCharm can find local packages/modules only if one marks them as "sources root".
D
Dorin

Add path in PyCharm 2017

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

https://i.stack.imgur.com/zJtDv.png

https://i.stack.imgur.com/h7Kxg.png

https://i.stack.imgur.com/oRnsx.png


Finally normal answer , just wanted to know where to paste the default pip size_packages folder .
I have done the same to add the path to a .jar file so I ca use some java libraries. However, I do not know how to specify the jar file and that it is supposed to be used by my script. Could you help me further?
c
chenlian

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.


For me at least, when I do this and click "ok", I go back, find it still doesn't work. Then I go back to the list of paths associated with the current interpreter and find that the path I just added is gone.
after adding the path you have to click the refresh. close the window. now the apply button is not active. switch around the settings and click apply if it is active
D
Daniel Braun

DON'T change the interpreter path.

Change the project structure instead:

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


Thanks, works great if imports in the project look broken.
G
GingerBreadMane

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'

https://i.stack.imgur.com/148bW.jpg

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

https://i.stack.imgur.com/sQWnz.jpg

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

https://i.stack.imgur.com/PiEm3.jpg

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

https://i.stack.imgur.com/TtqZh.jpg


W
Wesley

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.


r
rundekugel

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')

佚名

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.


My local modules could be found at runtime, but showed with red squiggles as if they could not be seen. "mark directory as sources root" took the squiggle off, though not sure that's the correct method.
u
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.


Good to note, but experienced virtual environment users usually do not want packages from other locations leaking into their isolated environment, hence the default setting to exclude system-level packages. It leads to unpredictable behavior when the same package is installed in multiple locations, especially when they are different versions.
f
flycoolman

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


C
Charlie Parker

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.


l
loki

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

https://i.stack.imgur.com/19upA.png