Easy way to customize Python import

It can be used when you want to import dynamically due to some special circumstances.

https://docs.python.org/ja/3/library/importlib.html

Everything is written in the document, but it's a little difficult to understand, so if you want to use it quickly, I think it's a good idea to put the following code in the beginning.

import importlib.machinery
import sys


class PackageFinder(importlib.machinery.PathFinder):

    @classmethod
    def find_spec(cls, fullname, path=None, target=None):
        if not path:
            path = [] 
        #Specify the list of directories you want to find in path
        path.append("/path/to/package/root")
        return importlib.machinery.PathFinder.find_spec(fullname, path, target)

sys.meta_path.append(PackageFinder)

By dynamically switching the path of the above code, you can also do acrobatic things such as synchronously reading a specific directory, downloading and reading a remote directory (recommended). I don't have one).

In addition, there is also sys.path_hooks, so this seems to be used when you want to customize the behavior of the module import corresponding to it by taking the path as an argument instead of adding the path.

Recommended Posts

Easy way to customize Python import
[Python] Another way to import
Easy way to use Wikipedia in Python
Easy way to use Python 2.7 on Cent OS 6
An easy way to call Java from Python
Easy way to rename files
Easy way to scrape with python using Google Colab
An easy way to create an import module with jupyter
[Python] How to import the library
[Python] An easy way to visualize energy data interactively [plotly.express]
Easy to use Jupyter notebook (Python3.5)
Easy Python to learn while writing
PyArmor ~ Easy way to encrypt and deliver python source code ~
[Python Tutorial] An Easy Introduction to Python
An easy way to hit the Amazon Product API in Python
Easy way to round off to the nearest whole number with python3
[Python] How to use import sys sys.argv
A way to understand Python duck typing
Updated to Python 2.7.9
Python is easy
Python module import
Import python script
"Backport" to python 2
Summarize Python import
[Python] Easy introduction to machine learning with python (SVM)
The fastest way for beginners to master Python
Python for super beginners Python for super beginners # Easy to get angry
Summary of how to import files in Python 3
Import Excel file from Python (register to DB)
The easiest way to synthesize speech with python
Python list comprehensions that are easy to forget
Easy way to plot HR diagram using astroquery
A clever way to time processing in Python
Excel X Python The fastest way to work
The easiest way to use OpenCV with python
Easy way to load CPU / memory on Linux
Introduction to Python with Atom (on the way)
Difference in object ID due to import in Python
[Small story] Easy way to convert Jupyter to PDF
From easy git installation to docker startup python
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
[Reintroduction to python] How to import via the parent directory
Python from or import
Rewrite Python2 code to Python3 (2to3)
python original module import
Easy to use Flask
How to install python
python decorator to retry
Introduction to Python language
An easy way to pad the number with zeros depending on the number of digits [Python]
Introduction to OpenCV (python)-(2)
It's not easy to write Python, it's easy to write numpy and scipy
Note to daemonize python
unable to import django
Introducing Python 2.7 to CentOS 6.6
Easy to use SQLite3
What to do when "cannot import name xxx" [Python]
[Introduction to Udemy Python3 + Application] 68. Import statement and AS
Connect python to mysql