Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

What is the our Python import preference? #64

Open
odscjames opened this issue May 16, 2018 · 4 comments
Open

What is the our Python import preference? #64

odscjames opened this issue May 16, 2018 · 4 comments

Comments

@odscjames
Copy link
Contributor

A)

import os
...
x = os.path.join(...)

B)

from os.path import join
...
x = join(...)

Discuss!

I think I prefer A? It makes it clearer when reading code what is being called, without having to check the imports, which might be a long way away from the code.

I don't think PEP8 covers this? (It says prefer on separate lines, prefer absolute imports where possible, and no wild card (*) imports).

@BibianaC
Copy link
Member

In the case of os, I prefer A.

If it would be something else like datetime I would prefer:
from datetime import datetime (or any other)

instead of
import datetime
and then having to do datetime.datetime

@odscjames
Copy link
Contributor Author

Is there some rules you have for when you prefer A or B?

I've just found issues with circular imports when using type B and now I think I need to go and read up more on how Python imports work, as I'm clearly missing some details!

@BibianaC
Copy link
Member

@odscjames
Copy link
Contributor Author

That's an interesting read, thanks!

As far as I can tell, PEP8 doesn't cover my original question. There is a section on "When importing a class from a class-containing module, ...." that touches on this, but it doesn't actually express a preference!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants