Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In python, how do you import all classes from another module without keeping the imported module's namespace?

Tags:

python

How do you import classes and methods from another module without retaining the former module namespace?

I am current refactoring some legacy code and am frequently doing imports similar to these.

from legacy_module import ClassA as ClassA from legacy_module import ClassB as ClassB from legacy_module import ClassC as ClassC from legacy_module import methodA as methodA from legacy_module import methodB as methodB 

This is done so that the classes can be referenced as ClassA rather than legacy_module.ClassA.

In python, how do you import all of the classes and methods above in a single statement?

from legacy_module import * as * 
like image 560
Chris Avatar asked Aug 16 '12 02:08

Chris


People also ask

How do I import a class from another module?

Python modules can get access to code from another module by importing the file/function using import. The import statement is that the commonest way of invoking the import machinery, but it's not the sole way. The import statement consists of the import keyword alongside the name of the module.

How do I import one module to another in Python?

To import a module from another path, you first need to import the sys module as well as any other Python modules that you would like to use in your program. The sys module is provided by the Python Standard Library and it provides functions and parameters that are system-specific. The path.

When importing a module into a Python program What is the difference between using the import statement and using the From statement?

The import statement allows you to import all the functions from a module into your code. Often, though, you'll only want to import a few functions, or just one. If this is the case, you can use the from statement. This lets you import only the exact functions you are going to be using in your code.


1 Answers

Use from legacy_module import * as your entire import.

like image 113
Jordan Avatar answered Sep 21 '22 18:09

Jordan