Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing all functions from a package: "from .* import *"

Goal

I want to be able to import (on the __init__.py) all functions from every single file inside my package.

Usage

For example in this folder structure.

manage.py - scripts/    -- __init__.py    -- tests.py    -- deploy.py 

I am currently doing the following:

manage.py:

from scripts import * 

script/init.py:

from .tests import * from .deploy import * 

But, every time I add another file to the package I have to add an import line on script/__init__.py, which is kind of annoying.

like image 971
CESCO Avatar asked Jan 18 '16 12:01

CESCO


People also ask

What is import * in Python?

It imports everything (that is not a private variable, i.e.: variables whose names start with _ or __ ), and you should try not to use it according to "Properly importing modules in Python" to avoid polluting the local namespace. It is enough, but generally you should either do import project.

What is __ import __ in Python?

__import__() Parameters name - the name of the module you want to import. globals and locals - determines how to interpret name. fromlist - objects or submodules that should be imported by name. level - specifies whether to use absolute or relative imports.

What is the difference between import and import * in Python?

Artturi Jalli. The difference between import and from import in Python is: import imports the whole library. from import imports a specific member or members of the library.

Should you import * Python?

Using import * in python programs is considered a bad habit because this way you are polluting your namespace, the import * statement imports all the functions and classes into your own namespace, which may clash with the functions you define or functions of other libraries that you import.

What is the difference between importing a module and a function?

Whether you import a module or import a function from a module, Python will parse the whole module. Either way the module is imported. "Importing a function" is nothing more than binding the function to a name. In fact import module is less work for interpreter than from module import func.

How to import a package using import statement in Java?

The import statement is optional in Java. If you want to use class/interface from a certain package, you can also use its fully qualified name, which includes its full package hierarchy. Here is an example to import package using import statement. The same task can be done using fully qualified name as follows:

How to use a class/interface from a specific package?

If you want to use class/interface from a certain package, you can also use its fully qualified name, which includes its full package hierarchy. Here is an example to import a package using the import statement.

What is the use of import in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.


2 Answers

You can do it, manually, but you shouldn't.

Why you really do not want to do this:

You'll end up with a namespace where understanding what is what and from where it came from will be extremely hard, with difficulty increasing as the size of the overall project does. Appart from being completely unintuitive for Python, think of anybody else that might view your code or even worse, think about yourself re-reading it after 1 month and not remembering what's going on. You don't need that in your life.

In addition to that, any functions you expose to the importer that might overlap with other functions in other modules are going to get shaddowed by the most recent one imported. As an example, think of two scripts that contain the same function foo() and watch what happens.

>>> from scrpt1 import * >>> foo() Script 1 >>> from scrpt2 import * >>> foo() Script 2 

Don't need that in your life either. Especially when it is so easy to bypass by being explicit.


Here are some related lines from the text contained in import this:

Explicit is better than implicit.

Be explicit about the place where your functions are defined in. Don't "spaghetti" your code. You'll want to hit yourself in the future if you opt in for a mesh of all stuff in one place.

Special cases aren't special enough to break the rules.

Really self explanatory.

Namespaces are one honking great idea -- let's do more of those!

"more of those!", not less; don't miss out on how wonderful namespaces are. Python is based on them; segregating your code in different namespaces is the foundation of organizing code.

like image 183
Dimitris Fasarakis Hilliard Avatar answered Oct 08 '22 01:10

Dimitris Fasarakis Hilliard


  1. importlib allows you to import any Python module from a string name. You can automate it with going through the list of files in the path.

  2. It's more pythonic to use __all__. Check here for more details.

like image 39
Dhia Avatar answered Oct 08 '22 02:10

Dhia