Skip to content

Instantly share code, notes, and snippets.

@mikepsn
Last active March 5, 2020 03:36
Show Gist options
  • Save mikepsn/497e6b6d50a90632e79ee0fbd4af02b9 to your computer and use it in GitHub Desktop.
Save mikepsn/497e6b6d50a90632e79ee0fbd4af02b9 to your computer and use it in GitHub Desktop.
Setting up Python environments with anaconda

Python Packaging with Anaconda

  • Assume Anaconda 3 is installed under Windows 10 with Python 3.7.0.
  • Tensorflow doesn't work with Python 3.7, so we will need to install a new Python 3.6 environment.
  • Secondly, we will install a new 3.7 test environment to test the local pip install of a local python package.

Conda Environment Basics

In the Anaconda Prompt to get a list of environments:

conda info --envs or conda env list

Find the available packages named Python:

conda search python

Create a new environment for Python 3.6

conda create -n py36 python=3.6 anaconda

Activate the new environment:

conda activate py36

Check to see the Python version:

python --version

Deactivate this environment and go back to the base:

conda deactivate or simply activate the (base) environment conda activate base

Note that this py36 environment will only have the basic packages installed. If you want to install numpy, scipy etc, you will need to do so manually. For example to install numpy:

activate py36 conda install numpy deactivate

Cloning an Environment

