Use conda commands to create and manage a virtual environment and the Python packages inside it.
Download the Anaconda or Miniconda. Anaconda and miniconda come with Python and pip installed. Anaconda installs many packages and tools that are common for data science. Miniconda is far smaller.
conda create -n myenv pandas jupyterlab
Create a new conda environment named myenv
with the latest version of Python on the main conda channel and have pandas and jupyterlab installed into it.
conda create -n myenv python=3.9
Create a new conda environment named myenv
with the Python version specified.
conda activate myenv
Activate the myenv
virtual environment. You can tell you are in a virtual environment because your command line prompt will start with the name of your virtual environment in parentheses. Conda can be setup to activate an environment whenever you open your terminal. Wherever you go on your machine you will stay in your activated virtual environment.
conda deactivate
Deactivate the current environment.
conda list
List installed packages and versions in the active environment.
conda install pandas
Download and install the pandas package from the main conda channel.
conda install -c conda-forge pandas
Download and install the pandas package from the main conda-forge channel. This is useful when a package doesn't exist on the main channel or the version on the main channel isn't as new as you would like.
conda update pandas
Download and install the latest pandas package from the main conda channel.
pip install -U pandas
Install or update the pandas package from PyPI - the Python package manager. -U
specifies to update all dependent packages. PyPI often has packages that are not on conda or conda-forge. Conda and PyPI packages generally play nicely. Make sure that if you are installing a version of a package from PyPI you don't already have the same package installed from conda. If you do have it installed, then uninstall the conda version first.
conda uninstall pandas
Uninstall pandas.
conda env list
List conda environments.
conda env remove --name myenv
Remove the myenv
environment.
Alternative virtual environment: use venv and pip
Alternatively, you can create virtual environments and install packages with venv and pip.
You have venv automatically if you have a recent version of Python installed.
Use the following commands (assuming you have python3.8 installed):
python3.8 -m venv my_env
Create my_env with Python 3.8 installed.source my_env/bin/activate
Activate my_envpip install jupyterlab
Install jupyterlab into active environmentdeactivate
Deactivate current environmentIf you want to install a bunch of packages, you can list them in a file.
requirements.txt
is the common name. You can specify the versions if you want.pip install -r requirements.txt
Install the list of packages in requirements.txt.To avoid confusion, don't use venv inside an active conda environment. Deactivate the conda environment first.