The best way to manage your frequently updated pure Python code without manually adding paths is to structure it as a Python package and install it in an editable mode (pip install -e). Hereโs the recommended approach:
1. Organize Your Code as a Python Package
Instead of placing scripts randomly, structure your project like this:
my_project/
โโโ my_package/ # Your actual Python code
โ โโโ __init__.py # Makes this directory a package
โ โโโ module1.py # Your code files
โ โโโ module2.py
โโโ scripts/ # Scripts using your package
โ โโโ run_analysis.py
โโโ tests/ # Unit tests
โ โโโ test_module1.py
โโโ setup.py # Installation script
โโโ pyproject.toml # Modern build configuration (optional)
โโโ requirements.txt # Dependencies (if any)
โโโ README.md
2. Write a setup.py for Easy Importing
Since you update your code frequently, installing it in editable mode (pip install -e .) allows changes to reflect without reinstalling.
Create a setup.py in the project root:
Now, install your package in editable mode:
After this, you can import my_package from anywhere in Python scripts without modifying sys.path manually:
The difference between pip install -e . and pip install . lies in how your Python package is installed and whether changes to the source code take effect immediately.
1. pip install . (Regular Installation)
When you run:
What happens?
Copies your packageโs files into the Python environmentโs site-packages directory.
The installed version is staticโchanges to your source code do not affect the installed package unless you reinstall it.
Use case:
โ When your code is stable and you donโt plan to modify it frequently.
2. pip install -e . (Editable Installation)
When you run:
What happens?
Instead of copying files, a symbolic link (symlink) is created pointing to your project directory.
Changes you make to your source code immediately reflect without reinstalling.
The package is marked as editable in pip list.
Use case:
โ When you frequently modify your code and want the changes to be available immediately.
Comparison Table
Feature
pip install .
pip install -e .
How it's installed
Copied to site-packages
Symlinked to the source directory
Changes reflect automatically?
โ No
โ Yes
Reinstallation needed for updates?
โ Yes
โ No
Best for
Stable, production-ready code
Active development
Which One Should You Use?
If you constantly update your code, use pip install -e . (editable mode).
If you're distributing a stable release, use pip install ..
Since your code is flexible and changes frequently, I strongly recommend pip install -e .. ๐
Use pyproject.toml or setup.py?
Advantages of pyproject.toml Over setup.py
Modern Python packaging encourages using pyproject.toml instead of the older setup.py. Hereโs why:
1. Future-Proof and PEP 517/518 Compliant
pyproject.toml follows PEP 517/518, which defines a standardized build system interface.
setup.py is legacy and might be deprecated in the future.
โ Advantage: Ensures compatibility with modern tools like Poetry, Hatch, PDM.
2. No Need for setup.py Execution
With setup.py, running pip install .executes Python code, which can cause unexpected behavior.
pyproject.toml is staticโpip reads it without executing any code.
โ Advantage: More predictable builds, avoiding accidental side effects.
3. Cleaner and More Readable
A pyproject.toml file is more structured and readable than setup.py. Compare:
Using setup.py
Using pyproject.toml
โ Advantage:TOML format is cleaner, structured, and easier to read.