Python dev in centos

How To Install Python 3 and Set Up a Programming Environment on CentOS 8

A versatile programming language, Python can be used for many different programming projects. Inspired by the British comedy group Monty Python, the development team behind Python wanted to make a language that was fun to use. An increasingly popular language with many different applications, Python is a great choice for beginners and experienced developers alike.

This tutorial will guide you through installing Python 3 on a CentOS 8 cloud server and setting up a programming environment via the command line.

Prerequisites

You will need a CentOS 8 server with a non-root superuser account.

Step 1 — Preparing the System

Before we begin with the installation, let’s make sure to update the default system applications to ensure we have the latest versions available.

We will be using the open-source package manager tool DNF, which stands for Dandified YUM the next-generation version of the Yellowdog Updater, Modified (that is, yum). DNF is a package manager that is now the default package manager for Red Hat based Linux systems like CentOS. It will let you install, update, and remove software packages on your server.

Let’s first make sure that our package manager is up to date by running this command:

The -y flag is used to alert the system that we are aware that we are making changes, preventing the terminal from prompting us to confirm.

Once everything is installed, our setup is in place and we can go on to install Python 3.

Step 2 — Installing and Setting Up Python 3

CentOS is derived from RHEL (Red Hat Enterprise Linux), which has stability as its primary focus. Because of this, tested and stable versions of applications are what is most commonly found on the system and in downloadable packages, so using the CentOS package manager you will find earlier versions of Python than the most recent release.

When this process is complete, we can check to make sure that the installation was successful by checking for its version number with the python3 command:

With a version of Python 3 successfully installed, we will receive the following output:

Next, we’ll install the CentOS Development Tools, which are used to allow you to build and compile software from source code:

With that installed, we’ll go over how to set up Python development projects in the next section.

Step 3 — Setting Up a Virtual Environment

With Python installed and our system set up, we can go on to create our programming environment with venv.

Читайте также:  Регулярные выражения удалить символ python

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

Setting up a programming environment provides us with greater control over our Python projects, as well as over different packages and versions. This is especially important when working with third-party packages.

You can set up as many Python programming environments as you would like. Each environment is essentially a directory or folder on your server that has a few scripts to set it up as an environment.

Choose which directory you would like to put your Python programming environments in, or create a new directory with mkdir , as in:

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command. You should use an environment name that makes sense for you, here we are calling it my_env .

In this case the environment is my_env , and this new directory contains a few items that we can display if we use the ls command in that directory:

Output
bin include lib lib64 pyvenv.cfg

Together, these files work to isolate your Python work from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.

To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script in the bin directory:

Your prompt will now be prefixed with the name of your environment, in this case it is called my_env :

This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

The Python package manager pip is already installed. A tool for use with Python, we will use pip to install and manage programming packages we may want to use in our development projects. You can install Python packages by typing:

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip install numpy .

Note: Within the virtual environment, you can use the command python instead of python3 , and pip instead of pip3 . If you use Python 3 or pip3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

Читайте также:  Blender module for python

After following these steps, your virtual environment is ready to use.

Step 4 — Creating a “Hello, World!” Program

Now that we have our virtual environment set up, let’s create the traditional “Hello, World!” program to test our installation. This will make sure that our environment is working and gives us the opportunity to become more familiar with Python if we aren’t already.

To do this, we’ll open up a command-line text editor such as vi and create a new file:

Once the text file opens up in our terminal window, we will have to type i to enter insert mode, and then we can write our first program:

Now press ESC to leave insert mode. Next, type 😡 then ENTER to save and exit the file.

We are now ready to run our program:

The hello.py program that you just created should cause the terminal to produce the following output:

To leave the environment, type the command deactivate and you’ll return to your original directory.

Conclusion

Congratulations! At this point you have a Python 3 programming environment set up on your CentOS 8 server and can begin a coding project!

