5 Step Guide to Setting Up a New Python Environment For Data Science
By: Yash Prakash
Tags: Installation, Data Science, Github, Macos, Python

When I recently upgraded to a new computer and my first MacOs operating system, naturally on my very first boot, I looked up ways to set up a new Python environment from scratch for all my machine learning and data science needs. I came across many articles on the web and reading two and three guides at the same time had me confused and quite frankly, after a bit of a struggle, I was finally able to get everything installed and working properly when I thought — why not write all of this down so that I could refer to them the next time I need to do the same.

In case there are people like me who are looking for ways to get their computer up and running for their needs, here is the step by step process I used to do mine.

1 — Install Homebrew

Homebrew is the most popular package manager for the mac operating system. You’ll need it to install other important packages in the future.

Open up the terminal and type and enter:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

This should follow up with a screen of easy to follow instructions to install homebrew. For more detailed instructions, you can refer to this official website if you want.

2 — Install a python version management tool

If you’re like me and has a number of projects running simultaneously — some from the web and some of my own — all of them running different python versions too, you’ll also need to have a tool to manage your python versions without resorting to messing up the environment of your projects.

I use pyenv to manage my installed python versions. Type and enter the following into your terminal:

After this comes a slightly tricky part of adding pyenv to your PATH variables. The three lines displayed at the end in the terminal will show you the exact instruction to add pyenv to your PATH. Copy those lines from the terminal.

In the terminal, type and enter:

cd /users/<your username>/

Replace your username in the command above. Then:

2. To show the hidden files in the folder type:

ls -a

This should show your .zhrc file displayed in the list. Open it by typing:

open -e .zhrc

3. When the file is displayed in TextEdit, add the copied lines into it and save and close the file.

That’s it, you’re done! Just restart your terminal after saving and closing the file.

4. Check your installed python versions with the help of pyenv by typing:

pyenv versions

This should show your ‘system’ —the already installed — python version.

3 — Install a particular version of python

Install a specific version of python by typing and entering:

pyenv install <python version> # example: pyenv install 3.9.1

Now when you type and enter pyenv versions, it should show you this version of the python in the list as well.

There is also one optional thing you can do — to set a global python version — by doing this:

pyenv global <version name>

This will be the default python version used by your projects unless you specify it by cd into your project folder and running:

pyenv local <version name>

4— Install a virtual environment for using with your projects

Check the pip version installed:

pip --version

I use pipenv as the virtual environment for my projects. Install it with this line:

brew install pipenv

You can simply activate your python environment by cd into your project folder and running:

pipenv shell

Installing packages is done with the help of:

pipenv install <package-name>

Coming out of the virtual environment can be done by simple entering:

exit

5 — Install Git

Version control is a must have for all projects and you can have it installed on your system by entering:

brew install git

Extra — Install Jupyter Notebook

This is arguably the most popular tool for running data science projects and can be installed using:

pip3 install jupyter

Here is the complete doc + installation guide if want to have a look.

Extra — Install VSCode

If you tend to use a code editor often, what’s better than Visual Studio Code? You can install it by going to this link.

Install the Python extension by Microsoft from the extensions tab in VSCode and you’re good to go!