Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is mixing 'from x import y' and 'import x' discouraged?

Tags:

python

import

My script contains the following two lines as import-statements:

import os
from os import path

This seems a logical approach for my script: I use os.path very often, so I want to access it as path. On the other side, I need additional methods from os, but very rarely, so it is ok for me to write os.access(...) for example.

Why is this discouraged? pychecker for example complains about this.

like image 911
theomega Avatar asked Jan 29 '26 05:01

theomega


1 Answers

It's usually odd to both import a name from a module, and import the whole module. In this case, you are importing a submodule, so it doesn't seem bad, though most people do just use "os.path" in their function calls.

Just because pychecker doesn't like it doesn't mean you can't do it. Turn off that warning.

like image 121
Ned Batchelder Avatar answered Jan 30 '26 20:01

Ned Batchelder