Install & uninstall

Contents

Install & uninstall#

This page discusses the process of installing packages using pip.

Editable install#

If you want your environment to see code in the specific folder you can package that folder as a python package and install it to pip in editable mode using pip install -e <path to the package>. It actually just adds to the environment reference to the package, but the package in it’s original location.


The following cells create a virtual environment and a simple python project that will be used as example.

mkdir /tmp/install_uninstall &> /dev/null
cd /tmp/install_uninstall
python3 -m venv venv
source venv/bin/activate

The following cell creates a simple python package that will be used as an example.

mkdir my_package &> /dev/null
cat << EOF > my_package/this_message.py
def echo():
    print('show example')
EOF
cat << EOF > pyproject.toml
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "test_package"
version = "0.0.0"

[package.setuptools]
packages = ["my_package"]
EOF

The next code implements the installation of the package in edit mode.

cd /tmp/install_uninstall
pip3 install -e . &> /dev/null

You can check if a package is installed in editable mode by checking the pip list for this package, if there is an Editable project location specified for this package, this means that the environment only has a reference to the package - the real location is specified in the Editable project location field.

pip3 list
Package      Version Editable project location
------------ ------- -------------------------
pip          24.3.1
test_package 0.0.0   /tmp/install_uninstall

The following cell runs code that shows that the library behaves as expected.

python3 -c "from my_package.this_message import echo; echo()"
show example

The main practical consiquence is that you can keep the source code where it’s convenient for you, and it will be available anywhere python enterpriter is running.

The next cell updates the package’s code - to show that you don’t need to reinstall the package to apply the changes.

cat << EOF > my_package/this_message.py
def echo():
    print('new message')
EOF

The following code re-runs the package’s code from a different directory.

cd /
python3 -c "from my_package.this_message import echo; echo()"
new message

Everything works fine and uses the latest version of the code published in the “working” directory.