The world’s leading publication for data science, AI, and ML professionals.

Complete Guide to Setting Up a New Python Environment For Data Science

Set up your data science python environment easily within minutes on your new Mac computer with this step by step guide.

Photo by Mia Baker on Unsplash
Photo by Mia Baker on Unsplash

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:

curl https://pyenv.run | bash

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.

  1. In the terminal, type and enter:
cd /users/<your username>/

Replace your username in the command above. Then:

  1. 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
  1. 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.

  1. 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!


That is pretty much it for the guide to setting up a new python environment for your new machine. I hope it helps and you can get right back to being productive in no time after this! 😁


Get my FREE guide to making a deep learning model and easily deploying it as an API here.

Connect with me on Twitter and LinkedIn!


Related Articles