Many specialized tools are written for some version of Python like python2.7 and has dependencies on some versions of packages like pandas 0.7.3. Installing these older versions will remove newer versions and create conflicts with existing code. So a better option is to create a virtual environment with the specific package versions only.
For example, QSTK does not work with Python 3 or pandas 0.21. It only works with python2.7 and pandas 0.7.3. So we have to create a virtual environment and install these versions.
virtualenv --python=/usr/bin/python2.7 ~/python2.7-virtual-env
This will create the ~/python2.7-virtual-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
Now go to that directory and run source activate to start a new environment (just like a chroot environment).
source ~/python2.7-virtual-env/bin/activate
This will start a new environment. To test that you are really in the environment do
$ which python
~/python2.7-virtual-env/bin/python
$ python --version
Python 2.7.12
Python 2.7.12
The environment is now using the local version of python which is python 2.7
Now we can install the older versions of the required packages.
$ pip install pandas==0.7.3
==0.7.3 forces install of the version 0.7.3 of pandas. It removes newer versions if already installed by default.
Install other packages if you need to.
Setting up a virtual environment in Anaconda
Now Anaconda itself is a virtual environment with the latest version of scientific and statistical tools. However, there will be instances where certain older codes will not run with newer versions of the packages. For example, the pandas datareader library which pulls data from Yahoo and Morningstar is broken in version 0.6.0 (See my GitHub page github.com/saugatach/stockanalysis). Let us say we are trying to work around this issue and want to get back pandas-datareader v0.5.0 but also want to keep the latest pandas-datareader v0.6.0. So we create a separate virtual environment within Anaconda called "stocks".
The process is very well detailed in the conda docs conda.io/docs/user-guide/tasks/manage-environments.html.
$ conda create --name stocks python=3.6 pandas-datareader==0.5.0
This creates a virtenv called stocks which has python 3.6 and the older pandas-datareader.
We can activate the environment by
$ source activate stocks
The CLI prompt should have the environment name as the prefix. We can check if the correct version of our package is installed.
The CLI prompt should have the environment name as the prefix. We can check if the correct version of our package is installed.
(stocks) $ pip3 list
........
numpy (1.15.1)
pandas (0.23.4)
pandas-datareader (0.5.0)
pandocfilters (1.4.2)
parso (0.3.1)
........
numpy (1.15.1)
pandas (0.23.4)
pandas-datareader (0.5.0)
pandocfilters (1.4.2)
parso (0.3.1)
........
Comments