-
-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First-party library (incorrectly?) detected as third-party depending on working directory #1497
Comments
Simpler test case:
main.py
When isort is run in the same directory as
When isort is not run in the same directory as
|
Source of the problem: https://github.com/PyCQA/isort/blob/develop/isort/settings.py#L388
|
Like most command line tools, isort uses the current working directory as a fallback if more explicit information isn't provided. See: https://pycqa.github.io/isort/docs/configuration/config_files/. Creating a config file at the root of your project (where you want lookup to occur) or setting your src paths manually should resolve this issue for you. Let me know if you run into any issues with this, Thanks! ~Timothy |
I don't think the definition of a first party library depends on where isort is run but on where the source file is located. I haven't upgraded isort for several months but the old version does not have this problem. For information, with my test case, isort detects |
@char101 please take a look at the upgrade guide here: https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0/#module-placement-changes-known_third_party-known_first_party-default_section-etc and associated issue here:#1147. The old approach had much bigger problems, but can still be used if desired as mentioned in the upgrade guide. That said, it is easy to tell isort exactly what you want from source paths etc. The only way to do what the old approach did, is to rely on a lot of very very fragile magic like what is loaded in the venv. |
https://pycqa.github.io/isort/docs/upgrade_guides/5.0.0/#module-placement-changes-known_third_party-known_first_party-default_section-etc links to #1147, of which the 6th point is
So shouldn't |
Yes, and your example shows that it is. However, think about it, if you run it in a directory other than that, isort has no explicit way to know then your project starts or ends. Just because you have a config.py at the same level, doesn't mean that it isn't nested 10 levels in your source tree and so using it to categorize would be inappropriate. |
Ok I can understand that. Still, using the currect directory as the project root has this unintended consequence: Let's assume this project structure
Output:
IMO unless |
I don't think this is a productive line of reasoning. Sure, there are reasons why defaulting to the path could give you the right answer, there are also reasons why defaulting to the current working directory could give you the right answer. After all, it is very common usage to have isort as a CI/CD tool that runs in the root of a project with many nested modules. It is also very common for users to run it in the root of the project. For both of these cases current working directory gives you the right answer, and path based does not. It is easy to come up with examples for both behaviours. However, if isort as a project switches back and forth for each example it will just make both use cases unpredictable. I think a better line of discussion: what things are there in your project structure that isort can use to know when it reached the top your project so it doesn't need to guess? And, is there a reason you cant put an isort configuration at the root of your project, again, so isort doesn't need to guess? |
The thing is python are often used to write scripts. In this case the user doesn't think it as a project. First one creates |
@char101 if you have a pile of scripts, in a larger application context, there is no reason not to take one second to touch an .isort.cfg file where you wont those scripts to start. Also, in that context, you should be fine running isort in that same directory. If you are running isort via an editor it is just as likely it is configured to CD into the root of your project before running isort, in which case CWD behaviour would be helpful. Again, this line of discussion is just going to be going in circles, because it's a question of what is the best default, when both or equally valid - and it is very unlikely that it will be changed for the duration of the 5.0.0 release. I'm sorry the default isn't your preferred one, but I'd rather focus on real life use cases that you are running into, and how the project can offer solutions to them. |
I don't have any problem with it currently. Maybe because it used to just works, so it feels like a bit of a regression. Ok so creating an empty The way I am using isort currently is in a MessagePack RPC server (with |
Adding
to my previously empty |
I'm going to close this, as the original opening question is now resolved. @char101 and @bersbersbers please feel empowered to open additional issues if I missed anything. Thanks! ~Timothy |
I cannot post my a fully-contained working environment here, but this is my example code, and maybe it's enough to reproduce or at least understand the issue.
This is
/home/bers/myproj/myproj/tool/bug.py
:What this outputs is this:
So you see that
isort
detectsmyproj
as a 3rd-party library depending on the working directory - is this intended?It may be relevant that I usually do
pip install -e .
from/home/bers/myproj
when setting up environments - this is what makesimport myproj
possible from within/home/bers/myproj/myproj/tool/bug.py
.The text was updated successfully, but these errors were encountered: