PyCharm autoimport with autocomplete
Asked Answered
L

3

15

I am giving PyCharm a try for the first time. Coming from an Eclipse/PyDev environment I have to say so far things have been going well.

There is one feature I am missing that I can't seem to find though and that is as follows:

In the auto-complete list in PyDev, when typing a symbol that doesn't exist in the file's scope it will show a list of modules that it's in. If you select that autocomplete item not only will PyDev complete the symbol for you but it will automatically add the import statement to take care of the dependency as well.

Does this exist in PyCharm?

So far it only seems to autocomplete things already in the scope of the document.

Lingcod answered 8/6, 2011 at 19:59 Comment(0)
W
18

See Settings | Editor | General | Auto Import.

Wellfavored answered 8/6, 2011 at 20:13 Comment(8)
Hrm, this is on by default. Anyway, giving you the answer since this is correct according to my question. It just doesn't add the import until I hit alt+enter I don't think...Lingcod
If you think that it's a bug, please make sure to submit it at youtrack.jetbrains.net/issues/PY and provide the steps to reproduce.Wellfavored
It works when you invoke the auto-complete with Control-Option-Space (probably Control-Alt-Space on a non-Mac). That will auto-complete symbols also from outside of the current file and accepting an item will automatically add the import statement for it.Frederiksberg
Right, it's not a bug. But not necessarily obvious at first.Lingcod
Indeed, it's different to the behaviour of IntelliJPhosphoric
So, auto-complete exists.. but auto-auto-complete (like from IntelliJ) does not..Hellenic
Press Control-Space twice has the same effect as Control-Option-SpaceOlivier
Unfortunately it doesn't address finely the problem since we need to press the shortcut for each missing import. Example : I have 10 distinct missing imports, I need to execute 10 times the shortcut. On intellij performing the completion import is required only for ambiguous imports.Saltatory
R
0

I think this is definitely a bug or bad functional

Fresh installation of pycharm 2021.3.3, when writing code, the IDE itself automatically quietly puts some packages according to random words from the code. As a result, the environment was broken.

here is an example (that from pycharm eventlog):

source /home/user/.cache/pypoetry/virtualenvs/project-_uWvZL1R-py3.9/bin/activate
/home/user/anaconda3/bin/poetry add self

Using version ^2020.12.3 for self

Updating dependencies
Resolving dependencies...

Writing lock file

Package operations: 2 installs, 0 updates, 1 removal

  • Removing setuptools (62.6.0)
  • Installing decorator (5.1.1)
  • Installing self (2020.12.3)

  EnvCommandError

    × python setup.py egg_info did not run successfully.
    │ exit code: 1
    ╰─> [1 lines of output]
        ERROR: Can not execute `setup.py` since setuptools is not available in the build environment.
        [end of output]
Respite answered 21/6, 2022 at 0:23 Comment(0)
P
0

These variants are already there, they are just not included in the basic completion and completion auto-popup for performance reasons. In order to see them you need to invoke completion for the second time at the same place or use a dedicated shortcut (search for "Second Basic Completion" in the keymap settings).

Auto-import extended completion demo

Patricio answered 10/8, 2023 at 11:15 Comment(2)
Thank you for the update. Unfortunately it doesn't address the problem since we need to press the shortcut for each missing import. Example : I have 10 distinct missing imports, I need to execute 10 times the shortcut. On intellij performing the completion import is required only for ambiguous imports. About performance issue, I don't understand why it would be more the case for python than Java or Javascript. I think that the on the fly import could be implemented in python, with even some adjustments like a list of package for on the fly imports if really the performance was a problemSaltatory
Because in Java and JavaScript worlds dependencies are normally managed on per-project basis, while in Python there are system interpreters that can have hundreds of packages installed with all their APIs available for import. Imagine the Anaconda distribution, for instance. Stay tuned, we're working on that.Patricio

© 2022 - 2024 — McMap. All rights reserved.