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.
- Pip installation is required this will install Python at the same time.
- Logged in as root or a user with admin privileges on an Ubuntu VPS LTS server. If logged in with a regular user with admin privileges be sure to use sudo before the commands discussed within this tutorial.
Step 1: Install Virtualenv
First, we will update our apt-get, then we will install the virtualenv module.
apt-get update apt-get install python-virtualenv
Virtualenv works by creating a folder that houses the necessary Python executables in the bin directory. In this instance, we are installing Python 3.5 while also creating two folders, the virtualenvironment, and project_1 directory.
virtualenv -p /usr/bin/python3 virtualenvironment/project_1
Virtualenv will create the necessary directories in the project_1 directory. In this directory you’ll find bin, include, lib, local and share.
Step 3: Activate Your Virtual Environment
Navigate to the project_1/bin directory and activate your new environment from within that folder by using the source command below. Anytime you need to work on your project you will need to enable with the following command.
cd virtualenvironment/project_1/bin source activate
Or if you are outside the bin directory, you can use the following command.
$ source virtualenvironment/project_1/bin/activate
You’ll see that you are now in this newly created environment by noting the change in the shell prompt, which reflects the name we created in Step 2.
When Python packages are installed, they will live in the lib directory, project_1/lib/python3.5/site-packages.
Exit your virtual environment by typing:
(project_1) root@host2:~# deactivate
Want To Know More?
Using Virtualenv with Python allows you to take advantage of multiple, isolated environments in which you can freely create, modify, or remove your applications. Both our Dedicated servers or VPS Cloud servers can easily utilize this technology to improve your productivity and provide a stable development platform from which you can launch your apps!
Give us a call at 800.580.4985, or open a chat or ticket with us to speak with one of our knowledgeable Solutions or Experienced Hosting advisors to learn how you can take advantage of these techniques today!