preface

Hello, everyone! Today we are going to introduce you to the Python virtual environment setup method! Most people are attracted to python for the first time by its powerful package. Django is used for writing a website, Numpy is used for mathematical operations, and Pandas is used for data analysis.

However, the more packages are installed, the more compatibility problems will occur in the Python environment. For example, two packages depend on Scipy, but package A and package B have different versions of Scipy. As A result, package A cannot install package B.

To solve this annoying problem, Anconda is a free Python distribution that features Virtualenv, which allows you to manage multiple Python environments simultaneously through the Conda manager. Going back to the above problem, by using A virtual environment, we separate package A and package B, placing them in two different Python environments, and the problem is solved.

Today we will introduce anaconda based multi-environment setup method

START

Now that we’ve talked about what Anaconda does, I’m going to talk about how to download, install, configure, and use Anaconda properly.

First, let’s go to Anaconda’s website:

www.anaconda.com/distributio…

At the bottom of the download screen, you can see the download address of Anaconda. There are three options on it: Windows macOS and Linux. We can choose the one corresponding to our system. Here we install the latest version of Anaconda3, also known as Python 3.7 version, by clicking Download.

Here is the installation screen, the installation package under Windows. Click “*Next *”.

Click “* I Agree” *

This is saying, do you use it for yourself or can all the users under your computer use it, because we’re usually our own computers, so it’s ok.

It’s time to choose the installation path. If you have enough space on drive C, you can install it in the default path. But if your username is in Chinese, I suggest creating another directory without Chinese.

At this point it is critical to check both options. The second option must be checked, and the first option means that anconda will be added to the environment variable. If this option is checked, you can use the conda command directly from the CMD console, otherwise you must run the conda command from the Start menu (Anaconda (64-bit)).

Start the installation, wait patiently for the installation to finish. (IT took me five minutes to load my SSD.)

Anaconda and Pycharm are the best tools for configuring Anaconda in Pycharm. However, we will also explain how to configure Anaconda in Pycharm

Uncheck these two boxes, and we’ll click finish, and if we don’t, it’ll bring up some Anaconda introductions.

A quick note on how to install for macOS and Linux:

In macOS, the installation method is no different from ordinary software installation. Drag the installation method to install the software directly. Later, there will be options similar to configuring environment variables.

Bash./ anacond3_xxx. bash on Linux, the installation package we downloaded might be anacond3_xxx. bash (XXX represents the version number of the installation package). We keep pressing the enter key, the screen will keep scrolling to see the terms before installation, at the end of the text will pop up whether to accept the question, we select yes, in the following installation path or configuration environment variables, we choose yes or enter.

Now that we have Anaconda installed, let’s walk you through how to use and configure Anaconda. The most convenient part of it is the Python virtual environment, which we will create now.

Start by opening CMD (terminal for macOS and Linux)

We enter

conda info -e
Copy the code

List all the environments that are currently managed by Anaconda. You can see that there are 5 environments under my conda. If it is the first installation, there is usually only one environment, which is base

Let’s start by creating our first virtual environment:

Input order

Conda create -n myenv python=3.6Copy the code

(The specific version depends on your actual situation)

The orange box is the network location where we download relevant resources. Generally speaking, the information here is different from my picture. I have changed the mirror source of Conda, so the website here is Tsinghua Mirror source.

Changing the image source is a very important step, because the resource requested here is from a foreign server, the download speed is very slow. How do I change the mirror source? Check out this guide:

Mirror.tuna.tsinghua.edu.cn/help/anacon…

I’ll make a few brief remarks about this help guide. First of all, notice where the orange line is. We need to change the.condarc file in our user folder. If you are Windows, you need to execute that command to see the file. But it exists, just a hidden file, we in our user file, directly use the vim command to edit the file.

Replace the original content in.condarc with the gray area code. Let’s do it again

Conda create -n myenv python=3.6Copy the code

Select Yes, and you will find that the download speed is much faster after changing the image source.

It should be noted that we only change the source of Anaconda image here, and only use the domestic source when using the conda command to install the package. When using PIP to install the package, we also need to change the source of pypi at the following website:

mirrors.tuna.tsinghua.edu.cn/help/pypi/

In the final description, we can see that if we want to use the virtual environment, we need to use the command:

conda activate myenv
Copy the code

(Here **myenv ** is the name we just created when we created the virtual environment)

When we enter this command, at the head of the command number, we can see the name of our environment, which indicates that we are currently operating in this virtual environment, where we execute PIP or conda installation commands to install packages in this virtual environment.

Install a Numpy library to test it out:

conda install numpy
Copy the code

Try it out if you’ve already switched pYPI sources

pip install numpy
Copy the code

So far we have not combined Anaconda and Pycharm. Here is how to use our Conda virtual environment in Pycharm.

First open Pycharm, open a new empty folder in File, remember not to create a project, and select Settings

Choose Project Interpreter

Here you can see the use of virtual environment, is a private name is already created before I good, if in your interface, the environment should be empty, no matter how it doesn’t matter, because here we will add our conda virtual environment, in the blue path beside the code is a gear style icon, click on it.

In the option on the right, we select conda environment, and select the existing environment in the second option. Pycharm is very kind to automatically set the environment path for us. If it is not set automatically, it doesn’t matter. In general, the path to the virtual environment is in the envs file of Anaconda3 in your user folder. Go to the corresponding virtual environment file and find the Python file. As for the red line option, if checked, you can reference the Python environment in another project. Unchecked can only be seen in the current project, but it doesn’t matter, even if it is not checked, you can always add it back in another project using the same method.

Once that’s all done, hit Apply and you’ll create a New Python file in that folder. Typically, the first time you use an environment system you’ll need to Indexing all resource files. Once the Run flag turns gray, you’ll be happy to start typing.

END

Anaconda is a highly effective Python environment manager. Anaconda + Jupyter or Anaconda + Pycharm is becoming more and more of a mainstream trend. Using more efficient tools can create more happiness.

If this article is helpful to you, please share/like/favorites