Cool!! I can import packages in Python in eight ways. How about you?

Keywords: Python pip github git


WeChat official account: Python programming time.
Original link:

Today, I'd like to introduce eight methods of Python package import that I've used.

1. Direct import

A well-known method, which can be imported directly

>>> import os
>>> os.getcwd()

Similarly, let's not go into details

import ...
import ... as ...
from ... import ...
from ... import ... as ...

In general, importing modules with import statements is enough.

But in some special scenarios, there may be other ways to import.

I'll introduce you one by one.

2. Use "import"_

__The import? Function can be used to import modules, and the import statement calls the function. It is defined as:

__import__(name[, globals[, locals[, fromlist[, level]]]])

Parameter introduction:

  • name (required): the name of the loaded module
  • globals (optional): a dictionary containing global variables, which is rarely used. The default value is global()
  • Local (optional): dictionary containing local variables, which are not used in internal standard implementation, and the default value - local()
  • fromlist (Optional): the name of the imported submodule
  • level (Optional): the option of import path, which is - 1 by default in Python 2, means that both absolute import and relative import are supported. Python 3 defaults to 0, which means only absolute import is supported. If it is greater than 0, it indicates the number of relative imported parent directories, i.e. 1 is similar to '.', 2 is similar to '..'.

Examples of use are as follows:

>>> os = __import__('os')
>>> os.getcwd()

If you want to achieve the effect of import xx as yy, just modify the left value

The following example is equivalent to import os as myos:

>>> myos = __import__('os')
>>> myos.getcwd()

As mentioned above, "import" is a built-in function. Since it is a built-in function, the built-in function must exist in "buildins". Therefore, we can also import the modules of os as follows:

>>> __builtins__.__dict__['__import__']('os').getcwd()

3. use the importlib module

importlib is a standard library in Python, which can provide a very comprehensive function.

A simple example:

>>> import importlib
>>> myos=importlib.import_module("os")
>>> myos.getcwd()

If you want to implement the import xx as yy effect, you can

>>> import importlib
>>> myos = importlib.import_module("os")
>>> myos.getcwd()

4. Use imp module

The import module provides some interfaces implemented within the import statement. For example, find module, load module and so on (the import process of a module includes the steps of module search, load and cache). This module can be used to simply implement the built-in import function:

>>> import imp
>>> file, pathname, desc = imp.find_module('os')
>>> myos = imp.load_module('sep', file, pathname, desc)
>>> myos
<module 'sep' from '/usr/lib64/python2.7/os.pyc'>
>>> myos.getcwd()

Since python 3, the built-in reload function has been moved to the imp module. Since Python 3.4, the import module has been rejected and is no longer recommended. Its functions have been moved to the importlib module. That is, starting with Python 3.4, the importlib module is a combination of the previous import module and the importlib module.

5. Use execfile

In Python 2, there is an execfile function that can be used to execute a file.

The syntax is as follows:

execfile(filename[, globals[, locals]])

There are several parameters:

  • Filename: filename.
  • globals: variable scope, global namespace, if provided, must be a dictionary object.
  • Locales: variable scope, local namespace, if provided, can be any mapping object.
>>> execfile("/usr/lib64/python2.7/")
>>> getcwd()

6. Execute with exec

execfile can only be used in Python 2, which has been removed from Python 3.x.

But the principle is worth learning. You can use open... Read to read the contents of the file, and then use exec to execute the module.

An example is as follows:

>>> with open("/usr/lib64/python2.7/", "r") as f:
...     exec(
>>> getcwd()

7. import_from_github_com

There is a package called import from github com. It is easy to know from its name that it is a package that can be downloaded, installed and imported from github. In order to use it, all you need to do is install it first using pip as follows.

$ python3 -m pip install import_from_github_com

This package uses the new introduction hook in PEP 302, which allows you to import packages from github. What this package does is actually install it and add it locally. You need Python 3.2 or later, and git and pip are both installed to use this package.

pip must be a newer version. If not, please execute the following command to upgrade.

$ python3 -m pip install --upgrade pip

Once the environment is ok, you can use import from GitHub com in the Python shell

An example is as follows

>>> from github_com.zzzeek import sqlalchemy
Collecting git+
Cloning to /tmp/pip-acfv7t06-build
Installing collected packages: SQLAlchemy
Running install for SQLAlchemy ... done
Successfully installed SQLAlchemy-1.1.0b1.dev0
>>> locals()
{'__builtins__': <module 'builtins' (built-in)>, '__spec__': None,
'__package__': None, '__doc__': None, '__name__': '__main__',
'sqlalchemy': <module 'sqlalchemy' from '/usr/local/lib/python3.5/site-packages/\
'__loader__': <class '_frozen_importlib.BuiltinImporter'>}

After reading the source code of import from GitHub com, you will notice that it does not use importlib. In fact, the principle is to use pip to install the packages that are not installed, and then use Python's UPU import uuupu() function to introduce the newly installed modules.

8. Remote import module

I'm in this article( In depth discussion on Python import mechanism: implementation of remote import module )At the end of this paper, we have realized the module content reading from the remote server manually and successfully imported the module locally.

There are many specific contents, you can click this link Conduct in-depth learning.

The sample code is as follows:

# Create a new py file (my importer. Py) with the following contents
import sys
import importlib
import urllib.request as urllib2

class UrlMetaFinder(
    def __init__(self, baseurl):
        self._baseurl = baseurl

    def find_module(self, fullname, path=None):
        if path is None:
            baseurl = self._baseurl
            # If the url is not the original definition, it will be returned directly and does not exist
            if not path.startswith(self._baseurl):
                return None
            baseurl = path

            loader = UrlMetaLoader(baseurl)
            return loader
        except Exception:
            return None

class UrlMetaLoader(
    def __init__(self, baseurl):
        self.baseurl = baseurl

    def get_code(self, fullname):
        f = urllib2.urlopen(self.get_filename(fullname))

    def get_data(self):

    def get_filename(self, fullname):
        return self.baseurl + fullname + '.py'

def install_meta(address):
    finder = UrlMetaFinder(address)

And open the http service on the remote server (for convenience, I only demonstrate locally), and manually edit a python file named my_info. If the import succeeds, ok will be printed.

$ mkdir httpserver && cd httpserver
$ cat><EOF
$ cat
$ python3 -m http.server 12800
Serving HTTP on port 12800 ( ...

Everything is ready, the verification begins.

>>> from my_importer import install_meta
>>> install_meta('http://localhost:12800 / ', register finder with sys.meta path
>>> import my_info  # Print ok, indicating successful import
>>>  # Verify that variables can be obtained

OK, all eight methods have been introduced to you. For ordinary developers, it's enough to master the import method. For those who want to develop their own framework, it's very necessary to study "import" and "importlib" in depth.

Posted by kenshintomoe225 on Sun, 17 May 2020 20:51:12 -0700