We want to create a clone of our (base) 3.7 environment so we do all our development in that new environment (e.g. so we can install local packages without polluting the base environment.

conda create --name miketest --clone base

Installing TensorFlow

In theory you can install tensorflow using pip

pip install tensorflow

However, to get the whole thing going (especially for GPU) there are a bunch of dependencies and I found it easier to use the anaconda package.

You can find the details at the Anaconda TensorFlow page.

I created a new conda environment called tp-gpu and installed tensorflow gpu into it as follows:

conda create -n tf-gpu tensorflow-gpu conda activate tf-gpu

This installs all the dependencies such as cudatoolkit and cudnn. The complete list of packages installed in the environment as as follows:

(tf-gpu) mikepsn@corsair-two:~/code$ conda list
# packages in environment at /home/mikepsn/anaconda3/envs/tf-gpu:
#
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                        main  
_tflow_select             2.1.0                       gpu  
absl-py                   0.8.1                    py37_0  
astor                     0.8.0                    py37_0  
blas                      1.0                         mkl  
c-ares                    1.15.0            h7b6447c_1001  
ca-certificates           2019.11.27                    0  
certifi                   2019.11.28               py37_0  
cudatoolkit               10.0.130                      0  
cudnn                     7.6.5                cuda10.0_0  
cupti                     10.0.130                      0  
gast                      0.2.2                    py37_0  
google-pasta              0.1.8                      py_0  
grpcio                    1.16.1           py37hf8bcb03_1  
h5py                      2.9.0            py37h7918eee_0  
hdf5                      1.10.4               hb1b8bf9_0  
intel-openmp              2019.4                      243  
keras-applications        1.0.8                      py_0  
keras-preprocessing       1.1.0                      py_1  
ld_impl_linux-64          2.33.1               h53a641e_7  
libedit                   3.1.20181209         hc058e9b_0  
libffi                    3.2.1                hd88cf55_4  
libgcc-ng                 9.1.0                hdf63c60_0  
libgfortran-ng            7.3.0                hdf63c60_0  
libprotobuf               3.11.2               hd408876_0  
libstdcxx-ng              9.1.0                hdf63c60_0  
markdown                  3.1.1                    py37_0  
mkl                       2019.4                      243  
mkl-service               2.3.0            py37he904b0f_0  
mkl_fft                   1.0.15           py37ha843d7b_0  
mkl_random                1.1.0            py37hd6b4f25_0  
ncurses                   6.1                  he6710b0_1  
numpy                     1.17.4           py37hc1035e2_0  
numpy-base                1.17.4           py37hde5b4d6_0  
openssl                   1.1.1d               h7b6447c_3  
opt_einsum                3.1.0                      py_0  
pip                       19.3.1                   py37_0  
protobuf                  3.11.2           py37he6710b0_0  
python                    3.7.6                h0371630_1  
readline                  7.0                  h7b6447c_5  
scipy                     1.3.2            py37h7c811a0_0  
setuptools                44.0.0                   py37_0  
six                       1.13.0                   py37_0  
sqlite                    3.30.1               h7b6447c_0  
tensorboard               2.0.0              pyhb38c66f_1  
tensorflow                2.0.0           gpu_py37h768510d_0  
tensorflow-base           2.0.0           gpu_py37h0ec5d1f_0  
tensorflow-estimator      2.0.0              pyh2649769_0  
tensorflow-gpu            2.0.0                h0d30ee6_0  
termcolor                 1.1.0                    py37_1  
tk                        8.6.8                hbc83047_0  
werkzeug                  0.16.0                     py_0  
wheel                     0.33.6                   py37_0  
wrapt                     1.11.2           py37h7b6447c_0  
xz                        5.2.4                h14c3975_4  
zlib                      1.2.11               h7b6447c_3  

Installing PyTorch-GPU

First create a new environment:

conda create -n pytorch-gpu

Next install PyTorch from the pytorch conda channel:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

For the CPU only version:

conda install pytorch torchvision cpuonly -c pytorch

NVidia Drivers Under Linux

Note that TensorFlow won't see your GPU if you have the standard open source Nouveau nvidia driver installed. You need to blacklist this driver and install Nvidia's closed source binary blob driver.

Instructions can be found here and here.

There are commands in TensorFlow to show if you are using the correct driver and to see if it can see the GPU.

Details on the GPU can be found by using the nvidia-smi command

nvidia-smi

(tf-gpu) mikepsn@corsair-two:~/code$ nvidia-smi
Thu Mar  5 14:29:51 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 430.50       Driver Version: 430.50       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce GTX 108...  Off  | 00000000:01:00.0  On |                  N/A |
| 29%   33C    P5    18W / 250W |    568MiB / 11153MiB |      1%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1230      G   /usr/lib/xorg/Xorg                           283MiB |
|    0      1556      G   /usr/bin/kwin_x11                            160MiB |
|    0      1564      G   /usr/bin/krunner                               2MiB |
|    0      1566      G   /usr/bin/plasmashell                          91MiB |
|    0      1885      G   /usr/lib/firefox/firefox                       2MiB |
|    0      1922      G   /usr/lib/firefox/firefox                       2MiB |
|    0      1980      G   /usr/lib/firefox/firefox                       2MiB |
|    0      2000      G   /usr/lib/firefox/firefox                       3MiB |
|    0      2022      G   /usr/lib/firefox/firefox                       7MiB |
|    0      2052      G   /usr/lib/firefox/firefox                       2MiB |
|    0      2078      G   /usr/lib/firefox/firefox                       2MiB |
|    0      2104      G   /usr/lib/firefox/firefox                       2MiB |
+-----------------------------------------------------------------------------+

General details on your system can be found using neofetch:

(tf-gpu) mikepsn@corsair-two:~/code$ neofetch
            .-/+oossssoo+/-.               mikepsn@corsair-two 
        `:+ssssssssssssssssss+:`           ------------------- 
      -+ssssssssssssssssssyyssss+-         OS: Ubuntu 19.10 x86_64 
    .ossssssssssssssssssdMMMNysssso.       Host: CORSAIR ONE V1 
   /ssssssssssshdmmNNmmyNMMMMhssssss/      Kernel: 5.3.0-40-generic 
  +ssssssssshmydMMMMMMMNddddyssssssss+     Uptime: 1 day, 4 hours, 44 mins 
 /sssssssshNMMMyhhyyyyhmNMMMNhssssssss/    Packages: 2445 (dpkg), 7 (snap) 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Shell: bash 5.0.3 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Resolution: 2560x1600 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   DE: KDE 
ossyNMMMNyMMhsssssssssssssshmmmhssssssso   WM: KWin 
+sssshhhyNMMNyssssssssssssyNMMMysssssss+   Theme: Breeze Dark [KDE], Breeze [GTK3] 
.ssssssssdMMMNhsssssssssshNMMMdssssssss.   Icons: breeze-dark [KDE], breeze [GTK3] 
 /sssssssshNMMMyhhyyyyhdNMMMNhssssssss/    Terminal: konsole 
  +sssssssssdmydMMMMMMMMddddyssssssss+     Terminal Font: Hack 11 
   /ssssssssssshdmNNNNmyNMMMMhssssss/      CPU: Intel i7-8700K (12) @ 4.700GHz 
    .ossssssssssssssssssdMMMNysssso.       GPU: NVIDIA GeForce GTX 1080 Ti 
      -+sssssssssssssssssyyyssss+-         Memory: 4446MiB / 15949MiB 
        `:+ssssssssssssssssss+:`
            .-/+oossssoo+/-.                                       
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment