[I tried using Pythonista3] Importing my own module

What is a module?

A module is a file that contains Python definitions and statements. (The suffix of the file name is .py) The following files can also be handled as modules.

HelloWorld.py


print( “HelloWorld” )

What is import?

As an example, import the ʻos` module of the standard library.

import_test.py


import os
print( type( os ) )
# <class ‘module’>
print( os.getcwd() )
# /private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/Test

As mentioned above, you can import by doing ʻimport . Modules are imported as objects of type module`.

Import your own module

directory

  • iCould - Test - MyFunc - MyCalc.py - MyCalcTest.py

We will proceed with the above directories. Import the MyCalc.py module with MyCalcTest.py Try using the functions in the MyCalc.py module.

MyCalc.py


'''
Self-made calculation module
'''
def sum ( v1, v2 ) :
	'''
Addition
	'''
	return v1 + v2
	
def sub ( v1, v2 ) :
	'''
Subtraction
	'''
	return v1 - v2

MyCalcTest.py


import MyFunc.MyCalc

v1 = 10
v2 = 5

res1 = MyFunc.MyCalc.add( v1, v2 )
print( "%d + %d = %d" %( v1, v2, res1 ) )
#>> 10 + 5 = 15

res2 = MyFunc.MyCalc.sub( v1, v2 )
print( "%d - %d = %d" %( v1, v2, res2 ) )
# 10 - 5 = 5

I was able to use the functions of the MyCalc.py module like this.

Easy access to functions in your own module

It's a hassle to write MyFunc.MyCalc.add every time.

Import object

Object specification

You can import an object with from <module name> import <object name>.

import_test.py


from MyFunc.MyCalc import add, sub

v1 = 10
v2 = 5

res1 = add( v1, v2 )
print( "%d + %d = %d" %( v1, v2, res1 ) )

res2 = sub( v1, v2 )
print( "%d - %d = %d" %( v1, v2, res2 ) )

All objects

All objects can be imported using the wildcard *.

import_test.py


from MyFunc.MyCalc import *

v1 = 10
v2 = 5

res1 = add( v1, v2 )
print( "%d + %d = %d" %( v1, v2, res1 ) )

res2 = sub( v1, v2 )
print( "%d - %d = %d" %( v1, v2, res2 ) )

Conclusion

Depending on the directory hierarchy, the imported path name may be long. It is better to specify the object instead of using it as it is. Does wildcard exist in the namespace to be very easy to use? It may be better to refrain from it because it is unknown.

Recommended Posts

[I tried using Pythonista3] Importing my own module
[I tried using Pythonista 3] Introduction
I tried learning my own dataset using Chainer Trainer
I tried using parameterized
I tried using argparse
I tried using mimesis
I tried using anytree
I tried using aiomysql
I tried using Summpy
I tried using coturn
I tried using Pipenv
I tried using matplotlib
I tried using "Anvil".
I tried using Hubot
I tried using openpyxl
I tried using Ipython
I tried using PyCaret
I tried using cron
I tried using the Datetime module by Python
I tried using ngrok
I tried using face_recognition
I tried using Jupyter
I tried using PyCaret
I tried using Heapq
I tried using doctest
I tried using folium
I tried using jinja2
I tried using folium
I tried using time-window
I tried to publish my own module so that I can pip install it
I tried using Random Forest
I tried using BigQuery ML
I made my own language. (1)
I tried using Amazon Glacier
I tried using git inspector
[Python] I tried using OpenPose
I made my own language (2)
I made my own AML
I tried using magenta / TensorFlow
I tried using AWS Chalice
I tried using Slack emojinator
I tried using Rotrics Dex Arm # 2
I tried using Rotrics Dex Arm
I tried using GrabCut of OpenCV
I tried using Thonny (Python / IDE)
I tried server-client communication using tmux
I tried reinforcement learning using PyBrain
I tried deep learning using Theano
Somehow I tried using jupyter notebook
[Kaggle] I tried undersampling using imbalanced-learn
I tried shooting Kamehameha using OpenPose
I tried using the checkio API
[Python] I tried using YOLO v3
I tried asynchronous processing using asyncio
I tried to analyze my favorite singer (SHISHAMO) using Spotify API
I made my own research tool using the legal API [Smart Roppo]
I tried using Amazon SQS with django-celery
I tried using Azure Speech to Text.
I tried using Twitter api and Line api
I tried using YOUTUBE Data API V3
I tried using Selenium with Headless chrome