Archives For November 30, 1999

When using pip command to install a Python package in Ubuntu 24.04 will output “error: externally-managed-environment“. Here are a few workarounds to ‘fix’ the issue.

As the terminal output shows you, it’s the change due to PEP 668. Since Ubuntu 23.04, it recommends Python-specific package management tools (e.g., pip) to install packages using a virtual environment, to avoid conflicts to packages installed by OS package managers. Though, user can still force pip to install into interpreter’s global context just like before.


Continue Reading…

This simple tutorial shows how to install Python 3.13, which is still in beta development stage, in Ubuntu 24.04, Ubuntu 22.04, and/or Ubuntu 20.04 LTS.

Python 3.13 so far is in beta development stage. For testing or software developing purpose, you can install it in Ubuntu either from PPA or by building from source tarball.

Features in Python 3.13 so far, compare to the last 3.12:

  • New interactive interpreter, features multi-line editing and color support, as well as colorized exception tracebacks.
  • Experimental free-threaded build mode.
  • A preliminary, experimental JIT.
  • The (cyclic) garbage collector is now incremental.
  • Include a modified version of mimalloc, and enabled by default if supported by the platform.
  • Docstrings now have their leading indentation stripped, reducing memory use and the size of .pyc files.
  • The dbm module has a new dbm.sqlite3 backend that is used by default when creating new files.
  • Support for type defaults in type parameters.
  • New type narrowing annotation, typing.TypeIs.
  • New annotation for read-only items in TypeDicts.
  • Removals of many deprecated modules: aifc, audioop, chunk, cgi, cgitb, crypt, imghdr, mailcap, msilib, nis, nntplib, ossaudiodev, pipes, sndhdr, spwd, sunau, telnetlib, uu, xdrlib, lib2to3.

For more, see the beta release note.

Continue Reading…

This simple tutorial shows how to fix broken pip installer after installing Python 3.12 from Deadsnakes PPA in Ubuntu 22.04 and Ubuntu 20.04.

Due to removal of long deprecated pkgutil.ImpImporter class, pip command may not work for Python 3.12 in your Linux with old setuptools. It either just does not work or outputs following error, when installing anything via pip install:

