Python environment setup tutorial using virtualenv
When developing Python applications, there is only one version of Python3 installed on the system: 3.4. All third-party packages will be pip installed into the site-packages directory of Python3.
If we want to develop multiple applications at the same time, then these applications will share one Python, which is Python 3 installed on the system. What if application A requires jinja 2.7, and application B requires jinja 2.6?
In this case, each application may need to have its own "independent" Python running environment. virtualenv is used to create an "isolated" Python running environment for an application.
First, we use pip to install virtualenv:
$ pip3 install virtualenv
Then, assuming that we want to develop a new project and need an independent Python running environment, we can do this:
The first step is to create a directory:
Mac:~ michael$ mkdir myproject Mac:~ michael$ cd myproject/ Mac:myproject michael$
The second step is to create an independent Python running environment and name it venv:
Mac:myproject michael$ virtualenv --no-site-packages venv Using base prefix '/usr/local/.../Python.framework/Versions/3.4' New python executable in venv/bin/python3.4 Also creating executable in venv/bin/python Installing setuptools, pip, wheel...done.
Commandvirtualenv can create an independent Python running environment. We also added the parameter --no-site-packages, so that it has been installed into the system Python environment. All third-party packages will not be copied over. In this way, we will get a "clean" Python running environment without any third-party packages.
The newly created Python environment is placed in the venv directory under the current directory. With the Python environment venv, you can use source to enter the environment:
Mac:myproject michael$ source venv/bin/activate (venv)Mac:myproject michael$
Notice that the command prompt has changed. There is a (venv) prefix, indicating that the current environment is a Python environment named venv.
Install various third-party packages normally and run the python command:
(venv)Mac:myproject michael$ pip install jinja2 ... Successfully installed jinja2-2.7.3 markupsafe-0.23 (venv)Mac:myproject michael$ python myapp.py ...
In the venv environment, packages installed with pip are installed in the venv environment, and the system Python environment is not affected in any way. In other words, the venv environment is created specifically for the myproject application.
Exit the current venv environment and use the deactivate command:
(venv)Mac:myproject michael$ deactivate Mac:myproject michael$
At this point, we have returned to the normal environment. Now pip or python are executed in the system Python environment.
It is completely possible to create an independent Python running environment for each application, so that the Python environment of each application can be isolated.
How does virtualenv create an "independent" Python running environment? The principle is very simple, that is, copy the system Python to the virtualenv environment, and use the command source venv/bin/activate to enter a virtualenv environment. The virtualenv will modify the relevant environment variables so that the commands python and pip both point to The current virtualenv environment.
summaryVirtualenv provides an isolated Python running environment for applications and solves the problem of multi-version conflicts between different applications.
The above is the detailed content of Python environment setup tutorial using virtualenv. For more information, please follow other related articles on the PHP Chinese website!

Linux'scommandlinecanbemoresecurethanWindowsifmanagedcorrectly,butrequiresmoreuserknowledge.1)Linux'sopen-sourcenatureallowsforquicksecurityupdates.2)Misconfigurationcanleadtovulnerabilities.Windows'commandlineismorecontrolledbutlesscustomizable,with

This guide explains how to automatically mount a USB drive on boot in Linux, saving you time and effort. Step 1: Identify Your USB Drive Use the lsblk command to list all block devices. Your USB drive will likely be labeled /dev/sdb1, /dev/sdc1, etc

Cross-platform applications have revolutionized software development, enabling seamless functionality across operating systems like Linux, Windows, and macOS. This eliminates the need to switch apps based on your device, offering consistent experien

Artificial Intelligence (AI) is rapidly transforming numerous sectors, from healthcare and finance to creative fields like art and music. Linux, with its open-source nature, adaptability, and performance capabilities, has emerged as a premier platfo

Looking for a fast, minimal, and efficient Linux distribution without a graphical user interface (GUI)? Lightweight, GUI-less Linux distros are perfect for older hardware or specialized tasks like servers and embedded systems. They consume fewer res

Wine 10.0 stable version release: Running Windows applications on Linux to a higher level Wine, this open source and free application, allows Linux users to run Windows software and games on Unix/Linux operating systems, ushering in the release of the 10.0 stable version! This version has been provided with source code and binary package downloads, and supports various distributions such as Linux, Windows and Mac. This edition embodies a year of hard work and over 8,600 improvements, bringing many exciting improvements. Key highlights include: Enhanced support for Bluetooth devices. Improve support for HID input devices. Optimized performance of 32-bit and 64-bit applications.

This tutorial guides you through installing SQL Server 2022 on RHEL 8.x or 9.x, connecting via the sqlcmd command-line tool, database creation, and basic querying. Prerequisites Before beginning, ensure: A supported RHEL version (RHEL 8 or 9). Sudo

Mozilla Thunderbird 135: Powerful cross-platform mail client Mozilla Thunderbird is a free, open source, cross-platform email, calendar, news, chat and contact management client designed to efficiently handle multiple email accounts and news sources. On February 5, 2025, Mozilla released the Thunderbird 135 version, introducing a number of new features, performance improvements and security fixes. Thunderbird 135 main features: XZ Packaging for Linux Binaries: Smaller files, faster unpacking, and better integration with modern distributions. Cookie storage support: when creating space


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor

Dreamweaver Mac version
Visual web development tools
