Why is your Python version a mess? Because of the lack of this operation!

Source: Internet
Author: User
Tags echo command virtual environment virtualenv

Version of Deceptive

The Python version problem has been a widely-spit black dot. So that someone joked:

Python is the best two languages in the world!

Here I would like to explain that, for beginners, the syntax differences between versions are not really big. Now that you are beginning to learn Python, feel free to use the latest version. The industry is mainly criticized for the incompatibility of the code.

But in all fairness, compatibility issues between versions are not unique to Python. And even if Python has only one version, the version of the third-party library can be just as maddening.

For example, you previously wrote a Web site with Python on your computer, using the Django Framework, which is version 1.11. After a year, you have to develop a new website, when Django has developed to version 2.0, do you use or not? Updated version, the old website code on the computer can not be executed.

Or, you install and use the latest version of a module A. Then the code for the module b,b for another project is dependent on the earlier version of Module A. In this case, you may not even be able to successfully install module B.

These versions of the conflict are common in development, and the version differences between Python 2 and 3 exacerbate this problem.

There is already a proven solution to this:

Virtual Environments

The so-called virtual environment in Python development is the creation of an independent development environment for Python versions and third-party libraries, so that different projects do not interfere with each other. With virtual environments, we can build project A on the same computer to run in a Python 2-based environment, and project B can run in a Python 3-based environment.

The virtual environment needs to be created through some tools, several common:

1, Virtualenv. This is Python's most used virtual environment tool, simple and effective, I use it myself. The basic installation and use is described later in this article. There are also quite a few tutorials available on the web.

One limitation of virtualenv is that it is impossible to manage all the virtual environments on your computer uniformly, so after a lot of environments, you may miss or cause redundancy. For this, there is an extension tool virtualenvwrapper that can help you manage each environment in a unified setting.

2, Venv. Its implementation is largely based on virtualenv and is very similar to the way it is used. Python now has a built-in venv.

3, Conda. This tool is equivalent to pip + virtualenv + virtualenvwrapper, which not only creates and manages a virtual environment, but is also a package manager that can download and install third-party libraries. The function is perfect, the use is not complicated. The Conda is used as the environment and Module management tool by default in the well-known data analysis development Package Anaconda.

Next we briefly introduce:

Use of virtualenv

Installation

Python is easy to learn! Small series have a communication, mutual ask each other, resource sharing exchange Learning base, if you are also a Python learner or Daniel welcome you to!?:548+377+875! Learn together to progress together!

Install on the system command line via PIP or Easy_install:

Pip Install Virtualenv

If your command line is unable to execute Python, Pip, and subsequent virtualenv commands, check your path path in the environment variable. (You are not familiar with the public number of the Reply keyword installation)

Create

Go to the appropriate directory (typically the project root, but not in the Git repository), create an environment named My_env with the virtualenv command (this name can be customized):

Virtualenv my_env

Without an error, the directory my_env will be created successfully, with some directories and files, which is where our virtual environment resides.

Activated

Before using a virtual environment, you need to "activate" the Environment:

Windows

My_envscriptsactivate

Linux

SOURCE My_env/bin/activate

The path will vary depending on your location, or you can use the full path.

After the environment is activated, the command line is preceded by parentheses to identify the environment in which you are located. Installing a third-party module through PIP will not affect the Python environment of the system itself.

Shut down

When the environment is not needed, it can be closed by the Deactivate command. Of course, it's OK to just turn off the command-line window.

Specify Python

The creation of a virtual environment, by default, uses the Python version where Virtualenv is located. If you have more than one python on your computer, you can specify the Python version of your environment, for example:

Virtualenv-p C:python27python.exe ENV27

The nature of virtual environment

Why is it possible to separate the Python environment with a few simple lines of command? The path in the environment variable is inherently dependent.

The role of PATH is that when you execute a command, the system knows where to look for the command. There are multiple paths in the path, which are searched in order from front to back. The Python, Pip, and other commands are performed in this manner.

You can view the path by using the echo command:

Windows

Linux

When the version is chaotic, it is often the path with multiple paths, with the same commands in the different paths, so that the commands in that version that are not expected are executed. For example, your Python command is in the Python27 directory, and Pip executes the python36/scripts, and then there is a "clearly installed successfully, but the program can not find" situation.

When activating a virtual environment, it is equivalent to adding the directory we created at the beginning of PATH, using the Python and related libraries in this directory. Installing a new library is also installed under this new directory. To avoid interference with other versions.

If you don't know exactly where the command you're using is, you can use the Where/which command at the command line to view:

Virtual Environments in the Pycharm

Many students may not be accustomed to using the command line, but are fully configured with the IDE. So in the end, I would like to mention the pycharm situation.

The new version of Pycharm by default creates a new virtual environment when the project is created, so the classmates who do not understand this operation wonder why the module in the command line with PIP is not used in the pycharm. The simple solution is to install the library from the Pycharm Setup Panel.

Other options available:

    1. Do not select a new virtual environment when building a project
    2. When you build a project, select the virtual environment to inherit the existing library
    3. Create a good virtual environment using the command line when building a project
    4. After the project is built, install the library after activating the corresponding virtual environment at the command line

Why is your Python version a mess? Because of the lack of this operation!

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.