ERROR: Exception:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/base_command.py", line 165, in exc_logging_wrapper
    status = run_func(*args)
             ^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 205, in wrapper
    return func(self, options, args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 285, in run
    session = self.get_default_session(options)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 75, in get_default_session
    self._session = self.enter_context(self._build_session(options))
                                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 89, in _build_session
    session = PipSession(
              ^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py", line 282, in __init__
    self.headers["User-Agent"] = user_agent()
                                 ^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/network/session.py", line 157, in user_agent
    setuptools_dist = get_default_environment().get_distribution("setuptools")
                      ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/pip/_internal/metadata/__init__.py", line 24, in get_default_environment
    from .pkg_resources import Environment
  File "/usr/lib/python3/dist-packages/pip/_internal/metadata/pkg_resources.py", line 9, in 
    from pip._vendor import pkg_resources
  File "/usr/lib/python3/dist-packages/pip/_vendor/pkg_resources/__init__.py", line 2164, in 
    register_finder(pkgutil.ImpImporter, find_on_path)
                    ^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?

To fix the issue, you just have to manually install pip for Python 3.12. And, here’s how to do the job in 2 ways.

Method 1: Install pip via ensurepip

Python has a built-in module ensurepip, to provide support for bootstrapping the pip installer into an existing Python installation.

First, press Ctrl+Alt+T on keyboard to open terminal. When it opens, run command:

python3.12 -m ensurepip --upgrade

After that, you should be able to use the pip installer for Python 3.12 by either pip3.12 or python3.12 -m pip command.

Method 2: Install pip via get-pip.py

In case the previous method does not work for you, Pypa.io provides a script that can do the same job.

First, open terminal (Ctrl+Alt+T) and run command to download the script via wget command:

wget https://bootstrap.pypa.io/get-pip.py

Then, just run the script to download & install pip:

python3.12 get-pip.py

After installation, you may run python3.12 -m pip command to use the pip installer. To bind it to pip3.12, just run alias pip3.12="python3.12 -m pip".

That’s all. Enjoy!

Python 3.12.0 final was officially announced this Monday! Ubuntu LTS can easily install it from PPA.

New features in Python 3.12.0:

  • More flexible f-string parsing, allowing many things previously disallowed.
  • Python-level API for the buffer protocol
  • New API for monitoring Python programs running on CPython at low cost.
  • Per-Interpreter Global Interpreter Locks
  • Support for the Linux perf profiler to report Python function names in traces.
  • New type annotation syntax for generic classes
  • New decorator typing.override() in the typing module.

The new Python release also removed a few deprecated modules, including smtpd and distutils. For Ubuntu 22.04, Ubuntu 20.04, it causes a broken pip. As a workaround, you need to manually install pip for Python 3.12.

How to Install Python 3.12.0 in Ubuntu:

For Ubuntu 22.04, Ubuntu 20.04, and their derivatives such as Linux Mint 21, the Deadsnakes PPA has made the packages for all supported CPU architecture types: amd64, arm64/armhf, ppc64el, and s390x.

1. First, press Ctrl+Alt+T on keyboard to open terminal. Then paste the command below and hit run to add PPA:

sudo add-apt-repository ppa:deadsnakes/ppa

Type user password (no asterisk feedback) when it asks and hit Enter to continue.

2. Ubuntu 20.04+ automatically refresh package cache while adding PPA. However, Linux Mint user may need to do this job manually by running command:

sudo apt update

3. Finally, run command to install Python 3.12:

sudo apt install python3.12

For other Ubuntu editions, you may manually compile Python 3.12 from source tarball.

For developers who want to prepare their project for the latest Python releases, here’s how to install Python 3.12 in all current Ubuntu releases.

Python 3.12 finally goes stable. It features more flexible f-string parsing, Per-Interpreter GIL, new type annotation syntax for generic classes, support for the Linux perf profiler, and many performance improvements, but removed the distutils package and wstr from Unicode. See more about Python 3.12.

How to Install Python 3.12

Python is easy to install in Ubuntu by either using the popular Deadsnakes PPA or building from the source. Choose either one that you prefer.

Option 1: Install Python 3.12 from PPA

For Ubuntu 22.04, Ubuntu 20.04, and their derivatives such as Linux Mint 21, the Deadsnakes PPA has made the packages for all supported CPU architecture types: amd64, arm64/armhf, ppc64el, and s390x.

1. First, press Ctrl+Alt+T on keyboard to open terminal. Then paste the command below and hit run to add PPA:

sudo add-apt-repository ppa:deadsnakes/ppa

Type user password (no asterisk feedback) when it asks and hit Enter to continue.

2. Ubuntu 20.04+ automatically refresh package cache while adding PPA. However, Linux Mint user may need to do this job manually by running command:

sudo apt update

3. Finally, run command to install Python 3.12:

sudo apt install python3.12

Option 2: Compile and install Python 3.12 from source

Don’t trust third-party repositories or you’re running Ubuntu 23.04 or Ubuntu 18.04? It’s easy to build Python from the source tarball.

1. First download the source tarball from its ftp download page:

2. Then open ‘Downloads’ folder, extract the source tarball, finally right-click on source folder and select “Open in Terminal”.

3. When terminal opens, run the commands below one by one to configure and build Python:

./configure --enable-optimizations
sudo make -j4 && sudo make altinstall

NOTE: You have to first install all build dependency libraries before running last 2 commands. See this tutorial for details.

Verify:

Once installed Python 3.12, verify by running command:

python3.12 --version && pip3.12 --version

Set Python 3.12 as default

It’s NOT recommended to set non-preinstalled Python package as default for Python3, since it will break some core applications.

However, you may try to set python3.12 as python by running the commands below one by one:

  • First, run command to find out where Python 3.12 executable is installed to:
    whereis python3.12

    It’s either /usr/bin/python3.12 or /usr/local/bin/python3.12.

  • Then, add Python 3.12 as an alternative link to python (replace /usr/local/bin/python3.12 according last command output).
    sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.12 1
  • Finally, run command to set default for /usr/bin/python executable if more then one available:
    sudo update-alternatives --config python

Uninstall Python 3.12:

If you installed Python 3.12 using the PPA repository, simply open terminal and run command to remove it:

sudo apt remove --autoremove python3.12

For the PPA, run command to remove it:

sudo add-apt-repository --remove ppa:deadsnakes/ppa

If you built the Python 3.12 from source tarball, then there’s no uninstaller script to automate the job.

However, you may manually remove the installed files by running commands:

cd /usr/local/bin && sudo rm python3.12* pip3.12 idle3.12 pydoc3.12 2to3-3.12
cd /usr/local/lib && sudo rm -R python3.12 pkgconfig libpython3.12.a

The popular Python programming language released version 3.11 today. Here’s the new features and how to install guide for all current Ubuntu releases.

Python 3.11 claimed to be 10-60% faster than the previous 3.10, and features:

  • Exception Groups and except* to raise and handle multiple unrelated exceptions simultaneously.
  • Add add_note() method to BaseException to enrich exceptions.
  • Add the tomllib module to the standard library for parsing TOML
  • Point to exact expression that caused error when printing tracebacks.
  • New -P command line option and PYTHONSAFEPATH environment variable
  • Add TypeVarTuple, enabling parameterisation with an arbitrary number of types
  • Required[] and NotRequired[] to mark whether individual TypedDict items must be present.
  • Add Self to annotate methods that return an instance of their class
  • LiteralString to accept arbitrary literal string types, such as Literal["foo"] or Literal["bar"].
  • dataclass_transform to decorate a class, metaclass, or a function that is itself a decorator.
  • Removed Py_UNICODE encoder APIs
  • Macros converted to static inline functions
  • Many legacy standard library modules deprecated and to be removed in Python 3.13

How to Install Python 3.11 in Ubuntu

For Ubuntu 22.04, Ubuntu 20.04, Ubuntu 18.04, and their derivatives, such as Linux Mint, there’s a popular Deadsnakes PPA maintains the packages for Python 3.11 as well as other Python versions.

NOTE: The PPA does not support Ubuntu 22.10. You may follow the bottom link to build it from source tarball.

1. First, press Ctrl+Alt+T on keyboard to open terminal. When it opens, run command to add the PPA:

sudo add-apt-repository ppa:deadsnakes/ppa

Type user password when it asks (no asterisk feedback) and hit Enter to continue

2. Then refresh package cache via command below, though it’s done automatically in Ubuntu 20.04+:

sudo apt update

3. Finally, install python 3.11 via command:

sudo apt install python3.11

Or replace python3.11 with python3.11-full for IDE, pip package manager, etc.

Verify:

To verify, run python3.11 --version, python3.11 -m pip --version in terminal.

Set Python 3.11 as default

NOTE: change default Python3 in Ubuntu may cause issues for some default apps, such as GNOME Terminal

You may set the new Python package as default by using update-alternatives command line tool.

1. First, run command to create symbolic links for system default python (change python3.10 depends your Ubuntu edition)

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 110

2. Then, add the new Python 3.11 via command:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 100

3. After that, you may choose which Python as Python3 at any time by running command:

sudo update-alternatives --config python3

Compile and install Python 3.11 manually from source

User may also compile the programming language from source tarball manually.

First, download the source from Python web site, then you may follow this step by step guide that I’ve tested in my Ubuntu 22.10 machine.

JetBrains announced the 2022.1 release of its PyCharm IDE few days ago. Here’s how to install in Ubuntu in different ways.

What’s New in PyCharm 2022.1

This is the first release of the IDE in 2022. Release highlights include (see release note for details):

  • Basic http authentication support for custom package repositories
  • Enhanced code completion for TypedDict
  • Improved TypedDict per-key warnings
  • Run commands directly from Markdown files
  • New Copy code snippet for Markdown.
  • Code cells remain in Edit mode after execution (Pro)
  • Optimized cell copy-pasting (Pro)
  • MongoDB: Editing fields in results (Pro)
  • New Services UI for Docker (Pro)

How to Install PyCharm 2022.1 in Ubuntu Linux

Method 1: Snap package

JetBrains provides official package for Ubuntu users through the Snap which runs in sandbox. Ubuntu user may just search for and install it from Ubuntu Software:

For choice, you may also press Ctrl+Alt+T on keyboard and run commands below to install it:

sudo snap install pycharm-community --classic

And you may replace pycharm-community with pycharm-professional in command for pro edition.

Method 2: Flatpak package

The IDE is also available to install as Flatpak, another universal package runs in sandbox.

1. First press Ctrl+Alt+T on keyboard and run command to install Flatpak daemon:

sudo apt install flatpak

2. Next, install the Python IDE as Flatpak using command:

flatpak install https://dl.flathub.org/repo/appstream/com.jetbrains.PyCharm-Community.flatpakref

For the Professional edition, use command:

flatpak install https://dl.flathub.org/repo/appstream/com.jetbrains.PyCharm-Professional.flatpakref

Method 3: Linux Tarball

For those don’t like the Snap and/or Flatpak, there’s no official .deb package available though you may keep an eye on this unofficial PPA.

There’s however Linux tarball available to download in its website:

After downloaded the package, extract and run the “pycharm.sh” file under ‘bin’ sub-folder will launch the IDE.

Create app shortcut so to launch PyCharm IDE from start menu

The Linux tarball lacks app shortcut integration. Here’s how to create one manually.

1.) For current user only, I’d recommend to move the source folder into “.local/bin” folder.

Open ‘Files’, press Ctrl+H, and then navigate to “.local -> bin (create if not exist)”. Then, move the source folder from Downloads to that directory.

2.) Press Ctrl+Alt+T on keyboard to open terminal, and verify by running command:

