How to update Python Standard Library packages with pip? - python

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!

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.

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.

Related

Can Python Embeddable Package Install IDLE Separately?

A Python embeddable package can install pip separately (pip with embedded python), but can it also install IDLE separately? As the embeddable package has pythonw.exe already, I tried to externally load idle.pyw with it, but more seem to be needed.
The IDLE IDE is part of the CPython standard library. It is usually an option packaged with tkinter, _tkinter, and, on Windows and Mac, an appropriate version of tcl/tk. Unless embedded Python comes with tkinter and _tkinter and tcl/tk is available, installing IDLE would be useless as well as difficult. It is not available as a separate package on pypi.org. (There are a couple of packages, such as https://pypi.org/project/friendly-idle/ that wrap or extend already installed IDLE.)
https://pypi.org/project/idle/ is a useless, non-functional, pretend package. "This is a program written in tkinter which acts as text editor.This can be used to edit file and as alternative of notepad." is a complete fiction. py -m pip install idle installs a 26-line idle.py. As reported in a comment, it tries to import a fictional module 'layout'. It later tries to run a fictional 'gui.mainloop()'.

How to install python modules using cppyy?

I want to package a python module containing python source and a native c++ library. Cppyy is used to dynamically generate the bindings so the library is really just a normal library. The build system for the library is meson and should not be replaced. The whole thing is in a git repository. I only care about Linux.
My question is how to get from this to “pip install url_to_package builds/installs everything.” in the least complicated way possible.
What I’ve tried:
Extending setuptools with a custom build command:
…that executes meson compile and copies the result in the right place. But pip install will perform its work in some random split-off temporary directory and I can’t find my C++ sources from there.
The Meson python module:
…can build my library and install files directly into some python env. Does not work with pip and has very limited functionality.
Wheels:
…are incredibly confusing and overkill for me. I will likely be the only user of this module. Actually, all I want is to easily use the module in projects that live in different directories…
Along the way, I also came across different CMake solutions, but those are disqualified because of my build system choice. What should I do?

Is there a way to include a brew instillation in a python build?

I'm trying to build a speech recognition application that works in a browser. Currently using pyodide with a web worker. I have my own package, built alongside pyaudio, that I use for the web worker.
Is there a way that I can include a brew instillation, specifically portaudio, inside of my python package so that when I build the package, portaudio is included in the wheel file? I need portaudio included for this to work in the browser.
Thank you!
I'm understanding two different questions here, so I'll try to answer them both.
Can I have a python build fetch a Homebrew project during buildtime
To my knowledge, the answer is no. The Python distribution system is separate from Homebrew, and they can't interact in this fashion.
Even if they could, this wouldn't necessarily be desirable:
What happens if the user isn't on macOS (or Linux)? Then the build would fail.
The prefix that Homebrew will install the package in isn't very deterministic. The user might be using a custom prefix, or they might be on Apple Silicon (which has a different default prefix to Intel).
Your python package might run into some difficulty locating the package.
What about if they don't have Homebrew installed? They might have another package manager like MacPorts or Fink, or maybe none at all.
Can I bundle portaudio into the build distribution?
Maybe? Even if you could, I almost certainly wouldn't recommend it.
Bundling dependencies increases the size of the distribution unnecessarily.
It would take a reasonable amount of effort to setup, assuming you can do it.
All these reasons are why for the majority of projects that have a similar setup, you will find that they recommend installing certain packages with their system package manager first, before building the Python source code.
This allows them to choose whatever package manager they have installed, and it should also be a quick and painless process.
Therefore, just change your installation instructions to the following:
# On macOS
brew install portaudio
pip install ...

install a library if necessary

i wanted to send a short code i was proud of to a fellow student and realized he won't be able to run it since there is no reason for him to have installed the library.
installing is of course super easy - but i realized this could happen often, mainly with beginners - wanted to build a simple function for it:
def smart_import(package_name):
try:
import package_name
except ImportError:
import pip
pip.main(['install', package_name])
problem is that i dont really know how to pass in the name of the package as a value that could be called by import
thought of converting a string back but that seems more complicated then i thought
This is a bad idea for many reasons, the main one being that people generally don't expect a Python function call to automatically attempt to install software on their machine.
Here are some other problems this approach has:
The import name is not always corresponding to the distribution name. For example dateutil module is provided by python-dateutil distribution.
If you try to use smart_import from another module, it will leave the name imported into the wrong namespace.
Some distributions export multiple top-level imports, e.g. setuptools provides setuptools, easy_install, and pkg_resources.
In some cases, pip itself may not be installed.
The script might not have permission to install packages.
The user might want to install distributions into their homedir explicitly with --user, and your script itself can't know that.
The invocation of pip might need to change depending on whether or not you are within a virtualenv.
The installation might attempt to pull in dependencies which cause conflicts with other distributions a user has installed.
IDEs likely won't see that a module dynamically imported is there, and may squiggly underline their subsequent use as a name which can not be resolved.
What to do instead:
Package your code up into its own distribution. Write a setup.py which mentions the dependencies using the install_requires argument to in the setup call. Ask your fellow student to pip install mypackage, and the dependencies will be collected at install time rather than at runtime. You can send your distribution directly to them (as a mypackage-0.1.tar.gz file or as a wheel). Or you can upload it to an index such as the test pypi.
The Python Packaging User Guide is a beginner-friendly resource describing how to create and upload your own distribution.

How can I install a Third-Party Package in Python Canopy?

I am currently attempting to install a third-party package (gnuradio, to be specific) into the Canopy version of Python on an Ubuntu computer. I currently have the entire package stored in my filesystem as a folder with subfolders and python files; however, I have been able to find no information as to how to make the transition into a functionally installed and recognized package. How can I do so?
You shouldn't use third party pythons on Ubuntu, unless you very exactly know what you're doing (you don't). Ubuntu keeps your python up-to-date and uses the package manager to install pack
So, setting up a recent version of python is just
sudo apt-get install python
So if you still want to integrate GNU Radio into your canopy installation, you will need to get the development headers of exactly their version of python, and specify that you want to only use them etc, and build GNU Radio from source. I do not recommend doing that.
In my opinion, you should probably rather install GNU Radio either from source against the python and libraries on your main OS rather than canopy's happy little installation folder, or use the gnuradio package that Ubuntu has.
I recommend using pybombs to install GNU Radio from source. You'll get the latest and greatest, in a safe install prefix, and easy access to bleeding edge SDR device drivers.
(1) Python packaging is inconsistent, but in general, it suffices to type python setup.py install from the package's top directory (see https://docs.python.org/2/install/#the-new-standard-distutils)
(2) How to be sure that you are installing into Canopy Python rather than your system Python? See https://support.enthought.com/entries/23389761-Installing-packages-into-Canopy-Python-from-the-command-line (tl;dr open a Canopy Terminal window from the Canopy Tools menu.)
EDIT: Marcus Müller has clarified below that gnuradio is not a python package, so this general advice is true but irrelevant. See his answer below.

Categories