42

I have two pure python projects in PyCharm 3.4.1 Professional Edition. The first one, let's call it p (like package), is structured as a setuptools package (i.e. with setup.py, all requirements etc., however it is not uploaded to pypi or any other online repository). The second one, let's call it s (like script), is just a python script along with two modules.

Project s is (in PyCharm) configured to use a dedicated virtualenv, let's call it venv.

The problem I have is the following: when I install the project (package) p in venv like this:

$ source /path/to/venv/bin/activate
(venv)$ cd /path/to/p
(venv)$ python3 setup.py develop

in PyCharm in project s, import p statements are errorneous with message No module named p. However, when I run the script in s, everything is fine, the only problem is the PyCharm IDE complaining about not being able to find the module. I can live with this but it is very annoying...

Why does this happen? Is it a PyCharm thing or packaging related thing? See NEWS below.


The project/package p has the following structure:

p/
|
+- p/
|  |
|  +- __init__.py
|  +- other subpackages, modules, etc.
+- setup.py
+- README, DESCRIPTION, setup.cfg, etc.

When I configure the PyCharm project p to live in its own virtualenv and install it there in development mode, everything works fine.


NEWS

This problem is still present in PyCharm 5.0.4. However, I managed to solve it, kind-of.

For some reasons I had to install another package from pypi. I did it through PyCharm by going to File -> Settings -> Project: -> Project Interpreter, there clicking on the green +, finding the package and pressing the Install Package button. After the installation, the package installed by python3 setup.py develop is well recognized by PyCharm. Obviously the problem was that PyCharm didn't have some cache in sync with reality.

So the new question is, can PyCharm be told to update its caches regarding the used python environment?

zegkljan
  • 8,051
  • 5
  • 34
  • 49
  • 1
    Nice! Yes, I was able to get this to work just fine even in 4.5.4 just by uninstalling and reinstalling a randomly chosen pip package from the pycharm gui. – Mumbleskates Mar 20 '16 at 12:01
  • for me deleting all conda envs except the one I wanted stopped pycharm getting confused when I selected the actual conda env I wanted to use...odd! – Charlie Parker Jun 28 '22 at 18:07

8 Answers8

26

As of Pycharm 2016.1.4 (professional edition), it seems that opening a different project (e.g. through Open Recent) and then opening the original one makes Pycharm reindex the installed packages and hence recognize the develop-installed packages.

This is enough for me.

zegkljan
  • 8,051
  • 5
  • 34
  • 49
  • ImportError: No module named contrib.tensorboard.plugins still continue to exist after the above steps are performed. – emeralddove Jun 01 '17 at 06:06
  • @makcbe If you get an actual error when running the code, it's a different problem. This question and answer is only about PyCharm failing to recognise installed packages. – zegkljan Jun 01 '17 at 06:12
  • WOW! THIS WORKED, even though the other answer didn't – user3180 May 15 '21 at 12:49
25

In the project pane, the directory which holds p needs to be marked as source. Available under: Right-click -> "Mark Directory As" -> "Sources Root".

Note: This setting will also add it to PYTHONPATH during execution from the run menu if the corresponding check box for the option is checked in "Edit Configurations" (default).

mike
  • 407
  • 4
  • 3
  • 2
    You mean that I should open the project `s` and add the directory of `p` as another root directory? Both projects are completely separated (from pycharm point of view), the only thing that connects them is that one is installed in the venv of the other. So the pycharm really does not recognize the development mode installation in the venv? – zegkljan Oct 10 '14 at 20:04
  • this solution works when you cannot access the other user created modules, which is present in the same folder, from other .py files. Upvoted – Yogamurthy Jul 06 '17 at 16:06
21

I had a devil of a time getting PyCharm to recognize a class in a module that I had just written.

The problem is that PyCharm seems to default to not importing module class paths, which requires two separate fixes to correct.

Step 1

Right click on the module name, and mark it as "Source":

enter image description here

Step 2

For some reason, by default in PyCharm, it does not actually add directories marked as "Sources Root" to the Python path. Fix this by switching this on.

enter image description here

Extra for experts

Notice the "Starting Script" in the image above. I assume that manually adding these lines to your Python script would also achieve the same result.

Tested On

  • Windows 10 x64.
  • PyCharm Community Edition 2017.2.3.
Contango
  • 76,540
  • 58
  • 260
  • 305
14

Solution

  • go to settings->project interpreter.
  • click wheel framed (settings gear icon) button.
  • In the drop-down list, click Show all.... The available interpreters show up in the Project Interpreters dialog.

  • Select the desired interpreter.

  • In the toolbar of the Project Interpreters dialog box, click the button icon show paths (last option). The existing paths of the selected interpreter show up in the Interpreter Paths dialog box.

  • Add your package's path (package that you pip developed) here.ex /home/../repo-name

voila!! you are good to go.

Explanation
Each project refers to an interpreter which you can find in settings->project interpreter. Now this interpreter uses a bunch of Paths to look for a library. By default it has site-packages and bunch of other paths there.
Now since you used pip develop -e or python setup.py develop, a dynamic link is created pointing to your package's repository instead of a package installation in site-packages directory. So our package's source path is not here, What we need to do is to add our source path to interpreter's Paths to make it work

Ref:

  1. https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000019690-PyCharm-not-recognize-development-library-
  2. https://www.jetbrains.com/help/pycharm/installing-uninstalling-and-reloading-interpreter-paths.html
lego king
  • 558
  • 5
  • 11
  • 1
    THIS WORKS YAY! In my case, PyCharm simply did not pick up the `site-packages` directory of a virtualenv created by `pyenv`; adding the `site-packages` directory manually fixes my "missing modules" problem. THANK YOU!!! – pepoluan Jul 26 '19 at 09:04
  • This one did help in my case, THANKS! – Macilias Mar 16 '22 at 11:40
2

Sort of workaround that worked for me:

Open both projects in PyCharm in the same window (workspace). Now open up the settings window, and under "Project -> Project Depencies" you can now select that project s depends on project p. Imports and autocompletion will now work fine.

Lucas van Dijk
  • 799
  • 1
  • 7
  • 11
1

I just had same problem like yours.

Seems pycharm can not recognize module installed directly by setup.py, but can recognize module installed by pip. Finally, I use pip install src_path, but I got to pip install it everytime I modify source code.

sofx
  • 171
  • 1
  • 8
0

I have Just installed package using Pycharm then problem solved.

Hardik Gajjar
  • 1,024
  • 12
  • 27
0

The problem could be your interpreter path. Check where the interpreter is pointing to. In most cases it is ~/PycharmProjects/trials/venv/bin/python and this could be pointing to a python bath installed as part of Pycharm

Change the softlink of python to your /usr/bin/python path and things should work fine

Shiva
  • 1