Guide to Conda for TensorFlow and PyTorch | by Eric Hofesmann


Three issues came up when I switched from pip to conda that took a bit of time to figure out.

1) Pip can run inside conda

If a package you want to use only exists in pip, you can use pip to install it inside of your conda environment. However, pip and conda don’t always play nice together.

The primary issue is that conda is not able to control packages that it did not install. So if pip is used inside of a conda environment, conda is unaware of the changes and may break the environment.

Follow these tips from Johnathan Helmus when using pip and conda together:

  • Never use conda after pip. Install everything that you can with conda , then install remaining packages with pip
  • Create new conda environments. Using pip can result in a broken environment, so always make a new isolated conda environment before installing pip packages.
  • If you need to install aconda package after having used pip, it is better to just make a new environment and reinstall everything in the correct order.

2) Conda envs aren’t globally isolated

The default behavior when creating apip virtual environment is that when inside the new environment, you do not have access to globally installed pip packages. If you want access to global packages, you need to initialize your virtual environment with:

virtualenv env --system-site-packages

conda , on the other hand, lets you access global system packages by default. If you want the environment to be completely isolated from these global packages like with pip, you can create the conda environment based on a clone of an empty pip virtualenv.

virtualenv empty_env
conda create -n env --clone empty_env

Ideally, you should avoid installing global pip packages anyway so this shouldn’t be an issue.

3) CUDA paths need to be specified

If you have a system-wide install of CUDA, then the LD_LIBRARY_PATH environment variable may point to the wrong location after installing CUDA inside of your conda environment.

To fix this, you will want to update LD_LIBRARY_PATH to point to the directory containing cuda within conda when you enter your conda environment (generally that directory is /path/to/conda/pkgs/). Then you’ll want to point it back to the system-wide install of CUDA when you leave the conda environment.

First, navigate to your conda environment location and create the following files and folders:

cd /path/to/anaconda/envs/my_env
mkdir -p ./etc/conda/activate.d
mkdir -p ./etc/conda/deactivate.d
touch ./etc/conda/activate.d/env_vars.sh
touch ./etc/conda/deactivate.d/env_vars.sh

In activate.d/env_vars.sh add the following lines. Substitute /your/path with the path to the installation of CUDA in your conda environment.

#!/bin/bashexport OLD_LD_LIBRARY_PATH=${LD_LIBRARY_PATH}
export LD_LIBRARY_PATH=/your/path:${LD_LIBRARY_PATH}

Generally, this /your/path would be something like:

export LD_LIBRARY_PATH=/home/user/conda/pkgs/cudatoolkit-10.2/lib:${LD_LIBRARY_PATH}

Then add the following lines in deactivate.d/env_vars.sh:

#!/bin/bashexport LD_LIBRARY_PATH=${OLD_LD_LIBRARY_PATH}
unset OLD_LD_LIBRARY_PATH

Being able to install non-Python packages, like CUDA, in only a few commands with conda is a godsend. Using conda in conjunction with the FiftyOne model zoo means you can generate predictions from dozens of models in minutes. However, switching from pip virtual environments to conda environments can result in some unexpected behavior if you’re not prepared.



Source link

Be the first to comment

Leave a Reply

Your email address will not be published.


*