What are all the ways to import modules in Python?
Asked Answered
K

5

6

I've done some research, and I came across the following article: http://effbot.org/zone/import-confusion.htm. While this seems to be a great guide, it was written in 1999, a while back. I'm am using Python 3.4.3, so I am thinking that some things have changed, which worries me, because I don't want to learn what is not applicable. Therefore, in Python 3, what are all of the ways to import packages and modules, in detail? Which ways are the most common and should be used above others?

Kelvin answered 28/8, 2015 at 4:53 Comment(0)
A
4

The only ways that matter for ordinary usage are the first three ways listed on that page:

  • import module
  • from module import this, that, tother
  • from module import *

These haven't changed in Python 3. (Some of the details about where Python looks for the module.py file to load module have been tweaked, but the behavior of the import itself still works as described on the page you linked.)

One thing has been added, before Python 3 but since that article. That is explicit relative imports. These let you do things like from ..module import blah. This kind of import can only be used from inside a package; it lets modules in a package refer to other modules in the same package in a way that is relative to the package (i.e., without having to specify how to import the top-level package). You can read the details in PEP 328. Even this, though, is basically just a new variation on the from module import blah style syntax mentioned on the page you linked to.

__import__ also still works in Python 3. This is an internal function that you only would need to use if doing something rather unusual. The same applies to various functions in the importlib module (and the deprecated imp module). The exact level of wizardliness of these importing functions varies from one to another, but for ordinary usage of "I just want to import this module and use it", you essentially never need to use them. They're only needed if you want to do something like dynamically import a module whose name isn't known until runtime.

Ardenardency answered 28/8, 2015 at 5:1 Comment(3)
@Miles - More info regarding relative imports can be found below : #73352Turbot
@Ardenardency Okay, this makes sense. However, I am having one particular problem. I have a package with two modules in it. I have another module outside of the package, and am trying to access the functions inside of both of the modules within the package. At the top of my current module, I type import package_name, yet I still don't have access to the functions in the sub-modules. What am I doing wrong?Kelvin
@MilesDavis: You should search for similar question on this site. If you don't find the answer, you should ask a separate question explaining the details of your problem. You asked a very general question here; if you have a specific problem with a specific setup, that warrants a separate question.Ardenardency
B
4

The Zen of Python gives you some hints:

>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

So given the simple, obvious method is: import module_name and it preserves namespaces I would suggest that while there are several import methods as you can see from the python3 manual entry and you can extend them by overriding the __import__() method or by rolling your own I would say stick with it until you have a good reason not to.

The fact that__import__() is surrounded by double underscores is also a hint to leave it alone.

If you are looking to understand the design decisions behind the import mechanisms then start with the manual then follow up into the PEPs 302 & 420 are good starting points.

Backswept answered 28/8, 2015 at 5:11 Comment(0)
I
1

I think import as tuple would be much better for readability and Maximum Line Length(pep8)

The import statement has two problems:

  • Long import statements can be difficult to write, requiring various contortions to fit Pythonic style guidelines.
  • Imports can be ambiguous in the face of packages; within a package, it's not clear whether import foo refers to a module within the package or some module outside the package.
  • golang language have the same thing for that

so would more prefer import kinda this

from package import (x, y)

instead of this

from authentication.views import SignupView, LoginView, VerificationView, SignupDetailView

https://legacy.python.org/dev/peps/pep-0328/

Irreducible answered 11/3, 2019 at 12:10 Comment(0)
H
0

We can import modules in Python using the following ways

  • import module
  • from module import function
  • from module import *

Although using from module import * is not a good practice, because of readability: Other programmer cannot understand what all are actually used in the current module. Memory overload: All are loaded in to memory. Best practices for using import in a module.

Hiroko answered 28/8, 2015 at 12:50 Comment(0)
C
0

Say you have python modules (mymod1.py, mymod2.py files containing different functions) inside mypkg package (folder having init.py file, it can be an empty file).

#mymod1.py
def add_fun(a,b):
    return a+b
def sub_fun(a,b):
    return a-b
def mul_fun(a,b):
    return a*b
def div_fun(a,b):
    return a/b

#mymod2.py
def fun1(...):
    ........
    ........
def fun2(...):
    ........
    ........

Following are different ways to import:

from mypkg.mymod1 import *  #import all the function from mymod1 
add_fun(10, 20)
mul_fun(10, 2)

from mypkg.mymod1 import add_fun,div_fun  #import only needed functions from mymod1 
add_fun(10, 20)
div_mul(10, 2)

from mypkg import mymod1  #import mymod module
mymod1.add_fun(10, 20)
mymod1.mul_fun(10, 2)

import mypkg    #import package and use different models inside it
mypkg.mymod1.add_fun(10, 20)
mypkg.mymod1.mul_fun(10, 2)
Cradlesong answered 31/5, 2021 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.