PyCharm's refactoring to move a package into a new package breaks imports

Answered

Hi,

I have a question about how to move a package into another package in PyCharm. When I use the refactor --> Move option and include the checkbox on check references, I get very inconsistent and often erroneous refactoring of the import statements in other files that used that package.

For example,

Let's say I have a package general_utils with module sql_utils.py containing a class SQL.

In my source code, I have an import statement like:

`from general_utils.sql_utils import SQL`

Then I use PyCharm's refactoring to move general_utils inside a new package utils.

In some files with that import, I will get the proper

`from utils.general_utils.sql_utils import SQL`

But bizarrely, what is more common in most files with that import is that it is refactored to:

`from utils.general_utils import SQL`

So for some reason moving a package into a package loses the name of the module when I refactor it.

Is there any fix for this? This is obviously not a problem with IntelliJ and Java, but I confirmed that the same problem also occurs with IntelliJ and the Python plugin.

I also thought it could be an indexing error, so I invalidated my cache and rebuilt it, but that did not fix it.

 

4
8 comments

Hi Hendrikkvh! Sound like a known issue: PY-26091. Unfortunately the fix is not yet ready - please feel free to vote for this issue.

0

please fix this, it's very annoying

0

Hello @Mateu Batle, 

The issue was specific for the Python plugin for IntelliJ IDEA and not relevant anymore.

please submit a new ticket at https://youtrack.jetbrains.com/issues/PY   and attach a code example to reproduce the issue. 

 

 

 

 

 

0

Thanks for your response Antonina Belianskaya. But I can confirm it happens with pycharm when doing Refactor Move. I have Pycharm Professionl version 2021.1 a built from 6th April 2021

0

Mateu Batle, 

 

Could you please submit a ticket with a code example? Thank you! 

0

PyCharm breaks imports on source file / package moves when the file and class name are the same. Google AI search points here. Is there a better ticket for it? this is very unforgiving in large projects (!)

This happens in the most latest versions of PyCharm.

0

It sounds much like the original description at the top of this issue, the underlying semantics of the issue seem to converge.

0

Hi Matan

 

This post is quite old, I tried to replicate based on the original description in the latest 2025.2.1.1 and I was unable to replicate.

Would you please raise in youtrack if you are able to replicate with the list of steps and possibly sample project and IDE Logs.

The logs can be provided from Help -> Collect Logs and diagnostics data

 

Kind regards,

 

0

Please sign in to leave a comment.