Easily use your own functions in Python

First, create your own function.

myplot.py


import matplotlib.pyplot as plt

def myplot(x):
    plt.plot(x, 'o-')
    plt.show()

if __name__ == '__main__':
    x = [i**2 for i in range(20)]
    myplot(x)

I like to make the file name and the function name the same here.

figure_1.png

Put this in your own function collection folder.

mymodule/
  __init__.py
  myplot.py

If you keep this file inside,

__init__.py


from myplot import myplot

You can import myplot from mymodule without stress.

test_myplot.py


from mymodule import myplot

x = [(-0.5)**i for i in range(20)]
myplot(x)

figure_1.png

However, in this case, the executed test_myplot.py must be in the same folder as mymodule.

mymodule
test_myplot.py

Therefore, collect your own function collection in a specific folder.

/home/okadate/pyfiles/
  mymodule
  mymodule2

If you put test_myplot.py in a free position like this,

test_myplot.py


import sys; sys.path.append('/home/okadate/pyfiles')
from mymodule import myplot

x = [i/(5.0+i) for i in range(30)]
myplot(x)

figure_1.png

You can use it!

It seems that there are other methods such as packaging. I would like to try it if it is easy. ..

Recommended Posts

Easily use your own functions in Python
Use the CASA Toolkit in your own Python environment
[Python] logging in your own module
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Get your own IP address in Python
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use profiler in Python
Use Python in your environment from Win Automation
Don't use readlines () in your Python for statement!
Import your own modules in Grasshopper's Python development
python: Use your own class for numpy ndarray
Let's use python janome easily
Easily format JSON in Python
Create your own Big Data in Python for validation
Use let expression in Python
Use Measurement Protocol in Python
Use callback function in Python
Create your own Random Dot Stereogram (RDS) in Python.
Use parameter store in Python
Try to improve your own intro quiz in Python
Use HTTP cache in Python
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Overriding library functions in Python
Use Spyder in Python IDE
[Road to intermediate Python] Define in in your own class
Python functions learned in chemoinformatics
Try sorting your own objects with priority queue in Python
How to use pyenv and pyenv-virtualenv in your own way
Comparison of how to use higher-order functions in Python 2 and 3
[Python] Make your own LINE bot
[python] Manage functions in a list
Use fabric as is in python (fabric3)
How to use SQLite in Python
Use rospy with virtualenv in Python3
Using global variables in python functions
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Dynamically define functions (methods) in Python
Use OpenCV with Python 3 in Window
[Python] When you want to import and use your own package in the upper directory
How to use functions in separate files Perl and Python versions
Python functions
Create your own graph structure class and its drawing in python
Specify your own class in class argument and return type annotation in Python
[Python3] Dynamically define global variables in functions
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
[Python] Package and distribute your own modules
Pharmaceutical company researchers summarized functions in Python
[Python] Register your own library on PyPI
Easy way to use Wikipedia in Python
Until you install your own Python library
Import your own functions on AWS Glue