Get started developing with Python 3.4 from Red Hat Software Collections in under 10 minutes.
In this tutorial, you will set your system up to install software from Red Hat Software Collections (RHSCL), which provides the latest development technologies for Red Hat Enterprise Linux. Then, you will install Python 3.4 and run a simple “Hello, World” application. The whole tutorial should take less than 10 minutes to complete.
You will need a Red Hat Enterprise Linux 7 system with a current Red Hat subscription that allows you to download software and updates from Red Hat. Access to the Red Hat Software Collections (RHSCL) is included with many Red Hat Enterprise Linux (RHEL) subscriptions. For more information about which subscriptions include RHSCL, see How to use Red Hat Software Collections (RHSCL) or Red Hat Developer Toolset (DTS).
If you don’t have a Red Hat Enterprise Linux subscription, you can try it for free. Get started with an evaluation at https://access.redhat.com/products/red-hat-enterprise-linux/evaluation. Select the Red Hat Enterprise Linux Developer Workstation option to ensure your evaluation will include both RHSCL and DTS.
If you encounter difficulties at any point, see Troubleshooting and FAQ.
In this step you will configure your system to obtain software, including the latest dynamic languages, from the RHSCL software repository. Instructions are provided for both the command line (CLI) and graphical user interface (GUI).
Red Hat Subscription Manager can be started from the System Tools group of the Applications menu. Alternatively, you can start it from the command prompt by typing subscription-manager-gui
.
Select Repositories from the System menu of the subscription manager. In the list of repositories, check the Enabled column for rhel-server-rhscl-7-rpms and rhel-7-server-optional-rpms. If you are using a desktop version of Red Hat Enterprise Linux, the repositories will be named rhel-desktop-rhscl-7-rpms and rhel-7-desktop-optional-rpms. Note: After clicking, it might take several seconds for the check mark to appear in the enabled column.
If you don’t see any RHSCL repositories in the list, your subscription might not include it.
See Troubleshooting and FAQ for more information.
You can add or remove software repositories from the command line using the subscription-manager
tool as the root user. Use the --list
option to view the available software repositories and verify that you have access to RHSCL:
$ su -
# subscription-manager repos --list | egrep rhscl
If you don’t see any RHSCL repositories in the list, your subscription might not include it. See Troubleshooting and FAQ for more information.
If you are using a desktop edition of Red Hat Enterprise Linux, change -server-
to -desktop-
in the following commands:
# subscription-manager repos --enable rhel-server-rhscl-7-rpms
# subscription-manager repos --enable rhel-7-server-optional-rpms
In this next step you will install Python 3.4 from RHSCL:
$ su -
# yum install rh-python34
If you need help, see Troubleshooting and FAQ.
Under your normal user ID, start a Terminal window. First, use scl enable
to add Python 3 to your environment, then run Python 3 in interactive mode to see the version number:
$ scl enable rh-python34 bash
$ python3
Python 3.4.2 (default, Mar 25 2015, 04:25:42)
>>> print("Hello, Red Hat Developers World")
Hello, Red Hat Developers World
>>> quit()
The next step is to create a Python program that can be run from the command line. Using your preferred text editor, create a file named hello.py
:
$ nano hello.py
Add the following text to the file:
#!/usr/bin/env python3
import sys
version = "Python %d.%d" % (sys.version_info.major, sys.version_info.minor)
print("Hello, Red Hat Developers World from",version)
Save it and exit the editor. Then make the script executable and run it:
$ chmod +x hello.py
$ ./hello.py
Hello, Red Hat Developers World from Python 3.4
If you get the error: python3 command not found, you need to run scl enable rh-python34 bash
first.
The software packages in RHSCL are designed to allow multiple versions of software to be installed concurrently. To accomplish this, the desired package is added to your runtime environment as needed with the scl enable
command. When scl enable
runs, it modifies environment variables and then runs the specified command. The environmental changes only affect the command that is run by scl
and any processes that are run from that command. The steps in this tutorial run the command bash
to start a new interactive shell to work in the updated environment. The changes aren’t permanent. Typing exit
will return to the original shell with the original environment. Each time you login, or start a new terminal sesssion, scl enable
needs to be run again.
While it is possible to change the system profile to make RHSCL packages part of the system’s global environment, this is not recommended. Doing this can cause conflicts and unexpected problems with other applications because the system version of the package is obscured by having the RHSCL version in the path first.
To make one or more RHSCL packages a permanent part of your development environment, you can add them to the login script for your specific user ID. This is the recommend approach for development as only the processes that are run under your user ID will be affected.
Using your preferred text editor, add the following line to the end of ~/.bashrc
:
source scl_source enable rh-python34
After making the change, you should log out and log back in again.
When you deliver an application that uses RHSCL packages, a best practice is to have your startup script handle the scl enable
step for your application. You should not ask your users to change their environment as this is likely to create conflicts with other applications.
Python 3 Tutorial at Python.org
https://docs.python.org/3/tutorial/
Find additional Python components
$ yum list available rh-python34-\*
View the list of software available in RHSCL
$ yum --disablerepo="*" --enablerepo="rhel-server-rhscl-7-rpms" list available
-
How to use Red Hat Software Collections (RHSCL) or Red Hat Developer Toolset (DTS)
-
Red Hat Software Collection 2.0 Documentation
-
Red Hat Software Collections 2.0 Packaging Guide
Developers should read this guide to get a more complete understanding of how software collections work, and how to deliver software that uses RHSCL.
Red Hat delivers the resources and ecosystem of experts to help you be more productive and build great solutions. Register for free at developers.redhat.com.
-
The RHSCL repository is not available or is not found on my system.
The name of the repository depends on whether you have a server or desktop version of Red Hat Enterprise Linux installed.
Some Red Hat Enterprise Linux subscriptions do not include access to RHSCL. See How to use Red Hat Software Collections (RHSCL) or Red Hat Developer Toolset (DTS).
-
yum install fails due to a missing dependency.
These packages are in the optional RPMs repository, which is not enabled by default. See [Enable Red Hat Software Collections (RHSCL)] for how enable both the optional RPMs and RHSCL repositories.
-
How can I find out what RHSCL packages are installed?
scl --list
will show the list of RHSCL packages that have been installed, whether they are enabled or not.$ scl --list rh-perl520 rh-php56 rh-python34 rh-ruby22
-
How do I find out if there is a newer version of Python in the RHSCL?
How do I find out what version of Python is available in the RHSCL?
I have the RHSCL repository enabled, but I can’t find the Python version listed in this tutorial.
Use the following command to find packages with matching names:
# yum list available rh-python\*
-
I’ve installed Python 3.4 from RHSCL, but it is not in my path.
I can’t find python3
When I type ‘python’, I get Python 2 instead of Python 3.
RHSCL does not alter the system path. You need to use
scl enable
to change the environment for your session:$ scl enable rh-python34 bash
For more information see the Red Hat Software Collection 2.0 Documentation.
-
When I try to run Python 3, I get an error about a missing shared library, libpython3.
This is due to not having run
scl enable
first. Whenscl enable
runs, in addition to setting up the command search PATH, it also sets up the search path for shared libraries, LD_LIBRARY_PATH. -
Some Python code/examples I’ve tried don’t work with Python 3 from RHSCL.
Python 3.x is a new version of the Python language that is incompatible with the previous 2.x series. The version of Python included with Red Hat Enterprise Linux in
/usr/bin/python
is from the Python 2.x series. There is a large amount of code written for Python 2.x that will not run without modification on Python 3.x.For more information see Should I use Python 2 or Python 3 for my development activity? at Python.org.
After you have added Python 3 to your environment by using
scl enable
, the commandpython
as well aspython3
will run Python 3 from RHSCL. Python 2 can be run by using the commandpython2
with or without RHSCL. Therefore, it is recommended that you use either the commandpython2
orpython3
to ensure that you get the version you are expecting. -
Some existing Python programs fail after I’ve enabled Python 3 from RHSCL.
See the previous question about Python 2 and Python 3 incompatibility. Scripts that have a first line of
#!/usr/bin/env python
will pick up which ever version of Python is first in your path. This is usually done to avoid hard coding a specific location like/usr/bin
or/usrlocal/bin
. Unfortunately on a system with both Python 2 and Python 3 installed, this is ambiguous.To fix the problem, change the first line to specify
/usr/bin/python
orpython2
:#!/usr/bin/python
or#!/usr/bin/env python2