Load the module with the same name in another location

You can load a module with the same name in another location by creating a module like the one below. Please use it when you cannot tamper with PYTHONPATH. (Py2 / 3 operation confirmed)

import os
import sys

if sys.version_info[0]>=3:
    from importlib import reload

sys.path.insert(0,os.path.abspath(os.path.join(__file__,'..','..','..','real')))
import hello
reload(hello)

full sample: https://github.com/cielavenir/import-transfer

Recommended Posts

Load the module with the same name in another location
Memorandum (Add name only to people with the same surname in the list)
Load the network modeled with Rhinoceros in Python ③
(Note) Importing Excel with the same column name
Load the network modeled with Rhinoceros in Python ②
Load the network modeled with Rhinoceros in Python ①
Dynamically load the module.
When a local variable with the same name as a global variable is defined in the function
When the variable name conflicts with the Devaga command in pdb
Access the C structure field with the name reserved in Go.
Automatically rename a node with the same name found while working
Put the module in the Pepper app
Get the host name in Python
Master the weakref module in Python
The story of making a tool to load an image with Python ⇒ save it as another name
Ansible: Shows multi-line commands executed in the shell module with line breaks
Create a record with attachments in KINTONE using the Python requests module
pyenv + pyenv-Automatically load the virtualenv environment in the virtualenv environment
Load the remote Python SDK in IntelliJ
Load csv with duplicate columns in pandas
Behavior when returning in the with block
[Python] Get the variable name with str
Display Python 3 in the browser with MAMP
When changing the table name with flask_migrate
Install the Python module in any directory
Location information data display in Python --Try plotting with the map display library (folium)-
Turn multiple lists with a for statement at the same time in Python
How to get a list of files in the same directory with python
Log in to the remote server with SSH
[Python] Get the files in a folder with Python
Access files in the same directory as the executable
[Automation] Extract the table in PDF with Python
Determine the numbers in the image taken with the webcam
Rewrite the name of the namespaced tag with lxml
Specifying the module loading destination with GAE python
Loop variables at the same time in the template
Load the TensorFlow model file .pb with readNetFromTensorflow ().
Configure a module with multiple files in Django
Load a package created locally with Go Module
Examine Python script bottlenecks with the cProfile module
Mock HTTP requests via the requests module in Responses
matplotlib: Replace the axis itself with another one.
The story that fits in with pip installation
[Note] How to write QR code and description in the same image with python
[Python] Explain the difference between strftime and strptime in the datetime module with an example
A memo of misunderstanding when trying to load the entire self-made module with Python3
[Python] If you create a file with the same name as the module to be imported, an Attribute Error will occur.