python | LinuxHostSupport Linux Tutorials and Guides Tue, 25 Jun 2024 15:55:14 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 How to Install Python on Debian 12 https://linuxhostsupport.com/blog/how-to-install-python-on-debian-12/ https://linuxhostsupport.com/blog/how-to-install-python-on-debian-12/#respond Tue, 30 Jul 2024 17:30:00 +0000 https://linuxhostsupport.com/blog/?p=2138 Python is a versatile programming language that can run on almost any system architecture, from web development to machine learning, and can be used for applications in various fields. Besides its versatility, Python is also relatively easy for beginners to learn, making it one of the most popular programming languages. This tutorial will show you […]

The post How to Install Python on Debian 12 appeared first on LinuxHostSupport.

]]>
Python is a versatile programming language that can run on almost any system architecture, from web development to machine learning, and can be used for applications in various fields. Besides its versatility, Python is also relatively easy for beginners to learn, making it one of the most popular programming languages. This tutorial will show you how to install Python on Debian 12.

Prerequisites

  • A Debian 12 VPS
  • SSH root access or a user with sudo privileges is required

Conventions

# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user

Login to VPS and Update the System

First of all, we need to log in to our Debian 12 VPS through SSH:

ssh root@IP_Address -p Port_number

Replace “root” with a user with sudo privileges or root if necessary. Replace “IP_Address” and “Port_Number” with your server’s IP address and SSH port number. Next, let’s make sure that we’re on Debian 12. You can do that like this:

# lsb_release -a

The command should return an output similar to this:

No LSB modules are available.
Distributor ID: Debian
Description: Debian GNU/Linux 12 (bookworm)
Release: 12
Codename: bookworm

Before starting, you have to make sure that all Debian packages installed on the server are up to date. You can do this by running the following commands:

# apt update -y

Install Python from Source

Debian 12 ships Python 3.11 as the default version. You can verify this by executing the command below:

# apt show python3

The command will return an output like this:

Package: python3
Version: 3.11.2-1+b1
Priority: optional
Section: python
Source: python3-defaults (3.11.2-1)
Maintainer: Matthias Klose doko@debian.org
Installed-Size: 82.9 kB
Provides: python3-profiler, python3-supported-max (= 3.11), python3-supported-min (= 3.11)
Pre-Depends: python3-minimal (= 3.11.2-1+b1)
Depends: python3.11 (>= 3.11.2-1~), libpython3-stdlib (= 3.11.2-1+b1)
Suggests: python3-doc (>= 3.11.2-1+b1), python3-tk (>= 3.11.2-1~), python3-venv (>= 3.11.2-1+b1)
Replaces: python3-minimal (<< 3.1.2-2)
Homepage: https://www.python.org/
Tag: devel::interpreter, devel::lang:python, devel::library,
implemented-in::c, implemented-in::python, role::devel-lib,
role::program, role::shared-lib
Download-Size: 26.3 kB
APT-Manual-Installed: no
APT-Sources: http://deb.debian.org/debian bookworm/main amd64 Packages
Description: interactive high-level object-oriented language (default python3 version)
Python, the high-level, interactive object oriented language,
includes an extensive class library with lots of goodies for
network programming, system administration, sounds and graphics.
.
This package is a dependency package, which depends on Debian's default
Python 3 version (currently v3.11).

Ubuntu has deadsnake’s PPA, which allows us to install multiple Python versions on an Ubuntu machine. Although we can install it on our Debian 12 machine using the same Deadsnake’s PPA with a little modification, it is not recommended. So, to install another version of Python 3 on a Debian 12 system, we need to install it from the source. We will show you how to install it.

Install Dependencies

Let’s install the dependencies by invoking this command below.

# apt install openssl \
build-essential \
curl \
gcc \
libbz2-dev \
libev-dev \
libffi-dev \
libgdbm-dev \
liblzma-dev \
libncurses-dev \
libreadline-dev \
libsqlite3-dev \
libssl-dev \
make \
tk-dev \
wget \
git \
zlib1g-dev

Download and Install Python 3.12.3

