pip packages
When using Conda Virtual Environments (VE), it is usually recommended to install all packages through Conda whenever possible, with pip as an alternative.
Using a mix of Conda and pip requires extra caution.
If a package is not available in Conda, then it can be installed using pip. This minimizes dependency conflicts.
Note that it is best to use pip install at the very end to avoid problems. Once pip has been used, it is no longer recommended to use Conda to install other packages. Packages installed by pip may have conflict with Conda packages. If you then use Conda to install those packages that have conflict, Conda will try to resolve the conflict, and this will cause problems. |
Managing Python Packages with pip
Example 1: Use pip directly in a virtual environment
In the below example, the python camelcase
package which capitalizes the first letter of each word was downloaded using pip
command.
-
Log in to Discovery.
-
Load the conda module
module load conda
-
Activate the virtual environment
conda activate my_env
-
Use the pip command to install the
camelcase
packagepip install camelcase
-
After the successful installation of the package, launch the python CLI and paste the codes below line after line.
>>> import camelcase >>> c = camelcase.CamelCase() >>> txt = "hello world" >>> print(c.hump(txt)) Hello World
Thus, the text
hello world
is converted to a Camel case format.
Example 2: Use pip when creating a virtual environment
1. Prepare the YAML file conda_myenv.yml
name: myenv
channels:
- conda-forge
dependencies:
- python=3.6
- numpy
- pandas
- pip:
- camelcase
Remember to use pip at the very end.
2. Log in to Discovery
3. Load the conda module
module load conda
4. Build your virtual environment
conda env create -f conda_myenv.yml
conda_myenv.yml
is the YAML file you prepared.
See Create & Use section 3. Create the environment: (1) Pre-defined ENV
for more details.