~/.local/bin/pycharm-*/bin/pycharm.sh

If the IDE launches, you can now create app shortcuts via the next step.

3.) In terminal (Ctrl+Alt+T), run command to create a shortcut file and edit via Gedit text editor:

gedit ~/.local/share/applications/pycharm-community.desktop

When the file opens, add following lines:

[Desktop Entry]
Name=PyCharm Community
Exec=/home/ji/.local/bin/pycharm-community-2022.1/bin/pycharm.sh
Icon=/home/ji/.local/bin/pycharm-community-2022.1/bin/pycharm.png
Terminal=false
Type=Application
Categories=Application;Development;
Keywords=ide;python;charm;
StartupWMClass=jetbrains-pycharm-ce

NOTE: you need to change the value of “Exec” and “Icon” by replacing ji with your username, and community with professional for Pro edition.

Tip: you may just Press Ctrl+L in file manager to copy the PATH to current folder quickly. And then paste in the previous text editor.

NOTE: If you write wrong value for ‘Exec’, the app won’t appear in system start menu (Show Applications) search results.

Remove PyCharm:

To remove the IDE, run the command below accordingly. Replace community to professional, or Community to Professional for Pro edition.

Remove the Snap package via command:

sudo snap remove pycharm-community

To remove the Flatpak package:

