Question about Packaging in Python with pip

Question:

I saw this nice explanation video (link) of packaging using pip and I got two questions:

The first one is:

I write a code which I want to share with my colleagues, but I do not aim to share it via pypi. Thus, I want to share it internally, so everyone can install it within his/ her environment.

I actually needn’t to create a wheel file with python setup.py bdist_wheel, right? I create the setup.py file and I can install it with the command pip install -e . (for editable use), and everyone else can do it so as well, after cloning the repository. Is this right?

My second question is more technical:

I create the setup.py file:

from setuptools import setup 

setup(
    name = 'helloandbyemate',
    version = '0.0.1',
    description="Say hello in slang",
    py_modules=['hellomate'],
    package_dir={"": "src"}
)

To test it, I write a file hellomate.py which contains a function printing hello, mate!. I put this function in src/. In the setup.py file I put only this module in the list py_modules. In src/ is another module called byemate.py. When I install the whole module, it installs the module byemate.py as well, although I only put hellomate in the list of py_modules. Has anyone an explanation for this behaviour?

Asked By: Archimedes_91

||

Answers:

I actually needn’t to create a wheel file … everyone else can do it so as well, after cloning the repository. Is this right?

This is correct. However, the installation from source is slower, so you may want to publish wheels to an index anyway if you would like faster installs.

When I install the whole module, it installs the module byemate.py as well, although I only put hellomate in the list of py_modules. Has anyone an explanation for this behaviour?

Yes, this is an artifact of the "editable" installation mode. It works by putting the src directory onto the sys.path, via a line in the path configuration file .../lib/pythonX.Y/site-packages/easy-install.pth. This means that the entire source directory is exposed and everything in there is available to import, whether it is packaged up into a release file or not.

  • The benefit is that source code is "editable" without reinstallation (adding/removing/modifying files in src will be reflected in the package immediately)
  • The drawback is that the editable installation is not exactly the same as a "real" installation, where only the files specified in the setup.py will be copied into site-packages directly

If you don’t want other files such as byemate.py to be available to import, use a regular install pip install . without the -e option. However, local changes to hellomate.py won’t be reflected until the installation step is repeated.

Strict editable installs

It is possible to get a mode of installation where byemate.py is not exposed at all, but live modifications to hellomate.py are still possible. This is the "strict" editable mode of setuptools. However, it is not possible using setup.py, you have to use a modern build system declaration in pyproject.toml:

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"

[project]
name = "helloandbyemate"
version = "0.0.1"
description = "Say hello in slang"

[tool.setuptools]
py-modules = ["hellomate"]
include-package-data = false

[tool.setuptools.package-dir]
"" = "src"

Now you can perform a strict install with:

pip install -e . --config-settings editable_mode=strict
Answered By: wim
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.