Import python script

problem

A python script with a filename that you wouldn't normally be able to import, For example, zz-myscript-01.py I want to import a function implemented inside such a guy. But,

import zz-myscript-01

Cannot be written like (Syntax Error)

I want to load it without changing the name.

Solution

Use `ʻimp. For example, to load the above script with the name mymodule`` OK if you do the following.

def customImport():
  import imp
  global mymodule
  fp, name, desc = imp.find_module("zz-myscript-01")
  mymodule = imp.load_module("mymodule", fp, name, desc)

customImport()

Why it works

Because `ʻimp`` is an interface to the Python import statement implementation, Because I use it to import my own.

reference

http://docs.python.jp/2/library/imp.html#examples-imp

Recommended Posts

Import python script
Script python file
python script skeleton
Python script profiling
Summarize Python import
Python from or import
python original module import
DynamoDB Script Memo (Python)
Import tsv with Python
How Python module import works
Python
POST json with Python3 script
Dynamically import scripts in Python
Import vtk with brew python
[Python] Another way to import
Python # Snap7 Libray Import problem
Execute Python script from batch file
Python script for ldapsearch base64 decode
[Python] The stumbling block of import
Working with LibreOffice in Python: import
python> link> from __future__ import absolute_import
python> Exit processing> import sys / sys.exit ()
[python] Copy script to generate copy log
About Python, from and import, as
[Python] How to import the library
Execute Python Script during CodeSys # RunTime
AWS Layer Creation Script for python
[Lambda] Make import requests available [python]
Load Mac Python import MySQL db
Python import directory order (on anaconda)
Launch a Python script as a service
Write a batch script with Python3.5 ~
[python] Script that (should) update pwsh
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
Python memo
Python comprehension
Execute Python script with cron of TS-220
Python Singleton
Python basics ④
Python Memorandum 2
Python increment
Pin current directory to script directory in Python
atCoder 173 Python
[Python] function
Python installation
python tips
Sample script to trap signals in Python
About import
Installing Python 3.4.3.
Try python