[Beginner memo] How to specify the library reading path in Python

It's time to study the pass.

It was a Python file I made, and I could use it by specifying the path when reading another file. I will leave it as a memo. The content is for beginners.

wrap up

--The form sys.path.append (os.pardir) using sys library and os library is often used. --If you specify a relative path in the hoge part of sys.path.append ("hoge"), it will be read from there.

Main paper

Premise

Assuming you want to read functions.py from main.py in the following file

/application
  /common
    functions.py
  /main
    main.py #Suppose you are here now

Method 1: Use sys.path.append (os.pardir).

import sys, os
sys.path.append(os.pardir)

#File name you want to read
from common.functions import hogehoge

Commentary

--sys, os-> Python basic library (= library that can be used only by specifying import without installation). sys enables operations related to the python execution environment, and os enables basic operations related to the OS. --sys.path-> Represents the path to load the library (like C: / hogehoge / hogehoge /) --sys.path.append ("hoge")-> Change the above to hoge --os.pardir-> Represents a string representing the OS-dependent parent directory. For Windows, it corresponds to *** ".." *** (corresponds to `..``` when typing `cd ..``` at the command prompt) --In the above code, the reading directory of the library is changed to / applications at the moment when sys.path.append (os.pardir) and the code are read, so common.functions.py can be read after that. ing

Method 2: Specify the relative path in parentheses of sys.path.append ()

import sys
sys.path.append("../common")

Commentary

--What you are doing is the same as Method 1 ――However, you can express a complicated directory here. --For example, `` sys.path.append ("../../another_application/lib/hogehoge ") `

(that's all)

Recommended Posts

[Beginner memo] How to specify the library reading path in Python
How to use the C library in Python
[Python] How to import the library
How to debug the Python standard library in Visual Studio
A story about how to specify a relative path in python.
How to use Python Image Library in python3 series
How to specify TLS version in python requests
How to get the files in the [Python] folder
How to retrieve the nth largest value in Python
How to pass the path to the library built with pyenv and virtualenv in PyCharm
How to get the number of digits in Python
How to know the current directory in Python in Blender
[Python] How to use the graph creation library Altair
[Python] Summary of how to specify the color of the figure
How to use the model learned in Lobe in Python
How to develop in Python
[Python] How to output the list values in order
[Selenium] How to specify the relative path of chromedriver?
[python] How to check if the Key exists in the dictionary
How to import Python library set up in EFS to Lambda
[python] How to use the library Matplotlib for drawing graphs
How to use the __call__ method in a Python class
How to develop in a virtual environment of Python [Memo]
How to specify Cache-Control for blob storage in Azure Storage in Python
How to get the last (last) value in a list in Python
Notes on how to use marshmallow in the schema library
Data science companion in python, how to specify elements in pandas
[Python] How to do PCA in Python
How to collect images in Python
Get the desktop path in Python
How to use Requests (Python Library)
How to use SQLite in Python
Get the script path in Python
In the python command python points to python3.8
How to get the Python version
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Get the desktop path in Python
[Nanonets] How to post Memo [Python]
How to handle Japanese in Python
How to know the internal structure of an object in Python
The 15th offline real-time how to write reference problem in Python
The 17th Offline Real-time How to Solve Writing Problems in Python
How to check the memory size of a variable in Python
How to judge that the cross key is input in Python3
How to use the asterisk (*) in Python. Maybe this is all? ..
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
The 14th offline real-time how to write reference problem in python
The 18th offline real-time how to write reference problem in Python
How to display bytes in the same way in Java and Python
[Introduction to Python] How to use class in Python?
How to access environment variables in Python
How to do R chartr () in Python
[Itertools.permutations] How to put permutations in Python
How to work with BigQuery in Python
[Python beginner memo] Python character string, path operation
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python