4

I am creating a requirements.txt file for my Python project. When someone installs my project via pip; I want pip to also download the Tkinter and time Python modules, both of which are modules in the Python Standard Library.

I used the command pip freeze to list all of the currently installed Python packages, but when I scrolled down to the T section, I could not find Tkinter or time in the list. I then tried to install Tkinter via pip using the command pip install Tkinter but got the following error:

Could not find a version that satisfies the requirement Tkinter (from versions: )
No matching distribution found for Tkinter

I also did the same with time and got the same error. (I am running Python 3.6 and Windows 10.)

Is it possible to get pip to install those modules while pip is installing my package, or do I just have to trust that because the user has Python installed they will also have Tkinter and time installed?

All help is welcome and appreciated!

William V.
  • 515
  • 5
  • 24

2 Answers2

5

Python's Standard Library is called the Standard Library because it is a standard of Python. In other words, if there is no Standard Library installed, the python environment is not python at all.

The Standard Library is tested and released together with each Python release as part of this release (not as an addition or extension).

So, YES, you can expect these libraries to exist if the user has Python installed. Just by definition.


Regarding the upgrades of the built-in libraries: NO, you cannot do this. Because they are part of the python setup, not of the application environment. Python is very tightly bound to the specific code in those libraries. All python apps & libs expect the same behavior of those libraries, even if they are buggy.

In addition to that, you cannot install a module/package with the same name as one of the python's builtins, because it will create the ambiguity on import, and can confuse/break all other libraries which depend on it (or worse, the system applications if you install it into the system python).

However, in some cases you can find the backports of some of the libraries. Usually, they are backported from py3 to py2. Of course, their name is changed.

As an example, you can look into concurrent.features library, which is a handy builtin in py3.2+, but was absent in py2.7.


UPD: Though, as @JulienPalard hints in the comments, some OS distributions can split this standard library to simplify the binary dependencies: e.g., on Debian, Tkinter will be installable separately as python3-tk.

This makes sense, indeed, from the point of view of the binary OS packaging: it is not worth installing the UI parts of the python library if you have no UI at all and want to save the disk space.

However, you are still unable to install it via pip. Because this package is not packaged and available separately on PyPI. This standard library separation is made by the selected OS distributions and is resolved with the means of that OS distribution only.

Sergey Vasilyev
  • 3,919
  • 3
  • 26
  • 37
  • 3
    Debian does not provide tkinter in their python3 package, they have a separated python3-tk package to avoid a dependency hell on minimal setups. – Julien Palard Oct 21 '17 at 22:59
  • @JulienPalard Thanks! I've added this to the answer. Forgot that something can be different outside of the python ecosystem... – Sergey Vasilyev Oct 21 '17 at 23:08
  • Thanks! @SergeyVasilyev Does this mean that I will just have to hope that Debian users have Tkinter installed when installing my package? – William V. Oct 22 '17 at 00:26
  • 1
    @WilliamV. Yes. In the best case, you can check it once at the start, and show them the nice error message and suggestion to `apt-get install python3-tk` (or whatever is needed for that OS). But it is not worth doing such checks on every module. Even better if you distribute your app as a binary package for those OSes, which just depends on `python3-tk` or similar. – Sergey Vasilyev Oct 22 '17 at 10:53
4

pip installs packages from pypi, which does not expose the standard library, which is bundled into Python.

So in theory you should trust your users environment, if they have Python, they should have the whole stadard library.

But some distributions are splitting Python in a few packages for reasons (A minimal Debian already depends on Python, but they don't want Python to pull tk to pull libx11 to pull the life, the universe, and everything).

So in practice some package will be there, and you can trust the distribs for this, like time will always be here. And some package may not be here, like tkinter, in which case you may want to surround the import tkinter by a try to error a nice "Looks like your distribution does not provide tk by default, please install it.".

Be reassured, you won't have to surround every imports by try statements just in case some distribution splitted the stdlib, just tkinter.

Julien Palard
  • 8,736
  • 2
  • 37
  • 44