Simple usage of Virtualenv and pip in Python, virtualenvpip

Source: Internet
Author: User
Tags virtualenv

Simple usage of Virtualenv and pip in Python, virtualenvpip

This article introduces the simple usage of Virtualenv and pip in Python and shares it with you as follows:

0X00 installation environment

We need to use various libraries in Python development and learning, and the versions may be different in different projects and works, the existence of such problems gave birth to virtualenv. Virtualenv allows you to create a virtual environment on your computer and create a virtual environment for each project, so you don't have to worry about conflicts when different projects use libraries of different versions. The following content is only applicable to Linux/OSX and has not been tested in Windows.

To use this function, you still need to install it. To install virtualenv, you must install it directly using pip. You can easily install pip install virtualenv. After the installation, we can test a wave.

0X01 Initialize an empty Working Environment

Run virtualenv -- no-site-packages test_env in an empty environment to create a virtual environment named test_env in the current directory. Here the -- no-site-packages parameter indicates that no third-party libraries are carried from the global Python. For example, if you have installed the xxx library in global Python and do not use this parameter to create a virtual environment, the library will also be included in the virtual environment; but with this parameter added, in a virtual environment, it is a pure Python without these libraries.

root in ~ λ virtualenv --no-site-packages test_envNew python executable in /root/test_env/bin/pythonPlease make sure you remove any previous custom paths from your /root/.pydistutils.cfg file.Installing setuptools, pip, wheel...done.

Then you can usesource test_env/bin/activateYou can enter (activate) to this virtual environment. After entering the virtual environment, a bracket appears at the beginning of your command prompt, which contains the name of your virtual environment.

This is a virtual environment. In fact, everything is real. It means that you have activated this environment. In this environment, all the libraries installed with pip are stored in test_env.

You can also usedeactivateTo exit the environment.

0X02 batch export and installation Libraries

For example, we have developed a project that uses dozens of 20 libraries such as pymongo, requests, flask, and pymysql. We also need to specify a specific version, so it will be very troublesome to migrate A project from machine A to machine B. It is very troublesome to manually record each database and version and install them one by one. Therefore, pip has a perfect solution to this problem.

(Test_env) root in ~ Lambda pip freeze> requirements.txt # export installed Libraries

This command can export all the third-party libraries installed in the current environment and export them in a standard format. Therefore, the root directory of a standard python project usually has the namerequirements.txt.

Since it can be exported at one time, it must be installed at one time. In this way, all databases of the specified version exported above can be fully loaded at one time. With virtualenv, you can quickly deploy a Python project without messing up other Python project environments.

(test_env_1) root in ~ λ pip install -r requirements.txt

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.