[conda installation] Based on miniconda and Windows

.Summary

Conda is an open source package management system and environment management system that runs on Windows, macOS, Linux, and z/OS. Conda quickly installs, runs and updates software packages and their dependencies. Conda makes it easy to create, save, load and switch environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.

The above is copied from the official website. In human terms: If you are a programmer in Python, R, Ruby, Lua, Scala, Java, JavaScript, C/C++, Fortran and other languages, and you have had enough of various If you have messy dependencies and conflicts between libraries/packages, then conda is suitable for you.

In other words, conda allows humans to install multiple versions of interpreters and various libraries in one operating system of a computer at the same time. When a specific version needs to be used, it can be picked out separately without reinstalling or facing various problems. kind of conflict.

Although conda supports many languages, it is estimated that the most used ones are Python projects using conda (such as machine learning, data processing, etc.). Therefore, this article will explain how to install and use conda to run Python programs.

This is just a record of my installation, there may be things that are incorrect or not good enough, it is for reference only (and as a memo to myself).

  • This tutorial may be more suitable for non-computer majors and people who are very experienced in using computers for reference (because it is very detailed). At the same time, friends in computer-related majors can also refer to it and make some comments.
  • This tutorial installs miniconda, This is a smaller conda version than anaconda, lightweight, and only contains the basic functions of conda (excluding anaconda’s IDE and a lot of other messy things) Library……Anaconda is very cumbersome, and the built-in functions are a bit bloated. In most cases, you shouldn’t use so many, and I have vsc as my editor, so why do I need so many? This is very Easy to use. I once installed anaconda and was immediately confused by its many tools…
  • I use VSC, but if you use pycharm or something else, you can do it. It doesn’t matter. I feel that the plug-in function of vsc is enough for Python, at least it can do what pycharm can do. Basically capable, and vsc can also write other codes.
  • The operation may require administrator rights, please make sure you have computer rights.
  • Tutorial is based on Windows.
  • Your computer must at least be able to access the Internet relatively easily.

Finally, I will briefly introduce several conda operations (such as creating a new environment, activating the environment, etc.), and then simply install the cuda version of PyTorch.

If you have something different from what is written here, then some processes may be different. Because I only installed it once, I don’t know what the consequences will be if the difference is different… It’s best to just follow it.

start.

Don’t worry, I have something to say…

Computer technology is an industry that advances very quickly, so many of the various tutorials similar to this article will change slightly over time…

My suggestion is that if there is some place where the actual operation does not match the tutorial, then you can think about it a little bit, because it is usually just fine-tuning.

For example, in the following article about initializing the conda environment, it used to be in the installation directory, but I found that the new version seems to have changed to the installation directory/condabin/. Like this, you can handle it flexibly.

As for why there is this tip….

Because too many people really don’t have the ability to deal with problems flexibly…

1. Uninstall the remaining Python

This assumes you already have a version of Python on your computer. If you have never installed Python, skip ahead.

In theory, you can not uninstall it, but in order to avoid various conflicts and incompatibilities that you may encounter later, and to save valuable disk space, you should uninstall it. After all, Python will be included when installing conda.

1.1. Uninstall in Control Panel

As shown in the picture

Click on the name to sort alphabetically, find the Python you have installed in the red circle starting with “P”, right-click to uninstall it (I have already uninstalled it, so please pretend there is a Python there).

1.2. Clean the portal

Mainly to clean up environment variables and residual packages.

1.2.1. Delete remaining files (optional)

Focus on checking the following locations. Is there Python and some related text? If so, then confirm that it is Python and delete it (if something is a dependency of other programs, don’t delete it easily) , note that some of the paths here may be hidden files, you need to turn on the display of hidden files, or enter it directly in the path bar and press Enter. If not, please use search:

C:\Program Files
C:\Program Files (x86)
C:\
C:\Users\%Replace here with your username%\AppData\Local
C:\Users\%Replace here with your username%\AppData\Roaming

For example, this is related to Python

If your Windows is in Chinese, then the system may thoughtfully Chineseize users for you, as shown in the figure

Then there are several folders here, one is public, and the other is most likely your username. Just select your account and double-click it. If you don’t know your name……

1.2.2. Clean up environment variables

The previous Python was most likely added to the system path, and the relevant paths need to be deleted.

Things like this that are related to Python can be deleted. (Check whether the path contains Python, and then confirm that only Python is using this, don’t delete it by mistake)

Generally speaking, there are several places, one is ..../Python/, one is ..../Python/bin, and the other is circled above . There are also some related ones… Theoretically, as long as you delete the ones containing the Python keyword, it should be fine, unless you installed something here by the way.

