Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get each python import on a different line when using Alt+Enter to magically import in Pycharm?

Tags:

python

pycharm

Currently if i do Alt+Enter on a function in a different module which isn't imported yet it simply adds it to a an existing import line.

Say I have:

from my_package.my_module import do_something

my_module.do_something()

Then I type:

from my_package.my_module import do_something

do_something()
do_something_else()  # My new line

I love that I can simply Alt+Enter on do_something_else and it gets imported. But what happens is this:

from my_package.my_module import do_something, do_something_else

do_something()
do_something_else()

While what I would like to happen is this:

from my_package.my_module import do_something
from my_package.my_module import do_something_else

do_something()
do_something_else()

I looked into the settings, but none of the ones I saw seemed right.

These are the ones I've looked at for now:

Editor > Code Style > Python

Editor > General > Code Folding

Editor > General > Auto Import

Where else can I look? Seems like something which should be possible. Maybe one of the options above is the one I'm looking for, but just didn't understand was the right one?

like image 756
André C. Andersen Avatar asked Oct 18 '22 11:10

André C. Andersen


2 Answers

This can now be done easily:

Settings > Code Style > Python > Imports

Then under Structure of "from" imports check the Always split imports option.

Image of import settings

like image 128
ruohola Avatar answered Oct 20 '22 10:10

ruohola


Currently there does not seem to be a way to do this, but there is an open ticket that you can up vote if you would like to have this feature.

https://youtrack.jetbrains.com/issue/PY-20100

like image 32
psarka Avatar answered Oct 20 '22 11:10

psarka