flatpak uninstall --delete-data com.jetbrains.PyCharm-Community

And, remove the tarball package via command:

rm -R ~/.local/bin/pycharm-* ~/.local/share/applications/pycharm-*.desktop

This simple tutorial shows how to compile and install Python 3.11, Python 3.10, or other certain Python version in Ubuntu.

For Ubuntu 18.04, Ubuntu 20.04 and Ubuntu 22.04 LTS, there’s well trusted “deadsnakes” team PPA that maintains all the Python packages. For non-LTS releases, you may build the programming language package manually from the source tarball.

NOTE: This tutorial is tested and works in Ubuntu 20.04, Ubuntu 22.04, and Ubuntu 23.04 in my case.

1. Preparation:

Before getting started, you need to install some essential packages for building the computer language package.

Open terminal either by pressing Ctrl+Alt+T on keyboard or by searching from start menu. When it opens, run the command below to install the dependencies:

sudo apt install wget build-essential libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

2. Download Python Tarball:

You can simply download the latest tarball from the Python website via the link below:

In case you’re running Ubuntu without desktop environment, download the source package by running the wget command in terminal:

wget -c https://www.python.org/ftp/python/3.11.0/Python-3.11.0.tar.xz

For other Python version, go and download via web browser in this page. And, change the version number in command accordingly.

And uncompress the tarball either by right-click and selecting ‘Extract Here‘ in file manager, or by running command in terminal:

tar -Jxf Python-3.11.0.tar.xz

3. Configure the source:

Depends on how you grab the source tarball, either right-click on it in file manager, extract, and open the source folder in terminal via context menu option:

Open Python 3.11 source folder in terminal

If you just did the tar command above, then you may navigate to source folder via:

cd Python-3.11.0/

The command varies depends on which Python version you downloaded.

When you’re in source directory, run command to configure the source with expensive, stable optimizations (PGO, etc.):

./configure --enable-optimizations

4. Build and install Python:

Finally compile and install it into “/usr/local/bin” by running command:

sudo make -j4 && sudo make altinstall

Here -j4 will start 4 threads to speed up the job. Depends on your CPU, you may increase the number or just skip it. And, when make command done, it will ask you to type password for running make install with sudo privilege.

5. Verify

If everything’s done successfully without error, you may verify by running command:

python3.11 --version
python3.11 -m pip --version

It should output the language version as well as the pip package manager version for you

5. Make Python 3.11 default

IMPORTANT: Change default Python3 in Ubuntu MAY cause issues!!! Do it at your own risk.

After installation, you may set it as default by creating symbolic links to /usr/bin/python3:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 110
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.11 100

In the first command change system default python3.10 to python3.8 if you’re on Ubuntu 20.04. Finally, run command below at any time to select which Python to use as default:

