Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple aliases on one-line Python import

Can I import module in Python giving it two or more aliases in one line?

With two lines this works:

from time import process_time as tic
from time import process_time as toc

This doesn't work, but I would want to be able to write something like:

from time import process_time as tic,toc
like image 918
Danny Lessio Avatar asked Apr 07 '16 15:04

Danny Lessio


People also ask

How do you import multiple items into Python?

Import multiple items at once from the same module at once by writing them separated by commas. If a line is too long, you can use parentheses () to break the line.

Can two Python scripts import each other?

You might have code that you want to both run on its own and import from other scripts. In that case, it's usually worthwhile to refactor your code so that you split the common part into a library module. While it's a good idea to separate scripts and libraries, all Python files can be both executed and imported.

Does Python support aliases for module names?

Aliasing ModulesIt is possible to modify the names of modules and their functions within Python by using the as keyword.

Does the order of imports matter in Python?

Import order does not matter. If a module relies on other modules, it needs to import them itself. Python treats each . py file as a self-contained unit as far as what's visible in that file.


2 Answers

You can do that with

from time import process_time as tic, process_time as toc
like image 56
Francesco Avatar answered Oct 12 '22 20:10

Francesco


You could do

from time import process_time as tic

toc = tic
like image 27
Ithilion Avatar answered Oct 12 '22 22:10

Ithilion