Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import multiple items from a module and rename them in Python?

Tags:

python

module

I want to import atan and degree from math and rename them both.

I have tried using this:

from math import atan,degree as t,z

But that gives ImportError: cannot import name 'z'.

I did a number of Google searches for "import multiple modules and rename", but all were fruitless. The Python manual hasn't helped - the page on imports doesn't explain this (as far as I can tell).

How can I import multiple items from a module, and rename them?

like image 815
Tim Avatar asked Apr 25 '15 12:04

Tim


People also ask

How do I import multiple functions from a Python module?

Import multiple modulesYou can write multiple modules separated by commas after the import statement, but this is not recommended in PEP8. Imports should usually be on separate lines. If you use from to import functions, variables, classes, etc., as explained next, you can separate them with a comma.

Can you import multiple files in Python?

The importFolder (R)/ import_file (Python) function can be used to import multiple local files by specifying a directory and a pattern. Example patterns include: pattern="/A/.

What is __ import __ in Python?

__import__() . This means all semantics of the function are derived from importlib. __import__() . The most important difference between these two functions is that import_module() returns the specified package or module (e.g. pkg. mod ), while __import__() returns the top-level package or module (e.g. pkg ).

What is the use of from module name import Object name?

Using from to import module You can import only a small part of the module i.e., only the required functions and variable names from the module instead of importing full code. When you want only specific things to be imported, you can make use of “from” keyword to import what you want.


2 Answers

You have to use the as for each item:

from math import atan as t, degree as z

This imports and renames them all.

like image 161
Tim Avatar answered Oct 11 '22 10:10

Tim


The Python Reference Manual does, in fact, cover this. It says, in its description for the import statement:

import_stmt     ::=  "import" module ["as" name] ( "," module ["as" name] )*
                     | "from" relative_module "import" identifier ["as" name]
                     ( "," identifier ["as" name] )*
                     | "from" relative_module "import" "(" identifier ["as" name]
                     ( "," identifier ["as" name] )* [","] ")"
                     | "from" module "import" "*"

Now, this notation is a little confusing at first glance, but as spend time with programming languages you will become more familiar with it. It is commonly refered to as "BNF" (which stands for Backus-Naur Form). Most programming language references will use some version of it.

From the sample above, we see the following symbols that could do with some explanation:

  • Vertical bar or Pipe character ( | ) -- this is used to separate alternatives
  • The asterisk / star character ( * ) -- this means that the preceding (usually enclosed statement) is repeated zero or more times
  • Square brackets ([ and ]) -- these indicate that the enclosed portion which occurs is optional, so included zero or one times.
  • Parenthesis (( and )) -- these are used to group statements for the asterisk to take affect on

Cutting down the reference above to what you seem interested in, we have:

"from" relative_module "import" identifier ["as" name]
                     ( "," identifier ["as" name] )*

TL;DR Which, for your example given, leads to the legal statement being

from math import atan as t, degree as z
like image 10
TZHX Avatar answered Oct 11 '22 10:10

TZHX