Setting up Python Environment

    2 minute read    

This note records steps to setup coding environment from scratch.

Working Locally

Install Python

Install Anaconda distribution.

Create virtual environment

It is better to create a virtual environment to avoid possible clash of version requirement for different projects. You can create virtual environment using vanilla python environment or using Anaconda environment.

Vanilla Python Environment

Alternatively, you may use python virtualenv to create virtual environment. To do this, run the following:

cd /Users/elynn/Dropbox/__openAI/openai
sudo pip install virtualenv      # This may already be installed
virtualenv -p python3 .env       # Create a virtual environment (python3)
# Note: you can also use "virtualenv .env" to use your default python (please note we support 3.6)
source .env/bin/activate         # Activate the virtual environment
pip install -r requirements.txt  # Install dependencies
# Work on the assignment for a while ...
deactivate                       # Exit the virtual environment

Anaconda Environment

To set up a virtual environment, run in a terminal (option + command + ~ in VSCode):

conda create -n openai python=3.6 anaconda

to create an environment called openai.

Then, to activate and enter the environment, run

conda activate openai

To exit, you can simply close the window, or run

conda deactivate openai

To see a list of all your environments, in your terminal window or an Anaconda Prompt, run:

conda info --envs

or

conda env list

To remove a specific environment, use the follow command in the terminal window:

conda remove --name myenv --all

You may refer to this page for more detailed instructions on managing virtual environments with Anaconda.

Install TensorFlow.

But there is a difference between pip intall and conda install, Because Conda introduces a new packaging format, you cannot use pip and Conda interchangeably; pip cannot install the Conda package format. You can use the two tools side by side (by installing pip with conda install pip) but they do not interoperate either. See this post for more details.

# Current release for CPU-only
pip install tensorflow
# GPU pakage for CUDA-enabled GPU cards
pip install tensorflow-gpu

Use pip unintall <packagename> fo uninstall a package.

or use conda:

conda install tensorflow

Install PyTorch

or use conda:

conda install pytorch torchvision -c pytorch

More information is provided by PyTorch website.

Verification

To ensure that PyTorch was installed correctly, we can verify the installation by running sample PyTorch code. The following code construct a randomly initialized tensor.

from __future__ import print_function
import torch
x = torch.rand(5,3)
print(x)

Touble Shooting

Well, it did not work for me for the first time. Running the test code returns “ModuleNotFoundError: No module named ‘torch’” error.

One possible reseaon is that conda package is not uptodate. So one solution is to run the following code before installing PyTorch.

conda update conda
conda install mkl=2018

For me, however, it is because the default version of Python is somehow set to 2.7 instead of 3.7. One can reset the default python to 3.7 (I don’t know how) or uninstall and reinstall Anaconda3.

Other Useful Commands

  1. Check version of python in an environment, first activate and enter the environment, then type:
python --version
# Python 2.7.10
python3 --version
# Python 3.6.7 :: Anaconda, Inc

Working Remotely

In most of the cases, it is better to work remotely.

Updated: