3

This issue has come up with previous versions of PyCharm (see this SO post and this one), but it manifests somewhat differently in 4.5.

I am trying to add another library to the Python path of my current project. This is an internal library and consists of a bunch of .py files in a different directory from the current project. I carry out the following steps:

  • Go to File -> Settings -> Project: summary -> Project Interpreter
  • Click on the gear icon near the top right corner of the screen that appears in the dialog
  • Select More... from the context menu that pops up
  • Click on the interpreter I am using
  • Click on the last of the 4 icons to the right of that, the thing with a folder and arrows, with tooltip "Show paths for the selected interpreter"
  • Push + in the resulting popup
  • Use the file dialog to add the path of the library

Having done this, the result is:

  • Good:
    • Running the current project code from a Run Configuration works; that is, the external library is picked up in the PYTHONPATH
    • The added path does not immediately disappear from the interpreter path dialog, as it was doing in 4.0 (see comment to this answer in one of the SO posts mentioned above).
  • Bad:
    • Indexing of the new library fails, and all references to the external library are marked as unresolved references in the editor

I have even tried File -> Invalidate Caches / Restart... and pressed the Invalidate and Restart button that appears. After sitting and waiting for indexing to finish, I get the same result. I have been very careful with setting the right path, and it seems to be correct, given that running the code actually works.

Does anyone know of a workaround for this issue, short of adding the external code as a content root?

Community
  • 1
  • 1
sparc_spread
  • 10,643
  • 11
  • 45
  • 59
  • What library is this? Maybe the library itself does something which prevents static analysis of source code. That's very common in Python. – Mikko Ohtamaa Jun 30 '15 at 17:54
  • Library is just an internal one we wrote ourselves - basically, a bunch of `.py` files in a named directory. If I add the whole thing as a content root, everything works fine, but I am trying to avoid that. What are some kinds of things that could prevent static analysis of source? – sparc_spread Jun 30 '15 at 17:57
  • 1
    Generating module-level variables dynamically is the most common reason. – Mikko Ohtamaa Jun 30 '15 at 18:01
  • 1
    Make sure you have a proper `__init__.py` file – Mikko Ohtamaa Jun 30 '15 at 18:01
  • Only module-level variables I have are treated as constants; various well-known names for things. The library consists of one package and one module. Code consists of one `.py` file for the package and a directory named for the module. The module directory always contained an `__init__.py` file, but till now it was empty. I added a proper `__all__` to it but no luck - indexing still not working. I believe I do not need an `__init__.py` as sibling to the package `.py` and module directory, correct? – sparc_spread Jun 30 '15 at 18:18
  • It is hard to follow the structure of the package in the comment message. However, what I would do is to create a dummy Python package with one variable and try to import it in. Then copy-paste more and more stuff in to see where it breaks. – Mikko Ohtamaa Jun 30 '15 at 18:45
  • I tried a single `.py` file with just one function in it, and PyCharm could not index it. Will try posting on the JetBrains boards. I can't believe it's this hard to use external libraries with PyCharm - I hope I am doing something wrong and that it isn't broken. – sparc_spread Jul 13 '15 at 15:26

2 Answers2

1

I ran into a very similar issue. I am working on an OpenStack component, and all third-party libraries were getting marked as unresolved references. It turned out to be because the .tox directory is automatically excluded by PyCharm, which prevents any virtual environments in that directory from getting indexed properly.

To fix this, I went to the Editor > File Types dialog of the Preferences menu, and removed the .tox folder from the Ignore files and folders option.

user1769868
  • 133
  • 7
1

Checked w/ JetBrains support, they confirmed that the only ways to add external libraries to a PyCharm project are:

  • Add the library as a Content Root
  • OR
  • Open it as separate project in the same window and attach it to your current project

Not the cleanest approaches, as they both basically mean adding the other library's actual code to your project. But they are the only ones at this point.

sparc_spread
  • 10,643
  • 11
  • 45
  • 59