Use conda commands to create and manage a virtual environment and the Python packages inside it.
Follow the instructions here for your operating system: https://docs.conda.io/en/latest/miniconda.html.
You may be prompted with “Do you wish the installer to initialize Anaconda3 by running conda init?” We recommend “yes”. The default conda virtual environment is named base.
You will know you are in a virtual environment because your terminal prompt will have the name of the environment, for example, (base), in parentheses.
The default environment is the base
environment. If you are starting out, you can just use that environment. To isolate dependencies for different projects or to use different software versions, you will want to create and use multiple virtual environments.
NOTE: You do not want to have more than a single virtual environment active at the same time - otherwise it can be tricky to know which versions of packages are being installed and used at runtime.
conda deactivate
Deactivate the current environment.
conda create -n myenv python=3.12
Create a new conda environment named myenv
with the latest version of Python from the main conda package channel with Python 3.12 installed. The environment will have very few packages installed.
conda activate myenv
Activate the myenv
virtual environment. Unlike the venv/virtualenv virtual environment managers, wherever you navigate in your terminal you will stay in your activated virtual environment.
conda list
List installed packages and versions in the active environment.
pip install -U pandas
Install or update the pandas package from PyPI - the Python package manager. -U
specifies to update the specified package and all dependent packages. PyPI often has packages that are not on conda or conda-forge. Packages from conda and PyPI packages generally play together nicely.
pip uninstall pandas
Uninstall pandas.
conda env list
List conda environments.
conda env remove --name myenv
Remove the myenv
environment.
Alternative: virtual environment creation and management with venv
Alternatively, you can create virtual environments and install packages with venv and pip.
venv comes installed with Python.
If you need to install Python 3.12, download it from the python.org
Commands (assuming you have Python 3.12 installed):
python3.12 -m venv my_env
Createmy_env
with Python 3.12 installed.source my_env/bin/activate
Activatemy_env
pip install pandas
Install pandas 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.