[python] This article takes you to understand and solve the conda activate virtual environment, the package installed by pip is not placed in the virtual environment

too long do not read the version

There is a problem with the environment variable. If you check the environment variable, you should find that there are other addresses before the address of your virtual environment, such as /home/xxx/.local/bin:, etc., and there are programs such as pip and python in this address. .
The easiest way: delete both bin and lib in /home/xxx/.local.
Do not want to delete: modify environment variables

vim ~/.profile

commented out these lines
insert image description here

1. Know the environment variable (PATH)

To solve this problem, we must first understand the environment variable. The environment variable PATH is a list, which contains the addresses of some programs in order (usually xxx/bin). After adding the environment variable, the system can recognize the command and enter it in any directory. Execute the program corresponding to the command. The role of environment variables is to tell the system that these places can be found.

The order of the system search is: current directory - system variable list in order - environment variable list in order

2. View the current environment variables of the system

echo $PATH
  • Generally, after installing anaconda, after logging in, it is the base environment. After checking the PATH, you can find that the beginning of the PATH is /xxx/anaconda3/bin:/xxx/anaconda3/condabin
  • After you activate your virtual environment yourEnv, then check PATH,=, you can find that the beginning of PATH becomes /xxx/anaconda3/envs/yourEnv/bin::/xxx/anaconda3/condabin.
  • This is also the principle of conda switching environments, which is to change environment variables.
  • It is also simple to create a new environment in conda, that is, create a folder named after your custom environment name in envs, and then copy or download some basic packages and put them in it.

For example mine:

/work/Users/fanxin/anaconda3/bin:/work/Users/fanxin/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda/bin:/opt/pycharm-community-2020.2.3/bin:/snap/bin

Why this order? When logging in to the Linux system and starting a bash shell, by default bash will search for environment variable settings in several files, which can be collectively referred to as system environment files. The environment variable files checked when the login shell enters bash are as follows:

Account and password login shell -->/etc/profile (global file G1) -->/etc/profile.d/ (global script directory F1) --> ~/.profile (user file U1) --> ~/ .bashrc (user file U2) ——>/etc/bash.bashrc (global file G2)

Reference article: [linux] Linux environment variable initialization and the order in which the corresponding files take effect

3. Solve the problem that the package installed by pip is not placed in the virtual environment

3.1 Problem description and analysis

Sometimes after conda activates the virtual environment, the pip installation package finds that: Requirement already satisfied: it is in the folder, but it is not in the folder of the virtual environment.

pip show package_name #查看包的信息,包括安装位置。
pip -V # 查看当前pip的版本,地址

At this time, if you check the environment variables, you should find that there are other addresses before the address of your virtual environment, such as /home/xxx/.local/bin:, etc., for example:

/home/fanxin/.local/bin:/opt/gurobi1000/linux64/bin:/work/Users/fanxin/anaconda3/envs/subdivnet/bin:/work/Users/fanxin/anaconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr/local/cuda/bin:/opt/pycharm-community-2020.2.3/bin:/snap/bin

And this address happens to have programs such as pip and python. Then the priority of pip here will be higher than the priority of pip in your virtual environment, so when executing the pip installation package, the pip of the virtual environment is not started by default, which causes the package installed by pip to not be placed in the virtual environment.

  • Why are there other addresses in the front? This is related to the execution order of the environment files mentioned above. For example, the address /home/xxx/.local/bin is added when executing ~/.profile.

insert image description here

3.2 Solutions

1: The easiest way is to delete both bin and lib in /home/xxx/.local.

2: Modify the environment variable file

For example, if the address /home/xxx/.local/bin appears, then

vim ~/.profile

Comment out these lines.
insert image description here
For other abnormal addresses, you need to check in order according to the execution order of the environment files, and remove the problematic parts.

reference article

After anaconda creates a new virtual environment, pip is always located in the pip path of the global Python (the pip of the virtual environment cannot be located)

Guess you like

Origin blog.csdn.net/weixin_43693967/article/details/128351766