Read and use Python files from Python

Introduction

I'm Nagi Goishi, who recently works in Python and scikit-learn. Personally, I'm more of a Ruby fan than Python, but I like the one I'm introducing here in the Python specification.

import Python beginners like me have a strong image of ʻimport loading the library. However, you can use ʻimport to load classes and methods written in other files.

If the file you want to read is in the same directory

helper.py


class Helper:
  def some_help():
    #Algorithms that help

app.py


from helper import Helper

Helper().some_help()

If the file you want to read is in a different directory

Project
- helpers
  - helper.py
- app.py

In cases like

app.py


from helpers.helper import Helper

Or

app.py


from helpers import helper
from helper import Helper

When the file you want to read wants to read another file

Project
- helpers
  - base_helper.py 
  - helper.py
- app.py

If you want to use the BaseHelper class of helper.py in base_helper.py with a structure like

helper.py


from helpers.base_helper import BaseHelper
# from helpers import base_helper
# from base_helper import BaseHelper
#I don't know why, but I couldn't. Please tell me who understands.

app.py


from helpers.helper import Helper

Recommended Posts

Read and use Python files from Python
[Python] Use this to read and write wav files [wavio]
Read and write JSON files in Python
Use MySQL from Python
Use MySQL from Python
Use BigQuery from python.
[Python] Read From Stdin
Use mecab-ipadic-neologd from python
Firebase: Use Cloud Firestore and Cloud Storage from Python
Study from Python Reading and writing Hour9 files
[Python] How to read data from CIFAR-10 and CIFAR-100
Use MySQL from Anaconda (python)
Use e-Stat API from Python
Include and use external Kv files in Python Kivy
Read and write files with Slackbot ~ Bot development with Python ~
Get files from Linux using paramiko and scp [Python]
Use Stanford Core NLP from Python
Python, yield, return, and sometimes yield from
[Python / matplotlib] Understand and use FuncAnimation
Manipulate files and folders in Python
Forcibly use Google Translate from python
About Python, from and import, as
Use kabu Station® API from Python
Use Azure Blob Storage from Python
Use the Flickr API from Python
Read files in parallel with Python
Export and output files in Python
Use fastText trained model from Python
Read fbx from python with cinema4d
Use Google Analytics API from Python
Create and read messagepacks in Python
Extract strings from files in Python
How to use functions in separate files Perl and Python versions
[python] Extract text from pdf and read characters aloud with Open-Jtalk
From Python to using MeCab (and CaboCha)
[Python] Use and and or when creating variables
Play audio files from Python with interrupts
Use PostgreSQL data type (jsonb) from Python
How to install and use pandas_datareader [Python]
Use machine learning APIs A3RT from Python
I want to use jar from python
Use PIL and Pillow with Cygwin Python
Decrypt files encrypted with openssl from python with openssl
Read and write csv files with numpy
Use Google Cloud Vision API from Python
Reading and writing JSON files with Python
Porting and modifying doublet-solver from python2 to python3.
Read Python csv and export to txt
python: How to use locals () and globals ()
How to use Python zip and enumerate
Use Django from a local Python script
Use C ++ functions from python with pybind11
[python] Read html file and practice scraping
How to use is and == in Python
Use Python and MeCab with Azure Functions
Deploy Python face recognition model on Heroku and use it from Flutter ②
Deploy Python face recognition model on Heroku and use it from Flutter ①
sql from python
Connect to postgreSQL from Python and use stored procedures in a loop.
Get options in Python from both JSON files and command line arguments
MeCab from Python