Using Safety in Conda Environments
Using Safety CLI with Anaconda Environments
Overview
Safety CLI is the only dependency scanner powered by Safety's industry-leading vulnerability database. Safety CLI can be used to scan and secure Anaconda projects and environments, with some additional steps required to help make implementation and rollout seamless across your team. This guide will show you how to use Safety CLI effectively with your Anaconda projects.
Table of Contents
General Process
To scan Anaconda environments with Safety CLI, the following general steps are recommended:
Export the Anaconda manifest (list of packages in the Anaconda project):
Separate pip-installed packages and conda-installed packages into two separate temporary requirements.txt files (instructions and examples for this below)
Scan the separated requirements files using
safety scan
Delete the temporary requirements.txt files that were created
Unix-based Systems (Linux/macOS)
For a streamlined workflow on Unix-based systems, use the following bash script:
Usage
Save the script as
conda_safety.sh
Make it executable:
chmod +x conda_safety.sh
Run the script as a replacement for the
safety
command, using any Safety CLI arguments. For example:
or
Setting Up as a Command using an alias
To use the script as a command:
Add an alias in your shell configuration file (
~/.bashrc
or~/.zshrc
):Reload your shell configuration:
Now you can use conda-safety
as a drop-in replacement for the safety
command. Once you've activated your conda environment, use `conda-safety`. For example:
Windows-based Systems
For Windows users, use the following PowerShell script:
Usage
Save the script as
Conda-Safety.ps1
Open PowerShell and navigate to the script's directory
Run the script with Safety CLI arguments:
or
Setting Up as a Command
To use the script as a command in Windows:
Create a directory for the script if it doesn't exist:
Move the script to this directory:
Add the directory to your PATH:
Open System Properties
Click on Environment Variables
Under System Variables, find and edit the PATH variable
Add
C:\Users\YourUsername\Documents\WindowsPowerShell\Scripts
Create a PowerShell profile if you don't have one:
Add this line to your PowerShell profile:
Now you can use conda-safety
as a command in PowerShell as a drop-in replacement for the safety
command. This requires safety
and conda
to already be installed and available in Powershell.
Important Note
Conda-installed packages may differ from standard PyPI packages. As a result, security findings for conda-installed packages may differ from PyPi equivalent packages. Always verify findings for conda-installed packages against the specific versions in your Anaconda environment.
Help and Implementation Assistance
Anaconda environments and setups vary. If the instructions above do not work or you encouter any issues, please don't hesitate to reach out to our support team at support@safetycli.com. We're here to help you ensure the security of your Python projects, regardless of your environment setup.
Last updated