[Python] When you want to import and use your own package in the upper directory

I want to import and use my own package in the upper directory with Python, but I could not find a solution when I do not want to import by relative path or change sys.path in the script, so I will write it down.

In conclusion, create setup.py and use the command pip install -e ..

situation

Directory configuration example:

project
+ mypackage
|  + __init__.py
|  + module_a.py
+ program_main
|  + executor
|    + executor.py
|  + tests
|    + executor_test.py

I want to import and use my own package called mypackage in the upper folder from ʻexecutor.py`.

In my case, there were multiple directories for AWS Lambda services, and LambdaLayer was placed in a higher folder as a common process, so LambdaLayer could not be imported correctly locally from Lambda functions.

Also, Lambda services and Lambda Layer have a directory structure that works fine in the deployed environment, but you have to be careful about the path when unit testing with CICD.

project
+ layers
|  + my_layer_1
|    + ...
|  + ...
+ lambda
|  + service_1
|    + handler.py
|    + serverless.yml
|    + requirements.txt
|    + env
|    + ...
|    + tests
|      + ...
|  + ...
+ setup.py

approach

You can manage packages with setup.py. Put the following setup.py in the root directory.

from setuptools import setup, find_packages

setup(name='myproject', version='1.0', packages=find_packages())

find_packages () will automatically find the package where __init__.py exists. If the path doesn't work or you can't find it, you may want to play with package_dir in addition to packages or check if __init__.py exists.

After that, install your own package by executing the following command in the root directory.

pip install -e .

-e reference

e, --editable <path/url>
Install a project in editable mode (i.e. setuptools “develop mode”) from a local project path or a VCS url.

If you specify -e, when you edit and save your own package, it will automatically recognize the changes, so you do not need to reinstall your own package (pip install).

reference

https://stackoverflow.com/questions/17155804/confused-about-the-package-dir-and-packages-settings-in-setup-py

Recommended Posts

[Python] When you want to import and use your own package in the upper directory
[Python] When you want to use all variables in another file
Use the CASA Toolkit in your own Python environment
I want to use the R dataset in python
How to use pyenv and pyenv-virtualenv in your own way
If you want your colleagues to use the same language
If you want to use field names with hyphens when updating firestore data in python
[Subprocess] When you want to execute another Python program in Python code
When you run diff in python and want both returncode and output
Easily use your own functions in Python
[Python] Package and distribute your own modules
How to use is and == in Python
When you want to use multiple versions of the same Python library (virtual environment using venv)
Python Note: When you want to know the attributes of an object
The file name was bad in Python and I was addicted to import
If you want to count words in Python, it's convenient to use Counter.
How to use the C library in Python
When you want to update the chrome driver.
I want to use Temporary Directory with Python2
Import your own modules in Grasshopper's Python development
I want to display the progress in Python!
How to build an environment when you want to use python2.7 after installing Anaconda3
[Python] I want to know the variables in the function when an error occurs!
I want to use Python in the environment of pyenv + pipenv on Windows 10
Use libsixel to output Sixel in Python and output a Matplotlib graph to the terminal.
How to solve "attempted relative import beyond top-level package" when importing in Python
I want to get the file name, line number, and function name in Python 3.4
It is convenient to use stac_info and exc_info when you want to display traceback in log output by logging.
How to know the current directory in Python in Blender
Try to improve your own intro quiz in Python
[Reintroduction to python] How to import via the parent directory
% And str.format () in Python. Which one do you use?
[Road to intermediate Python] Define in in your own class
Call your own python module from the ROS package
How to use the model learned in Lobe in Python
When you want to plt.save in a for statement
The programming language you want to be able to use
I want to do something in Python when I finish
A convenient function memo to use when you want to enter the debugger if an error occurs when running a Python script.
Use Python to monitor Windows and Mac and collect information on the apps you are working on
[Django] A memorandum when you want to communicate asynchronously [Python3]
Jedi-vim shortcut command that allows you to refer to the definition source and definition destination in Python
I know? Data analysis using Python or things you want to use when you want with numpy
[Small story] A painstaking measure when you have to execute a function before import in Python
How to import a file anywhere you like in Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Use aggdraw when you want to draw beautifully with pillow
[Python] I want to use the -h option with argparse
[Note] Import of a file in the parent directory in Python
Comparison of how to use higher-order functions in Python 2 and 3
[Python3] Code that can be used when you want to resize images in folder units
I want to know the features of Python and pip
When you want to use python2.x on modern Gentoo Linux
If you want to put an argument in the closure function and execute it later
When you want to hit a UNIX command on Python
How to write when you want to put a number after the group number to be replaced with a regular expression in re.sub of Python
Introduction to how to use Pytorch Lightning ~ Until you format your own model and output it to tensorboard ~
When you want to replace multiple characters in a string without using regular expressions in python3 series
Do you want to wait for general purpose in Python Selenium?
processing to use notMNIST data in Python (and tried to classify it)
[Python] Solving the import problem due to the difference in entry points