Hướng dẫn rhel alternatives python
A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more. Log in for full access Log In This article shows how to install Python 3, Using Python virtual environments is a best practice to isolate project-specific dependencies and create reproducible environments. Other tips and FAQs for working with Python and software collections on RHEL 7 are also covered. There are a number of different ways to get Python 3 installed on RHEL. This article uses Red Hat Software Collections because these give you a current Python installation that is built and supported by Red Hat. During development, support might not seem that important to you. However, support is important to those who have to deploy and operate the applications you write. To understand why this is important, consider what happens when your application is in production and a critical security vulnerability in a core library (for example SSL/TLS) is discovered. This type of scenario is why many enterprises use Red Hat. Python 3.6 is used in this article. It was the most recent, stable release when this was written. However, you should be able to use these instructions for any of the versions of Python in Red Hat Software Collections including 2.7, 3.4, 3.5, and future collections such as 3.7. In this article, the following topics are discussed:
TL;DRHere are the basic steps so you can just get going. See below for explanations and more details. How to install Python 3 on RHEL
Using Python 3 on RHEL
If you start a new session, here are the steps for using your virtual environment:
Why use Red Hat Software CollectionsThe benefit of using Red Hat Software Collections is that you
can have multiple versions of Python installed at the same time along with the base Python 2.7 that shipped with RHEL 7. You can easily switch between versions with Note: The latest stable packages for .Net Core, Go, Rust, PHP 7, Ruby 2.5, GCC, Clang/LLVM, Nginx, MongoDB, MariaDB, PostgreSQL, and more are all Using software collections requires an extra
step because you have to enable the collection you want to use. Enabling just adds the necessary paths ( Installation PrerequisitesInstall development tools including GCC, make, and gitIf you install modules that depend on compiled code you'll need the tools to compile them. If you haven't already installed development tools run the following command:
Enable repos with additional developer toolsWhile the default/base RHEL software repos have many development tools, these are the older versions that are shipped with the OS and are supported for the full 10-year life of the OS. Packages that are updated more frequently and have a different support lifecycle are distributed in other repos that aren't enabled by default. Red Hat Software
Collections are in the To enable the additional repos, run the following commands as
Notes:
To see which repos are available for your current subscription, run the following command:
To see which repos are enabled, use
Install Python 3You can now install Python 3.6 (or other versions in RHSCL) with
Notes:
Install additional packagesOptionally, you may want to install the following RPM packages that are part of the software collection:
Example:
Note: By default system modules will not be used with Python virtual environments. Use the option How to use Python 3 (scl enable)Python 3 is now installed. You no longer need to run under the As previously mentioned, software collections are installed under
Note: Enabling the Python collection makes the NOTE: See How to permanently enable a software collection below to permanently put Python 3 in your path. Create a Python virtual environment (best practice)Using Python virtual environments is a best practice to isolate project-specific dependencies and create reproducible environments. In other words, it's a way to avoid conflicting dependencies that lead to dependency hell. Using a virtual environment will let you use Using virtual environments along with By default, virtual environments will not use any system installed modules, or modules installed under your home directory. From an isolation
perspective and for creating reproducible environments this is generally considered the correct behavior. However, you can change that by using the argument Should I use venv or virtualenv or something else?When you install Python 3 from Red Hat Software Collections,
So for all the recent versions of Python 3, If you work with Python 2.7, you'll need to use The commands to create the virtual environments differ only in the module name used. Once created, the command to activate the virtual environment is the same. Note: for Create and activate a virtual environment with venvIf you haven't already done so, enable the
Now create the virtual environment. To avoid any surprises, use an explicit version number for running Python:
Anytime you need to activate the virtual environment, run the following command.
Note: once you've activated a virtual environment, your prompt will change to remind you that you are working in a virtual environment. Example:
Note: When you log in again, or start a new session, you will need to activate the virtual environment using the For more information, see Virtual Environments and Packages in the Python 3 tutorial at docs.python.org. Create and activate a virtual environment with virtualenvIf you haven't already done so, enable the
Now create the virtual environment. To avoid any surprises, use an explicit version number for running Python:
Anytime you need to activate the virtual
environment, run the following command. Note: you should already have run
Note: once you've activated a virtual environment, your prompt will change to remind you that you are working in a virtual environment. Example:
Note: When you log in again, or start a new session, you will need to activate the virtual environment using the For more information, see Installing packages using pip and virtualenv in the Python Packaging User Guide. Managing application dependencies with pipenvFrom the Python Packaging User Guide tutorial, Managing Application Dependencies: "Pipenv is a dependency manager for Python projects. If you're familiar with Node.js' npm or Ruby's bundler, it is similar in spirit to those tools. While pip alone is often sufficient for personal use, Pipenv is recommended for collaborative projects as it's a higher-level tool that simplifies dependency management for common use cases." With pipenv you no longer need to use
Creating and using isolated environments with
To activate a Pipenv environment, cd into that directory and run
Pipenv is similar to For more information see:
General tips for working with PythonThe python command: Avoid surprises by using a version numberTo avoid surprises, don't type At
a minimum, always use The same problem occurs with any of the Python utilities such as Scripts that start with #!/usr/bin/env python might breakFor many years the advice was to start scripts with Use which to determine which Python version will be runUse the Examples:
Avoid Python wrapper scripts such as virtualenv: Use the module nameSome Python utilities are put in your path as a wrapper script in a The
problem with wrapper scripts is the same ambiguity that happens when typing Note: There are several directories that wrapper scripts can
reside in. Which version you get is dependent on your path, which changes when you enable software collections and/or activate virtual environments. Modules installed with You can avoid the surprises from the path issues by running the module directly from a specific version of Python by using Recommendations:
Do not run pip install as root (or with sudo)Running
Using virtual environments will allow you to isolate the modules you install for each project from the modules that are part of the Python installation from Red Hat. Using virtual environments is considered a best practice to create isolated environments that provide the dependencies needed for a specific purpose. You don't need to use If you aren't using virtual environments, or need a module/tool to be available outside of a virtual environments, use In case you think this is overly dire, see this xkcd comic. Don't forget to hover so you see the alt text. Use virtual environments instead of pip --userSome guides recommend using The exception to this advice is modules and tools that you need
to use outside of virtual environments. The primary example is Don't use the system Python for your own projectsThe Python version installed in At some point, you might want to run your code on a different version of the OS. That OS will likely have a different version of Python installed as The one exception to the above is if you are writing system administration tools. In that case, you should use the Python in Tip: If you need to work with
Python 2.7, install the Don't change or overwrite /usr/bin/python, /usr/bin/python2, or /usr/bin/python2.7As mentioned above, the system Python is part of Red Hat Enterprise Linux 7 and is used by critical system utilities such as Software collection tipsEnable the Python collection *before* the virtual environmentYou should always enable the Python software collection before using any of Python virtual environment utilities to create or activate an environment. In order for things to work correctly, you need to have your desired version of Python in your path because it will be needed by the Python virtual environment. A number of problems, some of which are subtle, come up if you try to
enable/activate in the wrong order. Example for
When reactivating later in a new shell:
Example for
When reactivating later in a new shell:
How to permanently enable a software collectionTo permanently add Python 3 to your path(s), you can add an There are a few caveats with this approach:
Using your preferred text editor, add the
following line to your
Note: you could also add the How to use Python 3 from RHSCL in the #! (shebang) line of a scriptYou can create a script that will use Python from the
software collection without a requirement for
Note: You may be tempted to try using just the full path to How to see which software collections are installedYou can use the command
How to tell which software collections are enabledThe environment variable
In scripts, you can use How can I find a list of Red Hat Software Collections and how long they are supported?See Red Hat Software Collections Product Life Cycle on the Red Hat Customer Portal. It has a list of Red Hat Software Collections packages and support information. You can also check the release notes for the most recent release of Red Hat Software Collections. Find additional RPM packages and see other available versionsYou can use To search for other packages that are part of the
Starting with the Python 3.4 collection, the collection and package names are all prefixed with
Note: to see the
available packages in the Python 2.7 collection, search for
You can, of course, just search for TroubleshootingPython: error while loading shared librariesThis error occurs when you are trying to run a binary but the shared libraries it depends on can't be found. Typically this occurs when trying to run To see what
environment variables are modified, take a look at
Wrong version of Python when running pythonFirst, running Second, you can also get the wrong version if you've forgotten to enable the software collection. Enabling the software collection puts the collection's The software collection needs to be enabled even if you give the full path to the Error running pip: ImportError cannot import name 'main'If you run This can be worked around in several ways:
Note: To uninstall the upgraded
Can't find virtualenv3.6The The preferred workaround is to avoid the wrapper script entirely and invoke the module directly:
Alternatively, you could create your own symlink in your
More information: Developing in Python on Red Hat PlatformsNick Coghlan and Graham Dumpleton gave a talk Developing in Python on Red Hat Platforms at DevNation 2016. The talk is chock full of information and still very relevant. They include information on building Python applications using containers, using s2i, and deploying to Red Hat OpenShift. I recommend watching the video or at least reviewing the slides. https://www.youtube.com/watch?v=tLTSQiVQ8qk SummaryAfter reading this article you've learned:
Last updated: November 15, 2018 |