Home Backend Development Python Tutorial Windows installs Virtualenv to create an independent Python environment

Windows installs Virtualenv to create an independent Python environment

Mar 02, 2017 pm 03:54 PM
python

Sometimes we need to call different versions of Python packages and modules in our programs, so the virtual environment of Virtualenv can help us isolate them. Next, let’s take a look at installing and using Virtualenv in Windows to create an independent Python environment. Method

0. When will virtualenv be used?
Assume that there are two applications in the system. Among them, application A requires version 1 of the library LibFoo, while application B requires version 2 of the same library LibFoo. The requirements of the two applications for the same library are as follows: There is a conflict, and the libraries in the system are installed to /usr/lib/python2.7/site-packages by default. So how can we use these two applications at the same time?
Your user rights are insufficient to install packages into the site-packages directory
You need to use an application and be independent from other applications in the system.
If you encounter the above problems, then virtualenv will help you. Virtualenv can create an environment with its own installation directory without interfering with the system's shared libraries or other virtualenv environments.

1. Install virtualenv
Before installing virtualenv, we need to install at least one version of Python; because virtualenv is a third-party module of python, it must be installed based on the python environment. ;
If your python environment has pip, then use the command directly:

pip install virtualenv
Copy after login

to install; otherwise you need to download the source code, and then use the command: python install setup.py to install
After installation, you need to create a new independent environment for virtualenv. For details, you can view its help command:

virtualenv -h
Copy after login

Windows installs Virtualenv to create an independent Python environment

Commonly used parameters are:
-p: Specify a version of the python environment; usually used when multiple python versions are installed in your system; by default virtualenv will give priority to its host python environment, That is, which version of Python it is installed under will be selected as the default Python isolation environment by default.
--no-site-packages: Do not use the python installation package of the system environment, that is, the installation package of the real python environment cannot be used in the isolation package; this option is the default in the current version.
--system-site-packages: Contrary to the above, it enables the isolated environment to access the python installation package of the system environment
--distribute: Copy a branch of the python environment. By default, setup, pip, wheel and other basics will be installed. Module

2. Activation
After the installation is complete, you can create an isolation environment. Use the command:

virtualenv envname ##创建一个新的隔离环境 
cd envname 
Scripts\activate  ##激活并切换到virtualenv环境
Copy after login

3. After using
to create and activate the virtualenv environment, we can use and install the python installation package and modules in the normal way. You can also directly switch to the directory of the python file that needs to be executed and use python xxx.py to ensure that the python file is executed in an isolated environment.
That is: before we need to use the virtualenv environment, we only need to activate it first and then use the same method to perform python operations and run python files

Windows installs Virtualenv to create an independent Python environment

cd my_project_folder
virtual ENV
source bin/activate
deactivate
Copy after login

The above is the basic usage process of virtualenv. First, virtual ENV will create a virtual environment named ENV in the current directory. At this time, three directories will be automatically generated in the current directory: bin, include and lib. The virtual environment created by default will depend on the site-packages in the system, that is, the libraries already installed in the system can still be used in the virtual environment. If you want to remove the dependency, you can:

virtualenv --no-site-packages ENV
Copy after login

. The virtual environment in the current directory can be started through source bin/activate. After startup, the shell prompt will change, indicating that it has entered the virtual environment. Use deactivate to leave the virtual environment.


4. Integrate into the development environment
Currently, PyCharm supports virtualenv environment integration. Other IDEs have not been studied yet, and integration is also very convenient:
(1) Create a new project
(2) Click the settings icon in the interpreter bar
(3) Select virtualenv
(4) You can create a new one or select an existing virtualenv environment

Windows installs Virtualenv to create an independent Python environment

After configuring, we can also test it in this project, select an installation module that is different from the system environment, and check whether its version is correct. I have 2 django here. version, so I used the django module to test and got the correct results.

Windows installs Virtualenv to create an independent Python environment


For more articles related to installing Virtualenv on Windows to create an independent Python environment, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles