Anaconda and Package Management#
Table of Contents#
Anaconda#
[ h ][ d ][ g ][ w ] Anaconda [ y ]
Docs
[ b ] Blog
[ b ]
12-07-2022
. “Conda Is Fast Now. Like, Really Fast”.[ b ]
05-06-2022
. “New Release: Anaconda Distribution Now Supporting M1”.[ b ]
04-30-2022
. “New from Anaconda: Python in the Browser”.[ b ]
03-16-2022
. “A Faster Solver for Conda: Libmamba”.[ b ]
11-19-2021
. “Anaconda Individual Edition 2021.11”.[ b ]
07-15-2021
. “A Python Data Scientist’s Guide to the Apple Silicon Transition”.[ b ]
08-13-2019
. “What’s in a name? Clarifying the Anaconda Metapackage”.[ b ]
06-25-2019
. “How We Made Conda Faster in 4.7”.[ b ]
03-11-2019
. “Understanding and Improving Conda’s Performance”.[ b ]
11-28-2018
. “Understanding Conda and Pip”.[ b ]
08-15-2017
. “Keeping Anaconda Up To Date”.
[ h ][ d ][ g ][ w ] Conda [ PyPA ]
[ d ] Conda-Build
[ d ] User Guide
recipes
[ d ] Anaconda Server
[ h ][ d ][ g ][ w ] Anaconda Cloud & Anaconda Notebooks
[ h ] Anaconda Learning
Conda performance
[ d ] Conda > User Guide > Concepts > Performance
[ b ]
12-07-2022
. “Conda Is Fast Now. Like, Really Fast”.[ b ]
03-16-2022
. “A Faster Solver for Conda: Libmamba”.[ b ]
06-25-2019
. “How We Made Conda Faster in 4.7”.[ b ]
03-11-2019
. “Understanding and Improving Conda’s performance”.
Conda and pip
[ d ] Conda > User Guide > Tasks > Managing environments > Using pip in an environment
[ d ] Conda > User Guide > Tasks > Managing packages > Installing non-conda packages
[ d ] Conda > User Guide > Configuration > Improving interoperability with pip
[ b ]
12-04-2018
. “Using Pip in a Conda Environment”.[ b ]
11-28-2018
. “Understanding Conda and Pip”.
some Anaconda repos
More#
[ h ][ d ][ g ][ PyPA ] Package Installer for Python (PIP)
[ d ] Secure Installs
Security
[ g ] Python Packaging Advisory Database
[ h ] Open Source Vulberabilities Database
Ars Technica - 10 malicious Python packages exposed in latest repository attack
PyPI Security Pitfalls and Steps Toward A Secure Python Ecosystem
RealPython
[ RealPython ]
03-15-2023
. Acsany, Philipp. “How to Evaluate the Quality of Python Packages”.[ RealPython ]
12-20-2021
. Acsany, Philipp. “Dependency Management with Python Poetry”.[ RealPython ]
----------
. Stratis, Kyle. “Python Application Layouts: A Reference”.
more
Getting Started#
Initialization#
In the terminal, run
conda init bash --dry-run
to see the changes that would take place for shell configuration. If you are satisfied, run
conda init bash
Close the current terminal instance and open a new one for the changes to take effect. If you need to undo shell configuration, run
conda init bash --reverse --dry-run
to see the changes that would take place to undo shell configuration. If you are satisfied, run
conda init bash --reverse
Close the current terminal instance and open a new one for the changes to take effect.
Check the version and update conda#
conda --version # or `conda -V`
View the help menu of command conda
.
conda # or `conda -h` or `conda --help`
View the help menu of subcommand subcommand
.
conda subcommand -h
Update conda.
conda update -n base -c defaults conda
Configuration#
conda config
conda config -a
Package and Environment Management#
List conda environments.
conda info -e # or `conda env list`
Create a new conda environment called data-science-env
with Python 3.11, Pandas 2.1, etc., and their dependencies using the conda-forge channel.
conda create -n data-science-env -c conda-forge --strict-channel-priority \
'ipykernel =6.29' \
'jupyterlab=4.00' \
'numpy =1.26' \
'pandas =2.01' \
'python =3.11'
Alternatively, create a conda environment associated with a new project directory.
mkdir data-science-project && conda create -p data-science-project \
-c conda-forge --strict-channel-priority \
'ipykernel =6.29' \
'jupyterlab=4.00' \
'numpy =1.26' \
'pandas =2.01' \
'python =3.11'
Activate conda environment data-science-env
.
conda activate data-science-env
Install package matplotlib
in conda environment data-science-env
.
conda install -n data-science-env matplotlib
Delete package pandas
from conda environment data-science-env
.
conda remove -n data-science-env pandas
Delete the conda environment called data-science-env
.
conda activate base && conda remove -n data-science-env --all
List the current conda environment’s packages and dependencies; their versions; and their channels.
conda list --show-channel-urls
Deploying a conda environment in Jupyter#
deploy an environment in Jupyter
install packages
jupyterlab
andnb_conda_kernels
into the environment that hosts Jupyterinstall package
ipykernel
into the environment to be deployed
conda create -n jupyter jupyterlab nb_conda_kernels # env `jupyter` supports the deployment of other envs
conda create -n myenv ipykernel # env `myenv` can be deployed as a notebook in jupyter
List available Jupyter kernels.
jupyter kernelspec list
Create a new Jupyter kernel called myJupyterKernel
.
python -m ipykernel install --user --name myJupyterKernel
Delete the Jupyter kernel called myJupyterKernel
.
jupyter kernelspec remove myJupyterKernel
Lab extensions.
jupyter labextension -h
Notebook extensions.
jupyter nbextension -h
Building a conda package and uploading it to a channel#
conda activate base && conda list # look for packages `conda-build` and `anaconda-client` and install them if necessary
conda config --set anaconda_upload no
git clone https://github.com/Anaconda-Platform/anaconda-client/ && cd anaconda-client/example-packages/conda
conda build .
cd $CONDA_PREFIX/conda-bld/osx-64
tar zxvf package
anaconda login
anaconda upload package # upload package `package` to personal channel
Texts#
[ h ] Beuzen, Tomas & Tiffany Timbers. Python Packages. CRC Press.
Terms#
Conda Environments#
Best Practices#
Do not alter the base (or root) Conda environment upon installation by installing new packages from multiple channels there.
Create as many Conda environments as necessary but keep Conda environments small and focused; don’t try to pack everything into a single Conda environment.
Avoid mixing channels within a Conda environment.
Use Pip within a Conda environment only when
conda
has been exhausted and thenceforth consider the Conda environment at risk of unexpected behavior or breakage.It is better to include all dependencies at environment creation instead of installing dependencies one by one after environment creation.
It is better to create a new Conda environment instead of adding dependencies to an existing Conda environment.
The following Conda environments were tested at a specific time and place and may not work out-of-the-box in your own environment. If conda create
complains that the specified package version cannot be found from a given channel then navigate to https://anaconda.org to see what is available in that channel. It might be the case that the specified package version is outdated and just needs to be replaced with a newer one. On the other hand, another channel may be better suited to the set of packages you would like to install.
Blockchain#
A Conda environment for blockchain learning and simulation.
1
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n blockchain -y -v \
-c conda-forge --strict-channel-priority \
'ipykernel =06.25' \
'jupyterlab =04.00' \
'numpy =01.25' \
'pandas =02.00' \
'pip =23.02' \
'python =03.11' \
'python-dotenv=01.00' \
'requests =02.31' \
'streamlit =01.24'
conda create -n blockchain -y -v -c conda-forge --strict-channel-priority ipykernel=6.25 jupyterlab=4 numpy=1.25 pandas=2 pip=23.2 python=3.11 python-dotenv=1 requests=2.31 streamlit=1.24
2
conda activate blockchain
3
pip install bip44 'web3[tester]'
pip install bip44 web3
4
import numpy as np
import pandas as pd
import bip44
import web3
import requests
import streamlit as st
import datetime
from importlib.metadata import version
import os
import platform as p
import sys
pad = 10
print( f"\n{'Executed' : <{pad}} : {datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z %Z')}"
f"\n{'Platform' : <{pad}} : {p.platform(aliased = False, terse = False)}"
f"\n{'Conda' : <{pad}} : {os.environ['CONDA_DEFAULT_ENV'] or sys.executable.split('/')[-3]}"
f"\n{'Python' : <{pad}} : {p.python_implementation()} {p.python_version()} {sys.executable}")
print(*[f'{name : <{pad}} : {version(name)}'
for name in ['Bip44', 'NumPy', 'Pandas', 'Requests', 'Streamlit', 'Web3']], sep = '\n')
Executed : 2023-07-28 13:00:14 -0400 EDT
Platform : macOS-13.5-arm64-arm-64bit
Conda : blockchain
Python : CPython 3.11.4 /Users/davefriedman/anaconda3/envs/blockchain/bin/python
Bip44 : 0.1.3
NumPy : 1.25.1
Pandas : 2.0.3
Requests : 2.31.0
Streamlit : 1.24.1
Web3 : 6.7.0
CLI#
A Conda environment for building CLIs.
1
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n fq -y -v \
-c conda-forge --strict-channel-priority \
'fire =00.04' \
'ipykernel =06.16' \
'jupyterlab =03.05' \
'python =03.09' \
'questionary=01.10'
conda create -n fq -y -v -c conda-forge --strict-channel-priority fire=0.4 ipykernel=6.16 jupyterlab=3.5 python=3.9 questionary=1.10
2
conda activate fq
OpenAI’s ChatGPT#
1
conda create -n chatgpt -y -v \
-c conda-forge --strict-channel-priority \
'beautifulsoup4=04.12' \
'flask =02.03' \
'ipykernel =06.25' \
'numpy =01.25' \
'openai =00.27' \
'psycopg2 =02.09' \
'python =03.11' \
'requests =02.31'
2
conda activate chatgpt
3
macOS
python -m pip install -U 'spacy[apple]'
python -m spacy download en_core_web_trf
4
import numpy as np
import flask
import psycopg2
import requests
import openai
import datetime
from importlib.metadata import version
import os
import platform as p
import sys
pad = 10
print( f"\n{'Executed' : <{pad}} : {datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z %Z')}"
f"\n{'Platform' : <{pad}} : {p.platform(aliased = False, terse = False)}"
f"\n{'Conda' : <{pad}} : {os.environ['CONDA_DEFAULT_ENV'] or sys.executable.split('/')[-3]}"
f"\n{'Python' : <{pad}} : {p.python_implementation()} {p.python_version()} {sys.executable}")
print(*[f'{name : <{pad}} : {version(name)}'
for name in ['Flask', 'NumPy', 'OpenAI', 'Psycopg2', 'Requests']], sep = '\n')
Executed : 2023-07-28 13:26:50 -0400 EDT
Platform : macOS-13.5-arm64-arm-64bit
Conda : chatgpt
Python : CPython 3.11.4 /Users/davefriedman/anaconda3/envs/chatgpt/bin/python
Flask : 2.3.2
NumPy : 1.25.1
OpenAI : 0.27.8
Psycopg2 : 2.9.6
Requests : 2.31.0
Facebook’s Prophet#
1
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n fb -y \
-c conda-forge --strict-channel-priority \
'ipykernel =6.20' \
'jupyterlab =3.06' \
'hvplot =0.08' \
'numpy =1.24' \
'pandas =1.05' \
'prophet =1.01' \
'python =3.10' \
'scikit-learn=1.02'
2
conda activate fb
GeoViews#
A GeoViews-compatible Conda environment.
1
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n gv -y \
-c conda-forge --strict-channel-priority \
'geoviews =01.09' \
'hvplot =00.08' \
'ipykernel =06.20' \
'jupyterlab=03.05' \
'matplotlib=03.06' \
'numpy =01.23' \
'pandas =01.05' \
'pip =23.00' \
'python =03.09' \
'requests =02.28'
2
conda activate gv
3
pip install citipy
Google Maps with jupyter-gmaps
#
A Conda environment for displaying Google maps within a Jupyter notebook.
Important
Python package jupyter-gmaps
has not been maintained for several years; has a number of open issues; and thus may not work properly. See the jupyter-gmaps page on installing jupyter-gmaps
for the official installation instructions upon which the following steps are based.
1
Check whether other Jupyter servers are running.
jupyter notebook list
If there are other Jupyter servers running shut them down via port number before proceeding. (The following command shuts down the Jupyter server running on port 8888, for example.)
jupyter notebook stop 8888
2
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n gmap -y -v \
-c conda-forge --strict-channel-priority \
'gmaps =00.09' \
'hvplot =00.08' \
'ipykernel =06.16' \
'ipywidgets=07.07' \
'jupyterlab=03.00' \
'nodejs =18.12' \
'numpy =01.21' \
'pandas =01.03' \
'pip =22.03' \
'python =03.07' \
'requests =02.28' \
'scipy =01.07'
conda create -n gmap -y -v -c conda-forge --strict-channel-priority gmaps=0.9 hvplot=0.8 ipykernel=6.16 ipywidgets=7.7 jupyterlab=3.0 nodejs=18.12 numpy=1.21 pandas=1.3 pip=22.3 python=3.7 requests=2.28 scipy=1.7
3
conda activate gmap
4
pip install citipy
5
Check whether the appropriate Jupyter notebook extensions are enabled.
jupyter nbextension list
If necessary, enable them.
jupyter nbextension enable --py --sys-prefix widgetsnbextension
jupyter nbextension enable --py --sys-prefix gmaps
6
Once you’ve verified that no other Jupyter servers are running (see step 1), startup Jupyter Notebook.
jupyter notebook
7
There is no dedicated Jupyter kernel to go along with Conda environment gmap
. Make sure to select Jupyter kernel Python 3 (ipykernel)
.
Additionally, it may or may not help to
open Jupyter Notebook in the Chrome web browser and clear Chrome’s cache and cookies.
refresh the browser page.
select “Kernel > Restart and Clear Output” from the Jupyter notebook’s menu bar.
ML (also Jupyter Book, NLP, Postgres, Spark, TensorFlow for Apple Silicon)#
A machine learning Conda environment.
1
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n ml -y \
-c conda-forge --strict-channel-priority \
'boto3 =0001.28' \
'cython =0003.00' \
'dask =2023.07' \
'fastparquet =2023.07' \
'findspark =0002.00' \
'hvplot =0000.08' \
'imbalanced-learn=0000.11' \
'ipykernel =0006.25' \
'ipympl =0000.09' \
'ipywidgets =0008.00' \
'jupyterlab =0004.00' \
'nltk =0003.08' \
'numpy =0001.25' \
'matplotlib =0003.07' \
'mrjob =0000.07' \
'sphinx =0005.00' \
'plotly =0005.15' \
'pandas =0002.00' \
'pillow =0010.00' \
'pip =0023.01' \
'plotly =0005.15' \
'polars =0000.18' \
'prettytable =0003.08' \
'psycopg2 =0002.09' \
'pyarrow =0012.00' \
'pymongo =0004.03' \
'pyqt =0005.15' \
'pyspark =0003.04' \
'python =0003.11' \
'pytorch =0002.00' \
'sympy =0001.12' \
'scikit-learn =0001.03' \
'scipy =0001.11' \
'spacy =0003.06' \
'statsmodels =0000.14' \
'streamlit =0001.26' \
'tabulate =0000.09' \
'wordcloud =0001.09'
2
conda activate ml
3
macOS
python -m pip install jupyter-book==0.15.1
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal
4
python -m ipykernel install --user --name ml
5
import numpy as np
import pandas as pd
import sklearn
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.preprocessing import (StandardScaler,
MinMaxScaler)
from sklearn.metrics import (balanced_accuracy_score,
confusion_matrix)
import imblearn
from imblearn.metrics import classification_report_imbalanced
import plotly
import plotly.express as px
from collections import Counter
from pathlib import Path
import warnings
warnings.filterwarnings('ignore')
import datetime
from importlib.metadata import version
import os
import platform as p
import sys
pad = 20
print( f"\n{'Executed' : <{pad}} : {datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z %Z')}"
f"\n{'Platform' : <{pad}} : {p.platform(aliased = False, terse = False)}"
f"\n{'Conda' : <{pad}} : {os.environ['CONDA_DEFAULT_ENV'] or sys.executable.split('/')[-3]}"
f"\n{'Python' : <{pad}} : {p.python_implementation()} {p.python_version()} {sys.executable}")
print(*[f'{name : <{pad}} : {version(name)}'
for name in ['Imbalanced-Learn', 'NumPy', 'Pandas', 'Plotly', 'Scikit-Learn']], sep = '\n')
Executed : 2023-07-28 13:54:16 -0400 EDT
Platform : macOS-13.5-arm64-arm-64bit
Conda : ml
Python : CPython 3.11.4 /Users/davefriedman/anaconda3/envs/ml/bin/python
Imbalanced-Learn : 0.11.0
NumPy : 1.25.1
Pandas : 2.0.3
Plotly : 5.15.0
Scikit-Learn : 1.3.0
PySpark and Postgres#
Revised
09 Jul 2023
Careful
compatible Java version: 8, 11, or 17!
Spark >= 3.4 requires Java 8 >= 8u362 and Python >= 3.8!
From the page Spark Overview:
“Spark runs on both Windows and UNIX-like systems (e.g. Linux, Mac OS), and it should run on any platform that runs a supported version of Java. This should include JVMs on x86_64 and ARM64. It’s easy to run locally on one machine — all you need is to have java installed on your system PATH, or the JAVA_HOME environment variable pointing to a Java installation.
Spark runs on Java 8/11/17, Scala 2.12/2.13, Python 3.7+, and R 3.5+. Python 3.7 support is deprecated as of Spark 3.4.0. Java 8 prior to version 8u362 support is deprecated as of Spark 3.4.0. When using the Scala API, it is necessary for applications to use the same version of Scala that Spark was compiled for. For example, when using Scala 2.13, use Spark compiled for 2.13, and compile code/applications for Scala 2.13 as well.
For Java 11, setting -Dio.netty.tryReflectionSetAccessible=true is required for the Apache Arrow library. This prevents the java.lang.UnsupportedOperationException: sun.misc.Unsafe or java.nio.DirectByteBuffer.(long, int) not available error when Apache Arrow uses Netty internally.”
conda create -n bigdata -y -v \
-c conda-forge --strict-channel-priority \
'fastparquet=2023.07' \
'findspark =0002.00' \
'ipykernel =0006.24' \
'numpy =0001.25' \
'pandas =0002.00' \
'psycopg2 =0002.09' \
'pyarrow =0012.00' \
'pyspark =0003.04' \
'python =0003.11'
conda activate bigdata
import findspark
import pyspark
from pyspark.sql import SparkSession
import datetime
import platform as p
import sys
pad = 20
print(f"\n{'Executed'.upper() :<{pad}} : {datetime.datetime.now()}"
f"\n{'Platform' :<{pad}} : {p.system()} {p.release()} | {p.mac_ver()[0]} | {p.machine()}"
f"\n{'Python' :<{pad}} : {p.python_implementation()} {sys.version}"
f"\n{'Findspark' :<{pad}} : {findspark.__version__}"
f"\n{'PySpark' :<{pad}} : {pyspark .__version__}")
EXECUTED : 2023-07-09 14:41:09.298787
Platform : Darwin 22.4.0 | 13.3.1 | arm64
Python : CPython 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:41) [Clang 15.0.7 ]
Findspark : 2.0.1
PySpark : 3.4.1
Web Scraping#
A Conda environment bundling some web scraping tools.
Revised
27 Jun 2023
[ Doc ] Chrome driver
1 - Check the Conda/virtual environment for the existence of the appropriate packages and note their versioning. An example environment:
Compatible shells:
Bash
Git Bash
Zsh
other Unix
conda create -n webscraping -y -v \
-c conda-forge --strict-channel-priority \
'beautifulsoup4 =04.12' \
'flask =02.03' \
'flask-pymongo =02.03' \
'ipykernel =06.23' \
'jupyterlab =04.00' \
'lxml =04.09' \
'matplotlib =03.07' \
'numpy =01.25' \
'pandas =02.00' \
'pip =23.01' \
'psycopg2 =02.09' \
'pymongo =04.03' \
'python =03.10' \
'selenium =04.10' \
'sqlalchemy =02.00' \
'sqlite =03.42' \
'webdriver-manager=03.08'
2
conda activate webscraping
3
pip install splinter
pip install selenium
Using pip instead of conda to install Selenium?
At the time of last writing, splinter’s setup.py
shows that the optional dependency syntax
pip install splinter[selenium4]
does not install the latest version of Selenium.
4
conda list --show-channel-urls
5 - (optional) Jupyter kernel
python -m ipykernel install --user --name webscraping
jupyter kernelspec list
6 - Check the programming environment for the existence of the appropriate packages and note their versioning. Compare this with what was noted previously and reconcile inconsistencies.
import selenium
import splinter
from splinter import Browser
# webdriver-manager is being phased out for Selenium >=4.6 (see docs)
import webdriver_manager
import datetime
from importlib.metadata import version
import os
import platform as p
import sys
pad = 20
print( f"\n{'Executed' : <{pad}} : {datetime.datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z %Z')}"
f"\n{'Platform' : <{pad}} : {p.platform(aliased = False, terse = False)}"
f"\n{'Conda' : <{pad}} : {os.environ['CONDA_DEFAULT_ENV'] or sys.executable.split('/')[-3]}"
f"\n{'Python' : <{pad}} : {p.python_implementation()} {p.python_version()} {sys.executable}")
print(*[f'{name : <{pad}} : {version(name)}'
for name in ['Selenium', 'Splinter', 'webdriver-manager']], sep = '\n')
Executed : 2023-07-28 13:31:29 -0400 EDT
Platform : macOS-13.5-arm64-arm-64bit
Conda : webscraping
Python : CPython 3.10.12 /Users/davefriedman/anaconda3/envs/webscraping/bin/python
Selenium : 4.10.0
Splinter : 0.19.0
webdriver-manager : 3.8.6
7 - According to versioning, consult the associated documentation for the appropriate setup code.
[ Splinter 0.19 ][ Selenium >= 4.6 ]
browser = Browser(driver_name = 'chrome',
headless = False,
retry_count = 3)
[ Splinter 0.19 ][ Selenium < 4.6 ] (requires webdriver-manager)
executable_path = {'executable_path' : webdriver_manager.chrome.ChromeDriverManager().install()}
browser = Browser(driver_name = 'chrome',
headless = False,
retry_count = 3,
service = selenium.webdriver.chrome.service.Service(**executable_path))
[ Splinter 0.18 ][ Selenium < 4.6 ] (requires webdriver-manager)
executable_path = {'executable_path' : webdriver_manager.chrome.ChromeDriverManager().install()}
browser = Browser(driver_name = 'chrome',
headless = False,
retry_count = 3,
**executable_path)
Command Reference#
anaconda
#
conda
#
conda --version
conda --help
conda activate
#
conda activate --help
conda activate <ENVIRONMENT_NAME> # activate env
conda activate <PATH> #
conda config
#
conda config --help
conda config --show
conda config --show-sources
conda config --add channels conda-forge
conda config --describe
conda create
#
conda create -n <ENVIRONMENT_NAME> PACKAGE(S) # create env
conda create --name <ENVIRONMENT_NAME> PACKAGE(S) #
conda create -p <PATH> PACKAGE(S) # create env at a specific location
conda create --prefix <PATH> PACKAGE(S) #
conda env
#
conda env --help
# create an environment from a file
conda env create --help
conda env create -f <ENVIRONMENT_FILE>
conda env create --file <ENVIRONMENT_FILE>
conda env list --help
conda env list
# export an environment to a file
conda env export --help
conda env export # view the env
conda env export > env.yml # save the env to file `env.yml`
conda env export --from-history > env.yml # save the env to file `env.yml`
conda info
#
conda info --help
# examples
conda info
conda info -a
conda info -e # --envs
conda init
#
conda init --help
# template
conda init [ --reverse ] [ --dry-run ]
# examples
conda init
conda init --all
conda init bash
conda init zsh
conda init powershell
conda init cmd.exe
conda install
#
conda install --help
# templates
conda install PACKAGE(S) # install packages into the active env
conda install -n <ENVIRONMENT_NAME> PACKAGE(S) # isntall packages into env `<ENVIRONMENT_NAME>`
conda install PACKAGE(S) -c <CHANNEL_NAME>
# example
conda install -n pyfinance -c conda-forge pyfinance==1.2.7=py_0
conda list
#
conda list --help
conda list # list packages in the active env
conda list -n <ENVIRONMENT_NAME> # list packages in env `<ENVIRONMENT_NAME>`
conda remove
#
conda remove --help
# templates
conda remove PACKAGE(S) # remove packages `PACKAGE(S)` from the active env
conda remove -n <ENVIRONMENT_NAME> PACKAGE(S) # remove packages `PACKAGE(S)` from env `<ENVIRONMENT_NAME>`
conda remove -n <ENVIRONMENT_NAME> --all # remove env `<ENVIRONMENT_NAME>`
conda repo
#
conda repo --help
conda search
#
conda search --help
# examples
conda search matplotlib
conda search 'matplotlib>=3.5.0'
conda search pyfinance --channel conda-forge
conda search pyfinance -c conda-forge
conda skeleton
#
# conda skeleton
#
# Python (PyPI)
# R (CRAN)
# Perl (cpan)
# Lua (luarocks)
# RPM (rpm)
conda skeleton --help
conda skeleton pypi -h
conda skeleton pypi click # download package `click` from PyPI and genrerate a recipe
conda skeleton cran fansi # download package `fansi` from CRAN and genrerate a recipe
conda update
#
conda update --help
conda update conda
Notes#
Programs are distributed as packages.
Package Manager
automatically search, download, compile, build, install a program from source, and update
identifies compatible versions of the program and its dependencies
handles updates
examples of platform- or ecosystem-specific package managers
Advanced Package Tool (APT) (Debian)
Homebrew (macOS)
pip (Pip Installs Packages, Package Installer for Python)
Environment Manager
easily create, save, load, and switch between environments
Conda
a platform-agnostic open-source package and environment management system
using a package and environment management tool facilitates portability and reproducibility of (data) science workflows
~/anaconda3/ # Anaconda root
├── bin/ # binary executables, human-readable scripts, compiled programs
│ └── conda # command `conda`
├── conda-meta/
├── condabin/
├── envs/ # named environments
│ └── snowflake/ # env `snowflake`
│ ├── bin/
│ │ └── python # python interpreter
│ ├── conda-meta/
│ ├── include/
│ ├── lib/
│ ├── man/
│ ├── share/
│ └── ssl/
├── etc/
│ └── conda/
│ ├── activate.d/ # activation script (everything in this directory is executed)
│ └── deactivate.d/ # deactivation script
├── include/ # header files
├── lib/ # uncompressed libraries and modules (callables)
├── libexec/
├── man/ # manual pages
├── pkgs/ # package cache of compressed tarballs (which populates directories `bin`, `lib`)
├── share/
├── shell/
└── ssl/
Conda Environment
a self-contained directory that contains a set of packages of programs, libraries, scripts, etc.
optional name points to prefix (the absolute path)
conda environment activation modifies
environment variable
CONDA_PREFIX
environment variable
PATH
# Conda Package - a compressed file with a particular directory structure (`bin`, `info`, `lib`)
package_name-package_version-build
├── bin/
├── info/
│ ├── info/about.json
│ ├── info/files
│ ├── info/git
│ ├── info/hash_input.json
│ ├── info/index.json
│ ├── info/licenses/
│ ├── info/paths.json
│ ├── info/recipe/
│ └── info/test/
└── lib/
Conda Channel
a URL pointing to a directory containing a collection of packages
local channels can be specified with
file://
or a pathcommunity-led public channel
maintain feedstocks: repositories that contain recipes that are built into packages by automated infrastructure
.condarc
an optional runtime config file in the YAML format
created in the home directory the first time command
conda config
is runpossible config files and folders
~/.anaconda
~/.conda
~/.condarc
~/.continuum
~/.condarc # config file
channels :
- conda-forge
envs_dirs :
- ~/anaconda3/envs
- /path/to/alternate/envs
pkgs_dirs :
- ~/anaconda3/pkgs
- /path/to/alternate/pkgs
Environment File
a plain text file in the YAML format that describes the content of an environment
name : machine-learning-env
channels :
- conda-forge
dependencies :
- ipython # interactive terminal
- pandas==1.5.2=py39hecff1ad_0 # data processing
- python
- scikit-learn # machine learning algorithms
- seaborn # visualization
variables :
VAR1 : valueA
VAR2 : valueB
prefix : /Users/user/anaconda3/envs/machine-learning-env
name : data-viz
channels :
- javascript
dependencies :
- python=3.9
- bokeh=2.4.2
- numpy=1.21.*
- nodesjs=16.13.*
- flask
- pip
- pip:
- Flask-Testing
# Conda Recipe - a directory that contains instructions on how to create a package (a meta.yaml file and additional files such as build.sh required to compile the package)
# recipes are compiled either by `conda-build` or by automated feedstocks
meta.yaml # mandatory: a file that contains the recipe metadata (only package/name and package/version required)
build.sh # optional: the bash-executable script that installs the files for the package (Linux/macOS)
bld.bat # optional: the cmd-executable script that installs the files for the package (Windows)
run_test.[py,pl,sh,bat] # optional: an optional Python test script that runs automatically if it is part of the recipe
# optional: patches that are applied to the source
# optional: other resources that are not included in the source and cannot be generated by the build scripts (icon files, readmes, build notes)
Anaconda Certified: conda Fundamentals#
[ h ] Anaconda Certified: conda Fundamentals
[ p ] Conda Basics
[ p ] Conda Essentials
[ p ] How to Use Anaconda Notebooks
Introduction
[ p ] Conda Essentials: Introduction
Module 1: The conda directory structure
[ p ] Conda Directory Structure: Learning Objectives
[ p ] How does conda work?
[ p ] What’s in the conda directory?
[ p ] Installing a new environment
[ p ] The conda directory structure
[ p ] What happens when an environment activates?
[ p ] Conda activation scripts
[ p ] Conda Directory Structure: Summary
[ p ] Exercise 1: Explore the environment directory
[ p ] Exercise 2: Locate Python in the new environment
[ p ] Exercise 3: Locating a newly installed Python package
Module 2: Building packages
[ p ] Building Packages: Learning Objectives
[ p ] What are conda recipes?
[ p ] Public recipes: conda_gc_test and toolz
[ p ] Public recipes: prodigal and pandas
[ p ] Conda recipes summary
[ p ] How to build a package
[ p ] Building packages continued
[ p ] Uploading a package
[ p ] Building packages: Summary
[ p ] Exercise 1: Create a new recipe from scratch
[ p ] Exercise 2: Build a package from your recipe
[ p ] Exercise 3: Building packages review questions
Module 3: Introduction to conda-build
[ p ] Introduction to conda-build: Learning Objectives
[ p ] What happens during the conda build process?
[ p ] Generating recipes for Python packages on PyPI
[ p ] Generating recipes for R packages on CRAN
[ p ] Writing a recipe and building a package from scratch
[ p ] Best practices for building packages
[ p ] Building packages: Summary
[ p ] Exercise 1: Add a source section to a recipe
[ p ] Exercise 2: Debugging a recipe
[ p ] Exercise 3: Installing a package
[ p ] Conclusion
[ p ] Anaconda Server Basics
Conda Essentials#
# Exercise 1: Explore the environment directory
conda activate base && echo $CONDA_PREFIX # `~/anaconda3/`
ls $CONDA_PREFIX # 1. list the contents of the conda installation directory
ls $CONDA_PREFIX/envs # 2. list the contents of directory `envs`
conda create -n ce-exercises # 3. create a new environment named `ce-exercises`
ls $CONDA_PREFIX/envs # 4. list the contents of directory `envs` again
ls $CONDA_PREFIX # 5. list the contents of environment `ce-exercises`
conda activate $CONDA_PREFIX/envs/ce-exercises # 6. activate environment `ce-exercises` by its prefix, not its name
echo $CONDA_PREFIX # `~/anaconda3/envs/ce-exercises/`
conda install python # 7. install Python in environment `ce-exercises`
ls $CONDA_PREFIX # 8. list the contents of environment `ce-exercises` again
# Exercise 2: Locate Python in the new environment
ls $CONDA_PREFIX/bin/python # 1. locate the Python executable
ls $CONDA_PREFIX/lib/python3.11/ # 2. locate the Python package
# Exercise 3: Locating a newly installed Python package
ls $CONDA_PREFIX/lib/python3.11/site-packages # 1. list the contents of directory `site-packages` within the Python installation directory
conda install numpy # 2. install NumPy in environment `ce-exercises`
ls $CONDA_PREFIX/lib/python3.11/site-packages # 3. list the contents of directory `site-packages` again
ls $CONDA_PREFIX/lib/python3.11/site-packages/numpy # 4. list the contents of the new directory and compare with https://github.com/numpy/numpy
mkdir my-recipe
echo -e 'package:\n name: my-recipe\n version: 0.0.0' > my-recipe/meta.yaml
conda config --set anaconda_upload no
conda build .
ls $CONDA_PREFIX/conda-bld/osx-64/
tar jxvf $CONDA_PREFIX/conda-bld/osx-64/my-recipe-0.0.0-0.tar.bz2
Anaconda Server Basics#
conda config # generate file `~/.condarc` in the user home directory
conda config --set channel_alias http://gibbons-lms.cs.anaconda.com/api/repo # add Anaconda Server as a channel alias
conda config --prepend default_channels anaconda # set the default channel to channel `anaconda`
conda repo --version || conda install conda-repo-cli # check whether package `conda-repo-cli` is installed
conda repo config --set-site gibbons-lms.cs.anaconda.com # set the site for the conda-cli client (modify the Conda Repo CLI config file)
cat ~/.conda/repo-cli-config.yaml # view the Conda Repo CLI config file
conda repo login # login to Anaconda Server via conda-cli
conda repo mirror --list-all
To Review#
How to import package package
from the parent directory?
import sys
sys.path.append('..')
import package