One place for hosting & domains

      Programming

      How To Install Python 3 and Set Up a Local Programming Environment on Ubuntu 20.04


      Introduction

      Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Quick to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

      This tutorial will guide you through installing Python 3 on your local Linux machine and setting up a programming environment via the command line. This tutorial will explicitly cover the installation procedures for Ubuntu 20.04, but the general principles apply to any other distribution of Debian Linux.

      Prerequisites

      You will need a computer or virtual machine with Ubuntu 20.04 installed, as well as have administrative access to that machine and an internet connection. You can download this operating system via the Ubuntu 20.04 releases page.

      Step 1 — Setting Up Python 3

      We’ll be completing our installation and setup on the command line, which is a non-graphical way to interact with your computer. That is, instead of clicking on buttons, you’ll be typing in text and receiving feedback from your computer through text as well.

      The command line, also known as a shell or terminal, can help you modify and automate many of the tasks you do on a computer every day, and is an essential tool for software developers. There are many terminal commands to learn that can enable you to do more powerful things. The article “An Introduction to the Linux Terminal” can get you better oriented with the terminal.

      On Ubuntu 20.04, you can find the Terminal application by clicking on the Ubuntu icon in the upper-left hand corner of your screen and typing “terminal” into the search bar. Click on the Terminal application icon to open it. Alternatively, you can hit the CTRL, ALT, and T keys on your keyboard at the same time to open the Terminal application automatically.

      Ubuntu Terminal

      Ubuntu 20.04 ships with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

      • sudo apt update
      • sudo apt -y upgrade

      The -y flag will confirm that we are agreeing that all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades.

      Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:

      You will receive output in the terminal window that will let you know the version number. The version number may vary, but it will be similar to this:

      Output

      Python 3.8.10

      To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading “How To Import Modules in Python 3.”

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      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 pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python-dev

      Press y if prompted to do so.

      Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

      Step 2 — Setting Up a Virtual Environment

      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 and over how different versions of packages are handled. This is especially important when working with third-party packages.

      You can set up as many Python programming environments as you want. Each environment is basically a directory or folder in your computer that has a few scripts in it to make it act as an environment.

      While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

      • sudo apt install -y python3-venv

      With this installed, we are ready to create environments. Let’s either choose which directory we would like to put our Python programming environments in, or create a new directory with mkdir, as in:

      • mkdir environments
      • cd environments

      Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

      Essentially, this sets up a new directory that contains a few items which we can view with the ls command:

      Output

      bin include lib lib64 pyvenv.cfg share

      Together, these files work to make sure that your projects are isolated 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. Python Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

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

      • source my_env/bin/activate

      Your prompt will now be prefixed with the name of your environment, in this case it is called my_env. Your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

      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.

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

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

      Step 3 — Creating a “Hello, World” Program

      Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with 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 nano and create a new file:

      When the text file opens up in the terminal window we’ll type out our program:

      print("Hello, World!")
      

      Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

      Once you exit out of nano and return to your shell, we’ll run the program:

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

      Output

      Hello, World!

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

      Conclusion

      Congratulations! At this point you have a Python 3 programming environment set up on your local Ubuntu machine and can begin a coding project!

      If you are using a different local machine, refer to the tutorial that is relevant to your operating system in our “How To Install and Set Up a Local Programming Environment for Python 3” series. Alternatively, if you’re using an Ubuntu server, you can follow the “How To Install Python and Set Up a Programming Environment on an Ubuntu 20.04 Server” tutorial.

      With your local machine ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Programming Project tutorials.



      Source link

      How To Install Python 3 and Set Up a Programming Environment on Ubuntu 20.04 [Quickstart]


      Introduction

      Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

      This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 20.04 server. For a more detailed version of this tutorial, with more thorough explanations of each step, please refer to How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server.

      Step 1 — Update and Upgrade

      Logged into your Ubuntu 20.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

      • sudo apt update
      • sudo apt -y upgrade

      Confirm installation if prompted to do so.

      Step 2 — Check Version of Python

      Check which version of Python 3 is installed by typing:

      You’ll receive output similar to the following, depending on when you have updated your system.

      Output

      Python 3.8.2

      Step 3 — Install pip

      To manage software packages for Python, install pip, a tool that will help you manage libraries or modules to use in your projects.

      • sudo apt install -y python3-pip

      Python packages can be installed by typing:

      • pip3 install package_name

      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 pip3 install numpy.

      There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

      • sudo apt install build-essential libssl-dev libffi-dev python3-dev

      Step 5 — Install venv

      Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

      • sudo apt install -y python3-venv

      Step 6 — Create a Virtual Environment

      You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you should call yours something meaningful to your project.

      Step 7 — Activate Virtual Environment

      Activate the environment using the command below, where my_env is the name of your programming environment.

      • source my_env/bin/activate

      Your command prompt will now be prefixed with the name of your environment:

      Step 8 — Test Virtual Environment

      Open the Python interpreter:

      Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

      You’ll know you’re in the interpreter when you receive the following output:

      Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
      [GCC 9.3.0] on linux
      Type "help", "copyright", "credits" or "license" for more information.
      >>>
      

      Now, use the print() function to create the traditional Hello, World program:

      Output

      Hello, World!

      Step 9 — Deactivate Virtual Environment

      Quit the Python interpreter:

      Then exit the virtual environment:

      Further Reading

      From here, there is a lot you can learn about Python, here are some links related to this guide:



      Source link

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


      Introduction

      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.

      To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

      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.

      • sudo dnf install python3 -y

      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:

      Output

      Python 3.6.8

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

      • sudo dnf -y groupinstall development

      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.

      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:

      • mkdir environments
      • cd environments

      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:

      • source my_env/bin/activate

      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:

      • sudo pip install package_name

      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.

      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:

      print("Hello, World!")
      

      Now press ESC to leave insert mode. Next, type :x 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:

      Output

      Hello, World!

      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.



      Source link