For example, you want to install Python 3.12. So, you need to go to Python download page and get the link. In this example, you want to download Python 3.12.4.

Now, after getting the download link, let’s download it.

# cd /tmp
# wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz

Then, extract the downloaded file and go to the directory Python-3.12.3

# tar zxf Python-3.12.3.tgz
# cd Python-3.12.3

Finally, we can install it.

# ./configure --prefix=/usr/local
# make
# make install

That’s it — you have Python 3.12 now. It’s installed at /usr/local/bin/python3.12. To install multiple versions, repeat the steps and specify the versions you want to install.

Install Python using pyenv

Before installing pyenv, we need to get the latest version of pyenv frm GitHub. Let’s execute this command below.

# git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Now, we need to edit our ~/.bashrc file

# nano ~/.bashrc

and add these lines to it.

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

Save the file and exit from the editor. To apply the changes, we can source the file.

# source ~/.bashrc

At this point, we can use pyenv. Run this command below to see the available Python versions to install.

# pyenv install -l

Let’s say we are going to install Python 3.10, we can execute this command:

# pyenv install 3.10

Wait until it finishes installing Python. Once completed, you can run this command to check the available version.

# pyenv versions
* system (set by /root/.pyenv/version)
3.10.14

As your screen shows, Python 3.10.14 has been installed but is not activated. The one listed there with an asterisk (*) is currently the active one. The asterisk indicates the currently active version and refers to the system-wide Python version.

To activate Python 3.10.14 globally, let’s run this command

# pyenv global 3.10.14

That’s it. Python 3.10.14 is active now. You can verify it by running the command

# python3 --version

It will return an output like this:

Python 3.10.14

Besides activating it globally, you can also use it locally. Use this command instead:

# pyenv local 3.10.14

You can also install a virtual environment when using pyenv. The command is similar to the one you usually use.

# python -m venv [VENV-NAME]

That’s it all. You have learned how to install Python on Debian 12.

Of course, you don’t have to install Python on Debian 12 yourself if you use one of our server management plans. This means that you can ask our admins to do the installation for you, sit back, and relax. Our admins will install and set up Python on Debian 12 immediately without any additional fee, along with any helpful configurations and optimizations we can do for you. Managing Python is not just about the installation, though. We can also help you optimize your Python installation if you have an active service with us.

If you liked this post on installing Python on Debian 12, please share it with your friends on social media or leave a comment below. Thanks.

The post How to Install Python on Debian 12 appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/how-to-install-python-on-debian-12/feed/ 0
Why Using A Python Virtual Environment Is A Good Choice? https://linuxhostsupport.com/blog/why-using-a-python-virtual-environment-is-a-good-choice/ https://linuxhostsupport.com/blog/why-using-a-python-virtual-environment-is-a-good-choice/#comments Tue, 15 Feb 2022 18:30:00 +0000 https://linuxhostsupport.com/blog/?p=1642 Imagine yourself walking into a grocery store for a specific item. However, to your surprise, there is absolutely no organization within the entire store—no way to distinguish between products; no way to tell what product is for what purpose; simply no way to find your item. You go to the counter and ask the grocer […]

The post Why Using A Python Virtual Environment Is A Good Choice? appeared first on LinuxHostSupport.

]]>
Imagine yourself walking into a grocery store for a specific item. However, to your surprise, there is absolutely no organization within the entire store—no way to distinguish between products; no way to tell what product is for what purpose; simply no way to find your item.

You go to the counter and ask the grocer where the specific product is, but all he tells you is to “search for it.”

Now, what do you do? The only option left for you is to find the item you so desperately want on your own by searching every product in the store.

This grocery store is your computer, your Python package bin. All those disorganized products lying on the shelf are the endless torrent of packages you have installed over the years for your random projects.

The next time you start a project or install some software, you will not understand if the version is up to date, if it collides with another package or python version, or if the package exists at all. Such disorganization can cause setbacks, and that not only disrupts your project but takes away the valuable moment that otherwise could have been used for something more productive.

So what is the solution? Python Virtual Environment.

