Defining custom import aliases for auto import?

Answered

If I create a new project (with a blank virtual environment), and add a file that only contains `np.array([1, 2, 3])`, if I haven't imported NumPy yet, PyCharm offers to `import numpy as np`. In particular, PyCharm knows that np is referring to numpy and will add the appropriate import numpy as np.

Is there a way to define my own alias names to be recognized? For example, tensorflow is usually imported as tf. PyCharm doesn't know this by default though, and so doesn't give me the automatic import completion. Any way to do this?

Also notable, if I install another package which has a module named `np` I am no longer offered the `import numpy as np` auto import. For example, if I install `plotly`, now the only auto import options listed for `np` are the ones within `plotly` (e.g., `plotly.figure_factory.np`). Is there also a way to keep offering the `import numpy as np` option?

2 comments
Comment actions Permalink

The default library aliases were added with https://youtrack.jetbrains.com/issue/PY-13231

The problem with numpy auto-import is known https://youtrack.jetbrains.com/issue/PY-36374, please vote and follow for updates.

Also, I couldn't find a feature request about custom library aliases, so feel free to submit one.

0
Comment actions Permalink

I have now found this issue https://youtrack.jetbrains.com/issue/PY-30031, which, although is not explicitly for custom aliases, might eventually result in that implementation.

0

Please sign in to leave a comment.