Python is fast becoming one of the most popular programming languages worldwide. Its low entry barrier for new programmers and simple, elegant syntax makes it a fantastic language to start learning. Python is excellent for task automation, and thankfully most Linux distributions come with Python installed right out of the box. This is true of Ubuntu 18.04; however, the Python package distributed with Ubuntu 18.04 is version 3.6.8. This article will cover how to install a newer version of Python, specifically, the latest stable version 3.8.3.Continue reading “How to Install Python on Ubuntu 18.04”
Pyenv is an outstanding tool for managing multiple Python installations. Pyenv-virtualenv is a pyenv plugin that facilitates the creation and management of Python virtual environments with pyenv. This is a compelling proposition, making it possible to manage multiple Python versions with pyenv and provide the means to control the Python environment in a more granular manner.
These instructions are executed as the root user on a Liquid Web Self-Managed Ubuntu 18.04 server. It is not required to be logged in as the root user to run these commands though some may need sudo permissions.
These instructions assume pyenv is already installed and that pyenv has been used to install some version of Python. Looking for how to install pyenv? Check out our article How to Install Pyenv on Ubuntu 18.04.
Step 1: Update and Clone the Repository
It’s always a good idea before getting started installing new packages or software to make sure the system is up to date. To accomplish this, run the following command.
root@ubuntu:~# apt update -y
Once that task has finished, we can go ahead and clone the pyenv-virtualenv repository. The following command will clone the repository into the pyenv plugins directory:
root@ubuntu:~# git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
Step 2: Configuration
This step is optional; however, it has been included because of the additional value it adds when using pyenv and pyenv-virtualenv. Running the following command will allow pyenv-virtualenv to automatically activate a virtual environment if it has been set up to do so.
root@ubuntu:~# echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
Now to enable pyenv-virtualenv, we need to restart the shell:
root@ubuntu:~# exec “$SHELL”
Step 3: Verify the Installation
To verify that pyenv-virtualenv is working as expected, we will create a project directory, and change directories it into it.
root@ubuntu:~# mkdir python_project root@ubuntu:~# cd python_project/ root@ubuntu:~/python_project#
Once inside the project directory, it’s time to create a virtual environment. The syntax to create a virtual environment with pyenv-virtualenv is as follows.
To verify what versions of Python can be installed, run the following command.
root@ubuntu:~/python_project# pyenv versions * system (set by /root/.pyenv/version) 3.8.3
Create a Virtual Environment
Now, to create a virtual environment we run this command.
root@ubuntu:~/python_project# pyenv virtualenv 3.8.3 python-project-3.8.3 Looking in links: /tmp/tmpuwmdca68 Requirement already satisfied: setuptools in /root/.pyenv/versions/3.8.3/envs/python-project-3.8.3/lib/python3.8/site-packages (41.2.0) Requirement already satisfied: pip in /root/.pyenv/versions/3.8.3/envs/python-project-3.8.3/lib/python3.8/site-packages (19.2.3)
That’s it! Now recheck the available versions and the newly created virtual environment should be in the list:
root@ubuntu:~/python_project# pyenv versions * system (set by /root/.pyenv/version) 3.8.3 3.8.3/envs/python-project-3.8.3 python-project-3.8.3
Activating and Deactivating the Virtual Environment
To make things convenient in terms of activating and deactivating this virtual environment, run the following command.
root@ubuntu:~/python_project# pyenv local python-project-3.8.3 (python-project-3.8.3) root@ubuntu:~/python_project#
You should notice that the command prompt has changed, displaying the currently activated virtual environment. The pyenv local command adds a .python-version file to the directory and lets pyenv know which python environment to use whenever the directory is entered. Changing out of this directory will automatically deactivate the virtual environment.
(python-project-3.8.3) root@ubuntu:~/python_project# cd root@ubuntu:~#
Similarly, changing back into the directory automatically activates the virtual environment.
root@ubuntu:~# cd python_project/ (python-project-3.8.3) root@ubuntu:~/python_project#
However, this virtual environment can be used outside the project directory by invoking the pyenv activate command like so.
(python-project-3.8.3) root@ubuntu:~/python_project# cd root@ubuntu:~# pyenv activate python-project-3.8.3 pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1’ to simulate the behavior. (python-project-3.8.3) root@ubuntu:~#
It can then be deactivated manually by running this command.
(python-project-3.8.3) root@ubuntu:~# pyenv deactivate root@ubuntu:~#
That’s all there is to it! Pyenv, in combination with pyenv-virtualenv, makes the management of Python versions and environments a breeze. Installing multiple versions of Python on an Ubuntu system is simple and straightforward.
Having access to a tool that centralizes both the management of the Python version and the discreet environments, each of those versions has is something that saves time and makes the development process that much easier.
We pride ourselves on being The Most Helpful Humans In Hosting™!
Our support staff is always available to assist with any issues related to this article, 24 hours a day, 7 days a week 365 days a year.
We are available, via our ticketing systems at email@example.com, by phone (at 800-580-4986) or via a LiveChat or whatever method you prefer. We work hard for you so you can relax.
What Is Virtualenv?
Virtualenv is a tool that creates an isolated environment separate from other projects. In this instance, we will be installing different Python versions, including their dependencies. Creating a virtual environment allows us to work on a Python project without affecting other projects that also use Python. It will utilize Python’s core files on the global environment to run, thus saving you disk space while providing the freedom to use different Python versions for separate apps or projects.Continue reading “Creating a Virtual Environment for Python on Ubuntu 16.04”
Reading Time: 2 minutesArguably one of the easiest tools to use for installing and managing Python packages, Pip has earned its notoriety by the number of applications utilizing this tool. Fancied for its capabilities in handling binary packages over the easy_installed packages manager, Pip enables 3rd party package installations. Though Python does sometimes come with Pip as a default, this tutorial will show how to install, check its version as well as some basic commands for using Pip on Ubuntu 16.04.
In this tutorial, we will consider how to enable both Python 2 and Python 3 for use on CentOS 8. In earlier distributions of CentOS, an unversioned Python command was available by default.
When the CentOS installation was complete, it was possible to drop into a Python shell by simply running the “python” command in a terminal.
Paradoxically, CentOS 8 does not have an unversioned Python command by default. This begs the question, why? RedHat states that this choice is by design “to avoid locking users into a specific version of Python.” Currently, RedHat 8 utilizes Python 3.6 implicitly by default, although Python 2.7 is additionally provided to maintain existing software.Continue reading “How to Install Python on CentOS 8”
In this article, we will be discussing what a python package is, what requirements are needed to install a software title, and the many other options available when setting up a new Python program.Continue reading “How to Install Python Packages”
The SQLAlchemy Toolkit and Object Relational Mapper is an extensive set of utilities for working with Python and databases. This toolkit provides a package full of popular persistence patterns, designed for economical and robust database accessibility. SQLAlchemy allows a developer to use simple SQL statements (unlike other Object Relational Mapping tools) which provide a helpful method to connect database tables with user-defined Python classes. The SQLAlchemy Object Relational Mapping tool is primarily centered on using the SQL Expression language.Continue reading “How To Install SQLAlchemy”
The CentOS 7 Linux distribution includes Python 2 by default. However, Python 2 is going to reach EOL on January 1, 2020. While some legacy applications might require access to Python 2 for various reasons, it’s vitally important to kick start new projects in Python 3.
In this tutorial, we are going to take a look at how to get up and running with Python 3 on a CentOS 7 server. Specifically, we will take a look at how to install Python 3 via the CentOS 7 package manager Yum as well as from source.Continue reading “How to Install Python 3 on CentOS 7”
A Virtual Environment or a “venv” is a Python module that creates a unique environment for each task or project. It installs the packages we need that are unique to that setting while keeping your projects neatly organized. Additionally, venv never actually modifies the system’s default Python versions or modules that are installed on the system. Using venv essentially allows for a unique working environment while avoiding any disruptions to other variants of Python that are used, but not related to our project.Continue reading “How To Setup A Python Virtual Environment On Windows 10”
In this article, we will discuss how to access and utilize the Python Package Index or PyPI via the pip command.
PyPI is the default software repository for Python developers to store created Python programming language software developers and programmers alike use to publicize and share their software.Continue reading “How to Use PyPI & PIP”