Python-maint
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # Fedora 36: New installation scheme of Python packages In Fedora 36, Python changes the way installation paths of Python packages are handled. The changes affect the main Python in Fedora 36, Python 3.10, as well as any newer Python version included. Most Fedora users should not be affected by the change but there are situations where there might be slight differences. When Python packages are installed by `sudo pip`, `sudo python setup.py install` or similar methods, Python packages are installed to `/usr/local/lib(64)/python3.10/site-packages/`. This has already been [happening since Fedora 27](https://fedoraproject.org/wiki/Changes/Making_sudo_pip_safe). However the way this is achieved has been significantly re-implemented in Fedora 36 and that has created several minor differences. The `sysconfig` Python module from the standard library defines several *installation schemes*. By default, the installation scheme used on Fedora 36 when installing Python packages using root privileges (for example via `sudo`) is `{prefix}/local/lib(64)/python3.10/site-packages/` (where `{prefix}` is defined as `/usr` by default). When Python itself runs from a Python virtual environment or when building RPM packages, the installation scheme is `{prefix}/lib(64)/python3.10/site-packages/` (it does not include `/local/`). Previously, the `/local/` part was only artificially added when installing packages, now it is part of the installation scheme. This was changed to be more consistent with what other Python distributors are doing, so that the scheme is more likely to be accepted in upstream Python and to work well with upstream tools like setuptools or pip, which we cannot modify when they are installed or upgraded using pip directly from the [Python Package Index](https://pypi.org/). Here are the differences that might be observed with the new approach: ## `sysconfig.get_path(key)` returns paths with `/local/` Previously, on Fedora 35: ```python >>> import sysconfig >>> for key in sysconfig.get_path_names(): ... print(f'{key} = {sysconfig.get_path(key)}') ... stdlib = /usr/lib64/python3.10 platstdlib = /usr/lib64/python3.10 purelib = /usr/lib/python3.10/site-packages platlib = /usr/lib64/python3.10/site-packages include = /usr/include/python3.10 scripts = /usr/bin data = /usr ``` Now, on Fedora 36 (except during RPM build): ```python >>> import sysconfig >>> for key in sysconfig.get_path_names(): ... print(f'{key} = {sysconfig.get_path(key)}') ... stdlib = /usr/lib64/python3.10 platstdlib = /usr/lib64/python3.10 purelib = /usr/local/lib/python3.10/site-packages platlib = /usr/local/lib64/python3.10/site-packages include = /usr/include/python3.10 scripts = /usr/local/bin data = /usr/local ``` The values now reflect the reality of where packages are actually going to be installed with pip, setuptools, distutils, etc. However, if your Python code uses the values to determine where to load Python packages *from*, it won't see dnf-installed packages, which are installed in `/usr/lib(64)/python3.10/site-packages/`. Generally, `sysconfig.get_path(key)` gives results that determine where the Python packages should be installed *to*. To fix affected code, avoid assumptions that "where to install packages *to*" is the same as "where to load Python modules *from*". Example fixes from affected open source projects: - [anaconda/dracut: Don't assume Python modules are in sysconfig.get_path('purelib')](https://github.com/rhinstaller/anaconda/pull/3646) - [dnf: Determine the default plugin path at configure time, rather than runtime](https://github.com/rpm-software-management/dnf/pull/1782) If you need to restore the previous behavior of `sysconfig.get_path(key)`, you may explicitly select the `rpm_prefix` installation scheme: ```python >>> for key in sysconfig.get_path_names(): ... print(f'{key} = {sysconfig.get_path(key, scheme="rpm_prefix")}') ... stdlib = /usr/lib64/python3.10 platstdlib = /usr/lib64/python3.10 purelib = /usr/lib/python3.10/site-packages platlib = /usr/lib64/python3.10/site-packages include = /usr/include/python3.10 scripts = /usr/bin data = /usr ``` However this installation scheme is entirely Fedora 36+ specific and such code will not work on other operating systems (or even older Fedora releases). ## pip/setup.py installation with `--prefix` When pip or `python setup.py` installation is invoked with the `--prefix` option, the `/usr` part of the standard installation path is replaced with the given `--prefix` value. Note that **`/local/` is not a part of the prefix** but a part of the installation scheme. Hence, [despite some quite reasonable expectations](https://bugzilla.redhat.com/show_bug.cgi?id=2026979), the following invocation: ```console $ sudo pip install --prefix /usr Pello ``` Will *still* install the `Pello` package to `/usr/local/lib/python3.10/site-packages/`. And this: ```console $ sudo pip install --prefix /usr/local Pello ``` Will even install the `Pello` package to `/usr/local/local/lib/python3.10/site-packages/`. The only supported way to explicitly install a Python package directly to `/usr/lib(64)/python3.10/site-packages/` is to build an RPM package with it and install it. Python checks the `$RPM_BUILD_ROOT` environment variable and selects the `rpm_prefix` installation scheme when it is set. Strictly for testing purposes, it is possible to set the variable outside of RPM build environment to simulate installation from an RPM package. **Beware, this usage might have unexpected consequences** on a production system, including an entirely unrecoverable breakage. ```console $ sudo env RPM_BUILD_ROOT=/ pip install Pello ``` ### Installation with `--prefix` and `--root` The change in behavior also applies when a custom `--root` value is passed together with `--prefix`. The following command will install `Pello` to `~/myroot/usr/local/lib/python3.10/site-packages/`: ```console $ pip install --prefix /usr --root ~/myroot Pello ``` To install it to `~/myroot/usr/lib/python3.10/site-packages/`, the simulated RPM environment can be used: ```console $ RPM_BUID_ROOT=~/myroot pip install --prefix /usr --root ~/myroot Pello ``` ## RPM build–related caveats When Python runs during RPM build, it selects the `rpm_prefix` installation scheme. This behavior is triggered when the `$RPM_BUILD_ROOT` environment variable is set. That has several caveats: ### Executing Python in Python's subprocess If the Python code that runs in RPM build (for example in `%check`) executes another Python instance via a subprocess, it is relatively easy to inadvertently unset all environment variables. When this happens, the *inner* Python will not know it runs within RPM build and will return paths with the `/local/` infix. In the most trivial example, the surrounding environment variables are implicitly passed to subprocess and everything works as expected: ```python >>> import os, subprocess, sys >>> 'RPM_BUILD_ROOT' in os.environ True >>> command = [sys.executable, '-c', 'import sysconfig; print(sysconfig.get_path("purelib"))'] >>> subprocess.check_output(command) b'/usr/lib/python3.10/site-packages\n' ``` But when a custom environment is passed, it breaks the detection, because `$RPM_BUILD_ROOT` is no longer set: ```python >>> subprocess.check_output(command, env={'FOO': 'bar'}) b'/usr/local/lib/python3.10/site-packages\n' ``` A solution is to always make a copy of the surrounding environment, then editing it and passing it to the subprocess. That is a generally valid Python advice. ```python >>> env = os.environ | {'FOO': 'bar'} >>> subprocess.check_output(command, env=env) b'/usr/lib/python3.10/site-packages\n' ``` Example fixes from affected open source projects: - [argparse-manpage: Preserve the environment when running pip or setup.py from within the tests](https://github.com/praiskup/argparse-manpage/pull/39) ### `%(...)` RPM macros When RPM macros in the form of `%(command ...)` are expanded, `$RPM_BUILD_ROOT` is not yet set. Hence, Python does not know it is invoked from RPM build and the paths returned by `sysconfig.get_path(...)` contain `/local/`. To fix this, set `$RPM_BUILD_ROOT` in the macro definition (to any value, even empty). For example a macro defined like this: ```rpm %global python3_scripts_dir %(python3 "import sysconfig; print(sysconfig.get_path('scripts'))") ``` Needs to be changed to this: ```rpm %global python3_scripts_dir %(RPM_BUILD_ROOT= python3 "import sysconfig; print(sysconfig.get_path('scripts'))") ``` The affected RPM macros supplied by Fedora's `python-rpm-macros` packages have all been [changed accordingly](https://src.fedoraproject.org/rpms/python-rpm-macros/pull-request/110). ## Paths for bootstrapping Python virtual environments If your Python code uses installation schemes to determine paths to be used in created virtual environments, and the Python interpreter executing that code does not run from a Python virtual environment itself, the paths will not match. To bootstrap Python virtual environments, the code should use the `venv` installation scheme (but only if it exists). ```python >>> scheme = 'venv' if 'venv' in sysconfig.get_scheme_names() else None >>> sysconfig.get_path('purelib', scheme=scheme, vars={'base': '<venv>'}) '<venv>/lib/python3.10/site-packages' >>> sysconfig.get_path('scripts', scheme=scheme, vars={'base': '<venv>'}) '<venv>/bin' ``` The `venv` scheme is currently Fedora specific, but other Python distributors (such as [Ubuntu Deadsnakes](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa)) may define it as well. Due to checking if the `venv` install scheme exists the code is functional on other operating systems as well, as it falls back to a backwards compatible behavior. Example fixes from affected open source projects: - [virtualenv: Favor the "venv" sysconfig install scheme over the default and distutils scheme](https://github.com/pypa/virtualenv/pull/2209) - [build: Prefer the venv installation scheme if it exists](https://github.com/pypa/build/pull/434)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully