|
| 1 | +.. |
| 2 | +
|
| 3 | + |
| 4 | +Working with Python |
| 5 | +=================== |
| 6 | + |
| 7 | +No *pip* |
| 8 | +-------- |
| 9 | + |
| 10 | +Do not install a global system-wide version of *pip* at all. |
| 11 | + |
| 12 | +There is almost never a good reason to install global system-wide packages via |
| 13 | +*pip* to begin with. Especially on Linux where the default version of Python |
| 14 | +is part of the system and used by the system. So mixing this with Python |
| 15 | +projects that the user install install themselves via *pip* is very likely to |
| 16 | +cause conflicts sooner rather than later. |
| 17 | + |
| 18 | + |
| 19 | +Use isolation |
| 20 | +------------- |
| 21 | + |
| 22 | +If Python tools are needed to be always available from the command line, then |
| 23 | +isolate them with *zapp*, *shiv*, or *pex*. |
| 24 | + |
| 25 | +* *zapp* https://pypi.org/project/zapp/ |
| 26 | +* *shiv* https://pypi.org/project/shiv/ |
| 27 | +* *pex* https://pypi.org/project/pex/ |
| 28 | + |
| 29 | +Those are all *zipapp* single-file Python executables. |
| 30 | + |
| 31 | +* https://www.python.org/dev/peps/pep-0441/ |
| 32 | +* https://docs.python.org/3/library/zipapp.html |
| 33 | + |
| 34 | +*shiv* and *pex* applications are self extractable. *zapp* does not need to be |
| 35 | +extracted. The code is executed directly from within the zip-compressed |
| 36 | +archive. |
| 37 | + |
| 38 | +*pex* applications are executed from their own virtual environment. *zapp* |
| 39 | +applications are not executed in a virtual environment. Not sure about *shiv*. |
| 40 | + |
| 41 | +*shiv* applications show up somehow in the current environment. Whereas *zapp* |
| 42 | +applications do not, so they are perfect for tools such as *deptree*, and |
| 43 | +*pipdeptree*. |
| 44 | + |
| 45 | + |
| 46 | +Use *toolmaker* |
| 47 | +--------------- |
| 48 | + |
| 49 | +To automate the creation of single file Python applications with *zapp*, |
| 50 | +*shiv*, or *pex*, one can use *toolmaker*. |
| 51 | + |
| 52 | +* https://pypi.org/project/toolmaker/ |
| 53 | + |
| 54 | + |
| 55 | +Use *venv* |
| 56 | +---------- |
| 57 | + |
| 58 | +Python 3 has the module *venv* in its standard library since version 3.3. |
| 59 | + |
| 60 | +* https://docs.python.org/3/library/venv.html |
| 61 | + |
| 62 | +So the need for the third party library *virtualenv* is much less pressing. |
| 63 | + |
| 64 | +.. code:: |
| 65 | +
|
| 66 | + $ python3 -m venv .venv |
| 67 | + $ . .venv/bin/activate |
| 68 | +
|
| 69 | +
|
| 70 | +Do not activate virtual environments |
| 71 | +------------------------------------ |
| 72 | + |
| 73 | +The scripts that are installed in a virtual environment (with *setuptools* at |
| 74 | +least) get a shebang with the full path to the Python interpeter from the |
| 75 | +virtual environment. So there is no need to activate the virtual environment |
| 76 | +to call such scripts. |
| 77 | + |
| 78 | +.. code:: |
| 79 | +
|
| 80 | + $ .venv/bin/myscript |
| 81 | + $ .venv/bin/python3 -m mymodule |
| 82 | +
|
| 83 | +
|
| 84 | +.. EOF |
0 commit comments