Michael
Michael

Reputation: 13914

Most pythonic way to import all objects in a module as their name in the module

When you import a module, python protects the namespace by importing all objects in that module as module.objectname instead of objectname. import module.objectname as objectname will import the object as its original name in the module, but writing out every object in this manner would be tedious for a large module. What is the most pythonic way to import all objects in a module as their name within the module?

Upvotes: 5

Views: 2196

Answers (2)

John La Rooy
John La Rooy

Reputation: 304147

You only need to use this form

import module.objectname as objectname

If you wish to alias the objectname to a different name

Usually you say

from module import objectname, objectname2, objectname3

There is no "Pythonic" way to import all the objects as from module import * is discouraged (causes fragile code) so can hardly be called Pythonic

Upvotes: 2

aorcsik
aorcsik

Reputation: 15552

This would import everything from modules as their name:

from module import *

But it's not really good practice. Import only what is really needed and use PEP8 tests for your code.

Upvotes: 8

Related Questions