Python creates a virtual environment and uses a virtual environment in anaconda

python creates a virtual environment (Anaconda, windows)

You can use the cmd interface of the computer, or you can use Anaconda Prompt.
If you use Anaconda Jupter Notebook on the server, you can call up the terminal in the upper right corner (of course, you can also use the local jupter notebook)

The goal of this article is to make the notebook appear in a second python environment (python2.7 in the picture)
Local jupter screenshot

Conda common commands

Get the current conda version
conda --version

conda更新
conda update conda

View the current environment (* indicates the current environment)
conda env list
insert image description here
to create a new environment (if you do not specify the python version, the latest version is defaulted)
conda create --name your_env_name python=3.6

Switch environment (in preparation for installing modules)
conda activate your_env_name

Exit the current environment
conda deactivate

Delete the virtual environment
conda remove --name your_env_name --all
insert image description here

Install specific modules

#First you need to switch to the specified environment (otherwise it will be installed in the default environment)

Install the specified module
pip install package_name

View the currently installed modules
pip list
conda list -n your_env_name

Delete the specified module
conda remove --name env_name package_name
pip uninstall package_name

Upgrade module
pip install --upgrade module_name

Activate in jupterNotebook

If it is not activated in jupyterNotebook, even if other virtual environments are installed, jupter can only use the base environment to create ipynb files

In order to activate, ipykernel needs to be installed, otherwise the next step will prompt that there is no ipykernel module
pip install ipykernel

After this step is necessary, the new kernel will appear in the notebook.
210808 supplement, linux needs to add --user before --name (see https://blog.csdn.net/qq_33919727/article/details/107654495)
python - m ipykernel install --name env_name

may appear

An ipy is created under the new kernel, but import sys and then print(sys.version) you find that the python version has not changed. In this case, you need to:
view the current kernel list:
jupyter kernelspec list

Copy the new environment path and open it, there will be a kernel.json file
insert image description here
Open this file, change the path in argv to
the path of the virtual environment + python.exe, the picture shows
the path of python27 + python.exe
insert image description here

insert image description here

You can also change the display_name to your favorite. For example , open the notebook again
with python2.7 and find that the version is normal, and the new environment is installed successfully.

Example: install python2.7

conda create -n python27 python=2.7
conda activate python27
pip install ipykernel
python -m ipykernel install --name python27
(if necessary modify the kernel.json file)
restart the notebook, the new environment is created successfully

references

https://blog.csdn.net/hejp_123/article/details/92151293

https://blog.csdn.net/flying_ant2018/article/details/103055268

https://blog.csdn.net/flyer_tang/article/details/81305087

Likes are a meaningful thing, not only for the author, but also for the readers.
If I could go back in time, I would definitely like those articles that guided me

Guess you like

Origin blog.csdn.net/fei_YuHuo/article/details/117201239