With your machine ready for software development, you can continue to learn more about coding in Python by following along with our How To Code in Python series, or downloading the free HowTo Code in Python eBook.

To explore machine learning projects in particular, refer to our Python Machine Learning Projects eBook.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

How to install python3-devel on red hat 7

I am trying to install something in my virtual environment, which uses anaconda python 3.6. I get the gcc failed with exit status 1 , hinting on the absence of the right python3-devel package, as described in error: command ‘gcc’ failed with exit status 1 while installing eventlet. To fix the error, I tried to install the python3-devel package on my server running RHEL 7.3. I did yum install python3-devel , but got a ‘package not found’ error. Then I found https://serverfault.com/questions/710354/repository-for-python3-devel-on-centos-7, which hints to the python34-devel package in the EPEL repository. I installed it using yum, but upon trying to install something in my virtual environment, I still get the gcc failed with exit status 1 error. Does someone know how I can fix this? All help would be much apprechiated.

Hey, thanks for your reply. As I describe in my post, I believe the cause to be the absence of the python3-devel package.

As a sidenote, I would recommend Red Hat Software Collections for installing Python3 instead of EPEL — you get official support with that. See access.redhat.com/solutions/472793

Читайте также:  Decompile java class file to java file

3 Answers 3

Search for the package in yum , use the following command:

yum search python3 | grep devel 

It will list all the available devel packages. The result will be somewhat like this

python3-cairo-devel.x86_64 : Libraries and headers for python3-cairo python3-devel.x86_64 : Libraries and header files needed for Python 3 : development python34-devel.x86_64 : Libraries and header files needed for Python 3 : development 

Then you can choose the package you want to install from the list , suppose if you want to to install python3-devel , execute the following

yum install -y python3-devel.x86_64 

# yum search python3 // Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager // Warning: No matches found for: python3 No matches found — couldn’t find it on my system

I want to give you 2 points but SO won’t let me . thanks man . this is BY FAR the most straight forward answer

The above did not work for me, probably because I did not have repositories setup, so I found this here which worked:draculaservers.com/tutorials/install-python-3-centos-7 Or just this—————————————- sudo yum install centos-release-scl sudo yum install rh-python36 scl enable rh-python36 bash python —version

Which repo is python3-devel found? I have base, extras, epel and ius configured and it is not found. Pip search can’t find it. I can only seem to find it on rpmfind.net and that is a source rpm.

I thought I might update this for 2020. As of RHEL 7.7, python-devel is not available in EPEL, it has been retired by Fedora Project. All I wanted for today was the python h files, and this got me there:

# yum install python3-devel.x86_64 --enablerepo=rhel-7-server-optional-rpms 

We do have one of the Redhat No-Cost Developer licenses, but I am not sure that is required for the optional-rpms.

PS, this was helpful in verifying which packages of interest were in optional-rpms

# yum repo-pkgs rhel-7-server-optional-rpms list | grep python3 

There are no python3-* packages from Red Hat in Red Hat Enterprise Linux 6 or 7.

However there are python3-* packages available if you enable third party repos like EPEL or IUS. But, these are not supported by Red Hat. Chances are if you are running RHEL, your organization has a preference for supported packages.

You can get supported Python 3 packages from Red Hat via Red Hat Software Collections (RHSCL). Currently Python 3.6 is the newest available, the package name is rh-python36. Installing the RHSCL package will also install rh-python36-devel and a number of other packages.

Don’t forget to install @development first, so you have gcc, make, etc. for building any dynamically loaded shared objects.

$ su - # subscription-manager repos --enable rhel-7-server-optional-rpms \ --enable rhel-server-rhscl-7-rpms # yum -y install @development # yum -y install rh-python36 # yum -y install rh-python36-numpy \ rh-python36-scipy \ rh-python36-python-tools \ rh-python36-python-six # exit 

The blog linked above has lots of tips for working with Python, virtual environments, as well as software collections on Red Hat.

Источник

Оцените статью