Python Virtual Environments (virtualenvs) helps decouple and isolate versions of Python and their related pip versions. This authorizes end-users to install and manage their own set of software that is independent of those provided by the system.

One of the primary advantages of using Python is its active developer community and availability of many software packages on pypi.org.

What Is A Python Virtual Environment?

A Python Virtual Environment or virtualenvs is nothing but a directory having a specified structure. It consists of a subdirectory, also called bin_subdirectory, that is connected to a Python interpreter and other subdirectories that hold software installed within the virtual environment.

When imploring the Python interpreter by using the way to the bin subdirectory of virtualenv, the Python interpreter perceives the way to use the related software in the virtualenv (as contrasting to any software installed adjacent to the Python interpreter’s actual place). It is this perspective that virtual environments are virtual and not in the way you know it. They are not virtual like a simulated machine.

You can immediately use it when you set up a virtualenv by imploring Python and applying the complete route to the subdirectory_bin. ( for e.g., my_venv/bin/python3 my_program.py).

For ease, when you set up a virtual environment, it gives you an activated script that you can implore and which will allow you to set the subdirectory bin for your virtual environment initially on your route (simultaneously it upgrades your shell cue and informs you that the change is accomplished).

When your virtualenv is set in motion, you are no longer required to use the full trail to the Python interpreter ( for e.g., my_venv/bin/python3 my_program.py).

Important to remember: If you are imploring the Python script with the complete trail to the Python interpreter in the simulated domain, it will run in the simulated domain despite not being there in a collaborative session where you have used the activate command. This is particularly helpful for Bash scripts and cron jobs, where triggering the virtualenv is problematic.

Should You Always Use A Virtual Environment?

Yes, always.

With a virtual environment, you have complete control over the environment. You would know the package versions that are required to be updated and what versions are installed. Virtual environments give you a replicable and stable environment.

You have complete control over the versions of Python used, the installed packages, and their scheduled upgrades. In fact, the modern versions of the Python support virtual environments over the boundary. Having said that, it is not the only way to maintain a replicable environment. There are other options like using Conda in the home directory or project space that is also available on the CS system.

If you need to have a say on your updates to new packages of Python, all you need is to create your own Python interpreter and create a virtual world based on the interpreter. By this process, you can disengage the servers from the “System Python” update schedule.

There is, however, an exception to using a virtual environment. Suppose you have a simple program that only uses modules from the Python Standard Library (i.e., no third-party modules that would need a pip install). In such a case, you might contemplate not using a simulated environment.

Python has various modules and versions for different applications. A project may require a third-party library, which is installed. Another project also uses a similar directory for retrieval and storage but doesn’t require third-party software. So, the simulated environment can come into play and create a different secluded environment for both the projects, and each project can store and retrieve packages from their specific environments.

Let us consider another scenario where you are creating a web application using Django. Let’s say you are developing two projects, project A and project B.

If project A uses Django 2.2 and project B uses Django 3.2, they would be accumulated in the same directory with the same name, and then an error may occur. In such cases, virtual environments can be really helpful for you to maintain the dependencies of both environments.

Virtual environments aid in these problems by creating unique, isolated environments where all the packages and versions you install only apply to that particular environment. It is like a private island – but for code. Anything that happens on this island doesn’t affect your mainland, and anything that occurs on the island does not.

Once you are done staying on the island, come back to the mainland. In other words, you can come back to the original environment once you are done working on the projects.

Now, let us know more about the pros and cons of Python Virtual Environment:

Python Virtual Environment: Pros And Cons

Pros:

  • You can use any package of Python you want for a particular environment without having to worry about collisions.
  • You can arrange your packages much better and know exactly which packages you need to run your code in case someone else wants to run the code on their machine.
  • Your main Python versions directory does not get flooded with unnecessary Python packages.
  • Comes in stock with Python, and no extra tools are required.
  • Builds a primary virtualenv that works with almost all the tools: requirements.txt supports every domain manager using command pip.

Cons:

  • It acknowledges the software that is installed: builds a domain with the help of everything that had invoked Python to build it, so you are still in the loop of manually controlling the versions of Python.
  • No whistles and no bells but only the installable_ pip in the domain

