manage conda and python.
Install Miniconda
Linux
download the latest shell script
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
wget https://repo.anaconda.com/miniconda/Miniconda3-py310_23.1.0-1-Linux-x86_64.sh
Make the miniconda installation script executable
chmod +x Miniconda3-latest-Linux-x86_64.sh
Run miniconda installation script
bash ./Miniconda3-latest-Linux-x86_64.sh -p /opt/miniconda -b
-p
/opt/miniconda: This option specifies the installation path for Miniconda. In this case, it is set to “/opt/miniconda”, which means Miniconda will be installed in the “/opt/miniconda” directory.-b
This option stands for “batch mode” and instructs the installer to run in a non-interactive mode, skipping prompts and assuming default options for any questions asked during the installation process. Load conda environment when login
export PATH=$PATH:/opt/miniconda/bin
conda init
Windows
remember add ~/miniconda
and ~/miniconda\Scripts
to PATH.
MacOS
- remember download
.sh
installer when use m1/m2 chip, pkg installer may not work on m1/m2 mac. - after installed it, please add conda execute script to
~/.zprofile
$ vim ~/.zprofile
# conda
export PATH=/Users/frederick/miniconda3/bin:$PATH
$ conda init zsh
- apply it to zsh
/(your conda installation path)/bin/conda init zsh
Basic Usage for Miniconda
manage environment
|
|
modify channels
|
|
linux file localtion on /home/frederick/.condarc
windows file location on C:\Users\frederick\.condarc
tsinghua mirrors
|
|
USTC mirrors
channels:
- http://mirrors.ustc.edu.cn/anaconda/cloud/menpo/
- http://mirrors.ustc.edu.cn/anaconda/cloud/bioconda/
- http://mirrors.ustc.edu.cn/anaconda/cloud/msys2/
- http://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
- http://mirrors.ustc.edu.cn/anaconda/pkgs/free/
- http://mirrors.ustc.edu.cn/anaconda/pkgs/main/
auto_activate_base: false
show_channel_urls: true
after saved, use conda clean -i
remove index cache.
manage package
|
|
Create virtual environment (venv) using Python
Navigate to the directory where you want to create the virtual environment.Once you are in the desired directory, run the following command to create a new virtual environment:
python3 -m venv myenv
To activate the virtual environment, run the appropriate command based on your operating system:
source myenv/bin/activate
To exit the virtual environment when you’re done working with it, simply run the following command:
deactivate
install package for venv
Once the virtual environment is activated, you can install Python packages using pip without affecting your system-wide Python installation. For example:
pip install package_name
The packages you install will be isolated within the virtual environment and won’t affect other projects or the system.
Once you are in the correct directory, run the following command to install the packages from the requirements.txt file:
pip install -r requirements.txt
This command tells pip to install all the packages listed in the requirements.txt file.
Note that the requirements.txt file should be properly formatted, with each package listed on a separate line. For example:
# comment here
scikit-learn==1.1.3 # comment here
ipython>=8.0
change mirror for venv
Run the following command to open the pip.ini (Windows) or pip.conf (macOS and Linux) file for editing:
vim $VIRTUAL_ENV/pip.conf
here the example configuration
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
after saved, use pip config list -v
to validate the changed mirror.
Jupyter Notebook use venv or conda environment
Activate your virtual environment. If you haven’t activated it yet, refer to the previous answer on how to activate a virtual environment.
Install the ipykernel package within your virtual environment. This package allows Jupyter Notebook to recognize your virtual environment as a kernel.
pip install ipykernel
# or using conda
conda install ipykernel
Register the virtual environment as a kernel by running the following command:
python -m ipykernel install --user --name=myenv
Delete don’t want kernel.
# List the installed kernels
jupyter kernelspec list
# you can delete it using the following command:
jupyter kernelspec uninstall <kernel_name>
Launch Jupyter Notebook by running the command:
$ nohup jupyter notebook > ./jupyter.log &
# or modify config before run it
$ jupyter notebook --generate-config