Due to the special nature of conda, there is no need to set environment variables after installing conda. conda uses PowerShell add-ins to implement virtual environment loading.

At this point, the cleaning work is almost complete.

2. Conda installation

Download miniconda here. Miniconda is a free minimal installation program for conda. It is a small bootstrap version of Anaconda, containing only conda, Python, the packages they depend on, and a handful of other useful packages, including pip, zlib, and a few others. Use the conda install command to install 720+ additional conda packages from the Anaconda repository.

Of course, you can also download and install anaconda, which is almost the same.

2.1. Download

This is the official website document:

Miniconda – conda documentation?docs.conda.io/en/latest/miniconda.html

Of course you don’t know how to read the document, so here is Download (domestic mirror, fast):

Index of /anaconda/miniconda/ | Tsinghua University Open Source Software Mirror Station | Tsinghua Open Source Mirror?mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/

Just find the latest version, such as the one I used on December 12, 2022:

Windows and it is a 64-bit x86 processor, then just choose this.

2.2. Installation

Under the Windows graphical interface, just double-click and install. I believe everyone will have no problem.[1]

It is recommended to install it for all users.

As for the installation address, I default to the c drive because the location of the virtual environment created by conda can be set, and I like to put the program on the c drive (with a sense of ritual), so I might as well set one c:/miniconda/ is sufficient. Note that the path cannot have spaces, so it cannot be placed in a location such as c:/Program Files.

then here

If you choose to install for all users, then the first checkbox should be unselectable, and it will tell you that you need to simply manually set an environment variable later.

Check the second one (the version of Python you downloaded from miniconda will be installed).

After the installation is successful, you are done.

2.3. Initialize conda to global PowerShell

I am used to using PowerShell as a shell to operate. This thing is more similar to the operation logic of Linux, and it is much higher than cmd. And the default of vsc is also PowerShell.

First, go to the path where you installed miniconda and open the condabin folder under it. Hold down shift on the keyboard and right-click on the blank part of the folder to open a PowerShell

Then enter:

./conda init --all

conda will run something, register itself with PowerShell, and the environment will be automatically loaded next time you start PowerShell. Similar to this:

The environment will be automatically loaded

However, there may be two problems here:

  • In the end, an error may be reported, which when translated probably means “insufficient permissions”. At this time, you need to use administrator rights, just start an administrator PowerShell window and try again.

After starting, use the cd command to move there, for example cd c:/miniconda/condabin/. As long as you reach the destination, you can use the tab key to automatically complete it.

  • Then you will find that after conda init, every time you open PowerShell, it will be about 1~1.5 seconds slower. This is something you can’t help. The principle of conda init is to run a script and change the startup method of PowerShell so that it runs when it starts. The command to load the virtual environment. Although it will be slower, it doesn’t matter. You can still enter your commands during loading, but they will be displayed all after the loading is completed. So there is no need to worry about the computer not being able to keep up with your thinking speed. (Keyboard input to the stdin buffer). At the same time, this initialization of conda does not affect the normal use of PowerShell. And this is the only way for vsc to be compatible with conda[2]

2.4. Add environment variables

It’s better to add this to path:

Because this is the location of the package installed by pip in the base (default base) environment, sometimes some tools will be here, so that it is more convenient to use the package in the future (such as pyuic5 and the like). The method of adding path is the same as the one introduced above. Just go ahead and click the New button on the right.

20230512 update:

Note that when installing basic modules (such as yapf or autopep8 for formatting code) in the base environment, you should use administrator rights to execute pip install. Otherwise, pip does not have the permission to write information to the miniconda installation directory and will automatically redirect to It is under %username%/AppData/local/python/script (probably this directory, I don’t know if I remember it correctly). It is inconvenient to manage.

If you installed miniconda on another disk or location, you should not face permission issues.

If you use the default installation directory and maintain the good habit of using administrator rights when using pip under base (in fact, the base environment should not install new things every day), then add the following three environment variables. Refer to my Find the installation directory yourself.

The advantage of this is that if you use vsc, you can automatically find the conda environment and activate it easily.

Quickly switch working environments with one click

You see how convenient this is.

3. Use of conda

After the installation is complete, if nothing happens, you can start.

But before that, it’s best to do the following.

3.1. Switch source

As we all know, the international export bandwidth of mainland China is currently very low, so direct pip install or conda install will be very slow. Please make the following settings to use domestic mirrors to speed up downloads:

  • Edit C:\Users\%yourusername%\pip\pip.ini. If there is no new folder/file, just create one. Remember to edit the extension correctly .ini, I don’t think this is a problem.

Double-click to edit and add the following content

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host = pypi.tuna.tsinghua.edu.cn