Conclusion

Virtual environments are of great advantage when working on different projects. If your project is not package-dependent, there is no need for isolated versions and packages. Still, if your project requires even a few packages, there is nothing you can do without the help of virtual environments.

The post Why Using A Python Virtual Environment Is A Good Choice? appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/why-using-a-python-virtual-environment-is-a-good-choice/feed/ 2
Install SciPy through pip https://linuxhostsupport.com/blog/install-scipy-through-pip/ https://linuxhostsupport.com/blog/install-scipy-through-pip/#respond Tue, 28 Feb 2017 13:31:24 +0000 https://linuxhostsupport.com/blog/?p=31 SciPy (pronounced “Sigh Pie”) is an open-source Python-based package of tools mainly used for scientific computing, mathematics and engineering. It is built on the Numpy extension of Python and it has a lot of useful modules for statistics, integration, optimization, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and many more. SciPy runs on […]

The post Install SciPy through pip appeared first on LinuxHostSupport.

]]>
SciPy (pronounced “Sigh Pie”) is an open-source Python-based package of tools mainly used for scientific computing, mathematics and engineering. It is built on the Numpy extension of Python and it has a lot of useful modules for statistics, integration, optimization, linear algebra, Fourier transforms, signal and image processing, ODE solvers, and many more. SciPy runs on all major Linux based operating systems and its installation is quick and very easy. In this tutorial we will guide you through the steps of installing SciPy on a Linux VPS using the pip package manager.

SciPy is very well organized into sub-packages covering different scientific computing domains. These are summarized in the following list:

  • cluster – Clustering algorithms
  • constants – Physical and mathematical constants
  • fftpack – Fast Fourier Transform routines
  • integrate – Integration and ordinary differential equation solvers
  • interpolate – Interpolation and smoothing splines
  • io – Input and Output
  • linalg – Linear algebra
  • ndimage  – N-dimensional image processing
  • odr – Orthogonal distance regression
  • optimize  – Optimization and root-finding routines
  • signal – Signal processing
  • sparse – Sparse matrices and associated routines
  • spatial – Spatial data structures and algorithms
  • special – Special functions
  • stats – Statistical distributions and functions
  • weave  – C/C++ integration

To start the SciPy installation you have to log in to your Linux VPS as user root

ssh root@IP_Address -p <port_number>

Start a new screen session by executing the following command

screen -S scipy

Make sure that all installed packages on your server and up to date and install the package manager pip on your server if it is not already installed

CentOS/Fedora/RHEL

yum clean all
yum -y update
yum install -y python-pip

Ubuntu/Debian

apt-get update && apt-get upgrade
apt-get install python-pip

Once it is installed, update pip to the latest available version

pip install --upgrade pip

To check the currently installed version and confirm that pip is successfully installed on your server, run the following command:

pip -V

The output should be in the following format:

pip 9.0.1 from /usr/lib/python2.6/site-packages (python 2.6)

After pip is installed on your virtual server, you can go ahead and install SciPy:

pip install scipy

If the installation is completed successfully, you will receive the following output

Collecting scipy
Downloading scipy-0.18.1-cp27-cp27mu-manylinux1_x86_64.whl (40.3MB)
100% |################################| 40.3MB 20kB/s
Installing collected packages: scipy
Successfully installed scipy-0.18.1

Since Scipy is built on the Numpy Python extension and uses Numpy arrays and data types, so for all basic array handling needs you may use Numpy functions, for example:

import numpy as np
np.some_function()

The top level Scipy package is imported as sp by default:

import scipy as sp

For more information about SciPy, its features, functions, and usage please check their official documentation at https://docs.scipy.org/doc/scipy/reference/


Of course, you don’t have to do any of this if you use one of our Software Installation Services, in which case you can simply ask our expert Linux admins to install SciPy through pip for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

The post Install SciPy through pip appeared first on LinuxHostSupport.

]]>
https://linuxhostsupport.com/blog/install-scipy-through-pip/feed/ 0