Python is a programming language that is really general-purpose, flexible, and popular for data science. If your target is to harvest insights from your data and gain a competitive edge, this tool is suitable. The first of its publish is for the year 1991 which was inspired by the name of the British comedy group Monty Python. You would learn how to store and manipulate data and if you are a beginner it is a good idea to continue on this tutorial. Before all, have a look at Linux VPS plans and catch your own.
To let this tutorial work better, please consider the below Prerequisites:
a non-root user with sudo privileges.
To set up, follow our Initial Setup with Debian 10
Table of Contents
Install Python 3 and Set Up a Programming Environment on Debian 10
Let’s walk through the steps of this tutorial to get your Debian 10 server set up with a Python 3 programming environment. Programming on a server has many advantages and supports collaboration across development projects.
Step 1: Setting Up Python 3
While the Debian Linux ships with both Python 3 and Python 2 pre-installed, you can ensure that your versions are up-to-date. see the below command to update and upgrade the system to work with the Advanced Packaging Tool:
sudo apt update sudo apt -y upgrade
To confirm that you are agreeing for all items to be installed, you will use -y flag.
Once the process is complete, you can check the version of Python 3 that is installed in the system by typing:
You’ll receive output in the terminal window that will let you know the version number. While this number may vary, the output will be similar to this:
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.
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 you have a robust set-up for our programming environment:
sudo apt install build-essential libssl-dev libffi-dev python3-dev
Once Python is set up, and pip and other tools are installed, you 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 server 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 you with greater control over your 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 on your server 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, you will 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, you are ready to create environments. Let’s either choose which directory you 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:
python3.7 -m venv my_env
Essentially, pyvenv sets up a new directory that contains a few items which you can view with the ls command:
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 achieve by typing the following command that calls the activate script:
Your command prompt will now be prefixed with the name of your environment, in this case, it is called my_env. Depending on what version of Debian Linux you are running, 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 you 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 you test your environment. It provides you with the opportunity to become more familiar with Python if we aren’t already.
To do this, you open up a command-line text editor such as nano and create a new file:
(my_env) noodi@ubuntu:~/environments$ nano hello.py
Once the text file opens up in the terminal window you will type out our program:
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, let’s run the program:
The hello.py program that you just created should cause your terminal to produce the following output:
To leave the environment, simply type the command deactivate and you will return to your original directory.
In this article, you have a Python 3 programming environment set up on your Debian 10 Linux server. From now on you can begin the coding project! I case you need to learn this subject on another OS, read more on our article Tutorial Install Python 3.8 on Centos/RHEL 8 Linux AND How to install Python 3 on Ubuntu 20.04. But if you work with windows, this guide could be suitable.