This sets pip to source resources from Tsinghua University by default. The domestic Xar network is very fast. You can happily use pip from now on.

  • Then set up conda and edit the file C:\Users\\
    gc13\.condarc
    . If it doesn’t exist, create one.

Write the following setting text inside (you can right-click-open in other ways-notepad). Note that I have commented out most of the settings (so conda will use the default value, and the comment is # at the beginning of each line) symbol, if you look carefully you will find that the reserved area is the source change operation). After changing this file, the configuration here will be automatically used next time.

# add_anaconda_token: True
# add_pip_as_python_dependency: True
#aggressive_update_packages:
# - ca-certificates
#-certifi
# - openssl
# allow_conda_downgrades: False
# allow_cycles: True
# allow_non_channel_urls: False
# allow_softlinks: False
# always_copy: False
# always_softlink: False
# always_yes: None
# anaconda_upload: None
auto_activate_base: True
# auto_stack: 0
# auto_update_conda: True
#bld_path:
# changeps1: True
# channel_alias: https://conda.anaconda.org
# channel_priority: flexible # https://blog.csdn.net/egoyang123/article/details/106595117
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
  -defaults
# client_ssl_cert: None
# client_ssl_cert_key: None
# clobber: False
# conda_build: {}
# create_default_packages: []
# croot: C:\Users\\
gc13\conda-bld
#custom_channels:
# pkgs/main: https://repo.anaconda.com
# pkgs/r: https://repo.anaconda.com
# pkgs/msys2: https://repo.anaconda.com
# pkgs/pro: https://repo.anaconda.com
#custom_multichannels:
#defaults:
# - https://repo.anaconda.com/pkgs/main
# - https://repo.anaconda.com/pkgs/r
# - https://repo.anaconda.com/pkgs/msys2
#local:
# debug: False
#default_channels:
# - https://repo.anaconda.com/pkgs/main
# - https://repo.anaconda.com/pkgs/r
# - https://repo.anaconda.com/pkgs/msys2
default_python: 3.9
#default_threads: None
# deps_modifier: not_set
# dev: False
# disallowed_packages: []
# download_only: False
# dry_run: False
# enable_private_envs: False
env_prompt: ({default_env})
envs_dirs:
  - E:\conda_env\envs
  - C:\Users\\
gc13\.conda\envs
  - C:\Python\MiniConda\envs
  - C:\Users\\
gc13\AppData\Local\conda\conda\envs
# error_upload_url: https://conda.io/conda-post/unexpected-error
# execute_threads: 1
# experimental_solver: classic
# extra_safety_checks: False
# force: False
# force_32bit: False
# force_reinstall: False
# force_remove: False
#ignore_pinned: False
# json: False
# local_repodata_ttl: 1
# migrated_channel_aliases: []
# migrated_custom_channels: {}
# non_admin_enabled: True
# notify_outdated_conda: True
#offline: False
# override_channels_enabled: True
# path_conflict: clobber
# pinned_packages: []
# pip_interop_enabled: False
pkgs_dirs:
  - E:\conda_env\pkgs
  - C:\Python\MiniConda\pkgs
  - C:\Users\\
gc13\.conda\pkgs
  - C:\Users\\
gc13\AppData\Local\conda\conda\pkgs
# proxy_servers: {}
# quiet: False
remote_backoff_factor: 1
remote_connect_timeout_secs: 9.15
remote_max_retries: 3
remote_read_timeout_secs: 60.0
# repodata_fns:
# - current_repodata.json
# - repodata.json
# repodata_threads: None
# report_errors: None
# restore_free_channel: False
# rollback_enabled: True
# root_prefix: C:\Python\MiniConda
# safety_checks: warn
# sat_solver: pycosat
# separate_format_cache: False
# shortcuts: True
show_channel_urls: True
# signing_metadata_url_base: None
#solver_ignore_timestamps: False
#ssl_verify: True
# subdir: win-64
# subdirs:
#-win-64
#-noarch
# target_prefix_override:
# track_features: []
# unsatisfiable_hints: True
# unsatisfiable_hints_check_depth: 2
# update_modifier: update_specs
# use_index_cache: False
# use_local: False
# use_only_tar_bz2: False
# verbosity: 0
# verify_threads: 1
# whitelist_channels: []

But don’t copy mine directly! You need to change the assignment at least, probably the following locations (if some of your installations are not the default, you may need to change more):

  1. Change my username ngc13 to yours
  2. Change my installation path to yours
  3. It’s best to check other paths as well.
  4. The first item in pkgs_dirs: and envs_dirs: is what I set. You can follow suit and create some folders on other disks yourself, and then set them to the ones you created. The path of the folder, this operation will install the newly created conda environment to the location you set without wasting the c drive space. To be honest, conda will become ridiculously large after creating several more environments, so in the end It’s better to put the so-called “database” somewhere else.

3.2. Set the default location for creating new environments

Already mentioned above. I won’t say it here.

A brief introduction:

conda has a basic environment base environment, which is what you see after starting PowerShell or conda’s terminal. This is also the system environment. The packages you install using conda here will be installed in your conda directory. Download, and the package installed with pip will be installed in your user directory. The two locations are usually on the c drive.

The environment you create will be created by default in the folder you set (if not set, it will still be in the default location, which is the C drive). For example, I set some folders on the E drive. After activating the environment you created , the locations of packages installed by pip and conda (as well as the version of Python you require installed by conda) are all in the location you set. Different environments do not interfere with each other. This is the working principle of conda. The essence is to use shell commands, artificial A special environment variable scheme is built at runtime to call the relevant packages in your current environment and the specific version of the interpreter.

For example, I only installed PyQt5 and pyinstaller in the base environment, as well as a few packages, and then established an environment named py39 as a high-intensity load environment to install PyTorch and a lot of messy things. In this way, I The development environment will not use the C drive.

At the same time, you can also create a new environment as needed and configure different versions of Python.

3.3. conda basic commands

First of all, if you are using vsc, after you start it in the directory, it may prompt you to select the environment.

Just click and select

Very convenient. (provided you have already conda init)

Then, how to operate conda similar to pip? (Note that the following operations require conda init to be initialized. If you have not performed this operation, please turn forward. It is written before, in Chapter 2.3.)

First open a PowerShell, then[3]

  1. Create an environment demonstration (the parameter after –name is the name you gave him, you can name him casually, you can call him Xiaoshuai)
conda create --name py39 python=3.8 # Note that although I gave the name "py39", the actual version installed is Python 3.8

2. Activate the environment (activate by name)

conda activate py39

After activation, you will find that the environment has changed

3. The way to exit is to deactivate

conda deactivate py39

4. Delete the environment

The disk is large and does not need to be deleted. There is no system burden when it is not activated, so you can create many, many virtual environments.

conda remove --name py39 --all

5. List the environments in the current system

conda info -e

6. Conda to install the corresponding package

After switching to your target environment, type (here, take numpy as an example)

conda install numpy

7. List the packages installed in the current environment. This operation will also display the installation methods of the packages (for example, which ones are installed by conda and which ones are installed by pip)

conda list

8. Find package information in the current environment

conda search numpy

9. Update packages in the current environment

conda update numpy
conda update --all #Upgrade all
conda update conda #Upgrade conda itself

10. Delete the package in the current environment

conda remove numpy

11. Update Python in the current environment. Note that updating Python here refers to updating Python3.x.y to the latest version, without changing the major version.

conda update python

12. Using pip in the current environment

Just use it directly, for example

pip install matplotlib
pip list
pip...
...

However, it is recommended that pip be used only when conda cannot be used. Because conda’s package management and version control are stricter, dependency conflicts are less likely to occur. Using pip is fun for a while, but when conflicts arise later… .It’s not easy to deal with.

When pip checks the compatibility of a package, it only checks it during installation. If the package that has been installed conflicts with the package to be installed, an error will be reported. When conda installs multiple packages, it can automatically and intelligently select a compatible version solution, and then Take care of it. But note that conda cannot check the compatibility of the package installed by pip and the package it will install.

If pip really causes a problem, please be prepared to repair it manually or rebuild the environment directly.

4. Use conda to simply install a PyTorch

4.1. Go to the official website

PyTorch?pytorch.org/?Edit

4.2. Select version

and then get the command

4.3. Command line installation

Copy the command given below, then activate the target environment and run it in the environment you want to use.

conda will install it for you.

If conda doesn’t work, you can try pip installation.

Note that the command given here by PyTorch automatically installs cudnn. (But you still need to install the driver and cuda runtime library for the graphics card first, or you may have cudnn installed when installing cuda. Anyway, there is no need to install it specifically)

In addition, I suggest you stay up late and download from 4 to 7 a.m. as I did. At this time, the international link is relatively smooth and not stuck (there seems to be a problem with the image provided by Tsinghua Source).

Or use technology to access the Internet.

For more details, please refer to this tutorial of mine:

NGC13009: [PyTorch] Installation notes, based on Windows10/cuda11.641 Agree · 15 Comments? Edit

[end]

Reference

  1. ^The screenshot here is stolen, the original text is here Miniconda installation tutorial – Bilibili
  2. ^I didn’t find the second one
  3. ^Here is a reference to this article Conda User Guide – Zhihu