sudo update-alternatives --config python3

Uninstall Python 3.11:

Until you removed the source directory, you may first either navigate to that folder in terminal or right-click and select open folder in terminal, finally run make uninstall command to remove Python 3.11:

sudo make uninstall

If you’ve already removed the source, or the previous command does not work for you, try removing all the installed libraries manually via:

cd /usr/local/bin && rm -f sudo rm -f 2to3 2to3-3.11 idle3 idle3.11 pip pip3 pip3.10 pip3.11 pydoc3 pydoc3.11 python3 python3.11 python3.11-config python3-config

After several alpha, beta and rc tests, the Python programming language finally released version 3.10 today!

Python 3.10 will receive bug-fix updates in next 18 months. After that, it’s supported with 5-year security updates until October 2026.

What’s New in Python 3.10:

  • Pattern matching syntax, inspired by similar syntax found in Scala, Erlang, and other languages.
  • Support using enclosing parentheses for continuation across multiple lines in context managers
  • Allow writing union types as X | Y
  • Add optional length-checking to zip
  • Precise line numbers for debugging and other tools
  • Require OpenSSL 1.1.1 or newer
  • Remove Py_UNICODE encoder APIs
  • Add optional EncodingWarning
  • Explicit Type Aliases
  • Parameter Specification Variables

See the release note for more about Python 3.10.

How to Install Python 3.10 in Ubuntu:

The well trusted “deadsnakes” team PPA has made the packages for Ubuntu 20.04, Ubuntu 18.04, and derivatives, e.g., Linux Mint 20, Elementary OS 6 and Zorin OS 16.

1.) Firstly, open terminal by either searching from overview screen or pressing Ctrl+Alt+T on keyboard. When it opens, run command to add the PPA:

sudo add-apt-repository ppa:deadsnakes/ppa

2.) Then refresh system cache and install the language via command:

sudo apt update && sudo apt install python3.10

Use Python 3.10 as default Python3:

It’s possible to make Python3 in Ubuntu to be Python3.10 via following commands. However, it causes issues, such as Gnome Terminal refuses to launch. Do it at your own risk!

  • Check current Python3 version:
    python3 --version
  • Use update-alternatives to create symbolic links to python3 (replace python3.8 in Ubuntu 16.04 with ):
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
  • And choose which one to use as Python3 via command:
    sudo update-alternatives --config python3

PyCharm Ubuntu

JetBrains announced the release of PyCharm 2021.2. Features Python 3.10 support, auto-reload for browser HTML preview.

Starting with the new release, users from Asian can enjoy the a fully localized UI in Chinese, Korean, or Japanese. And it’s going to end support for several packages, e.g., mako, buildout, web2py, in next release.

Before Python 3.10 goes stable, the IDE starts working for Python 3.10 support with following features:

  • Adjust to the upcoming Structural Pattern Matching, such as the Unused local symbols and Unreachable code inspections, smart code completion and syntax highlighting for the match and case keywords, and the Complete Current Statement action.
  • Complete match statements and their corresponding case blocks.
  • Supports type inference for isinstance and issubclass arguments with the new syntax type.

And other changes include:

  • Working in the Python console is now supported on both the client and the host side.
  • Code completion for fields and operators in the MongoDB console (Pro only).
  • Context live templates from the data editor with SQL scripts action (Pro only).
  • auto-reload for browser HTML preview (pro).
  • Auto-import for require() (pro).
  • New Onboarding Tour in the IDE Features Trainer plugin.
  • Test runner auto-detection
  • The Test Management plugin includes Python support and available for PyCharm Community.
  • New Test Data plugin to generate random data.

How to Install PyCharm 2021.2 in Ubuntu:

Jetbrains provides official Linux package. They are portable tarballs, available to download at the link below:

To make life easier, you can install the community edition from the unofficial PPA. And it supports for Ubuntu 20.04 and Ubuntu 21.04 so far.

1.) Open terminal from start menu, and run command to add the PPA:

sudo add-apt-repository ppa:xtradeb/apps

2.) Manually refresh system package cache, e.g., for Linux Mint:

sudo apt update

3.) Finally install PyCharm via command:

sudo apt install pycharm-community

Uninstall:

To remove the PPA, either run command in terminal:

sudo add-apt-repository --remove ppa:xtradeb/apps

or open “Software & Updates” and navigate to “Other Software” tab to remove the relevant line.

And remove PyCharm by running command in terminal:

sudo apt remove --autoremove pycharm-community