Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make IntelliJ/PyCharm be able to correctly identify every part of a Python namespace?

I use many python packages sharing a namespace (setuptools has support for that). Those work well with python 2.7 with setuptools. My question is regarding the PyCharm plugin for IntelliJ: it does not fully recognize the files and members of the package.

Assume I have many packages:

First package:

cantrips
+--- types
|    +--- ...
+--- patterns
|    +--- ...
+--- (more subpackages here)
+--- entropy.py
+--- (more *.py files here)

Second package:

cantrips
+--- watch
     +--- ...

If I try to import anything inside cantrips.watch or cantrips.entropy I have no problem at all (this works perfectly in Python27). However the Pycharm plugin in IntelliJ IDEA 13 does not seem to be able to find cantrips.entropy with code inspection.

Q: How can I correctly make IntelliJ/PyCharm understand every part of the namespace?

like image 692
Luis Masuelli Avatar asked Mar 27 '15 03:03

Luis Masuelli


People also ask

What is namespace package Python?

In Python, a namespace package allows you to spread Python code among several projects. This is useful when you want to release related libraries as separate downloads.

Can IntelliJ be used for Python?

To work with your Python code in IntelliJ IDEA, you need to configure at least one interpreter. A system interpreter is the one that comes with your Python installation. You can use it solely for all Python scripts or take it as a base interpreter for Python virtual environments.


2 Answers

I'm using PyCharm 2019.1 and I've found a way to do it (It may qualify for a workaround)

The trick with namespace packages is that, one package is distributed over multiple directories, once you understand this fact, it's pretty easy.

Typically with PyCharm, the Content Root Directory or Project Root Directory is set to the directory you opened the project in (Default of Normal Packages), as it does not expect a namespace package. This confuses PyCharm as it sets the structure accordingly for the interpreter to set the PYTHONPATH.

To correct this for Namespace packages, just add all the directories that have the namespace folder to the Content Root.

File -> Settings -> Project Structure -> Add Content Root

So if you had a structure as

rootdir/
+--dist_package1/
   +--namespace/
      +--file1.py
+--dist_package2/
   +--namespace/
      +--file2.py

Instead of setting "Content Root" directory as rootdir, set it to all the directories that contain the namespace, such as, dist_package1 and dist_package2.

Remove the original Content Root that was set to rootdir.

This should fix the unresolved references to the namespace package

UPDATE (07/25/2019):

Instead doing it the difficult way, you could just right-click on dist_package1 and dist_package2 and select

Mark Directory As -> Sources Root

This should resolve your imports

like image 66
rite2hhh Avatar answered Oct 16 '22 20:10

rite2hhh


Are the packages in separate projects?

I think I am having the same issue and have filed a support ticket with JetBrains.

If in the second package you can import cantrips.entropy but in the body of the code if the code inspection can't resolve cantrips.entropy such as:

import cantrips.entropty

def my_func():
    cantrips.entropy.do_stuff()
    ...

The IDE complains that "entropy" is not resolved in my_func by highlighting it in yellow.

A couple of work-arounds are to use an alais:

import cantrips.entropty as cantrips_entropty

def my_func():
    cantrips_entropy.do_stuff()
    ...

Or use the "from import" syntax:

from cantrips.entropty import do_stuff

def my_func():
    do_stuff()
    ...

Hope this helps

like image 28
Adrian Jarvis Avatar answered Oct 16 '22 20:10

Adrian Jarvis