How to Set up Python3 the Right Easy Way! | by Salma El Shahawy | Towards Data Science

Update 12/12/2020 — Utilizing pyenv virtualenv to automate activate/deactivate the virtual environment. — Joe Klemmer Suggestion
Recently, I treated myself by purchasing a new Macbook Pro and started setting up my development environment for python. This step is pretty essential because if you did it the wrong way, plenty of issues would arise and distract you from focusing on the actual development work. So, I’ve decided to write a comprehensive guide listing the steps to help others setting their python development environment from scratch. Hence, you have a more comfortable life handling errors. Let’s get started!
For Data Scientist who doesn’t have much experience in the shell command.For Data Scientists/Developers who have errors like import pandas as pd ImportError: No module named pandas
For Data Scientist who doesn’t have much experience in the shell command.
For Data Scientists/Developers who have errors like import pandas as pd ImportError: No module named pandas
3. For Data Scientists/Developers who want to make python3 the default interpreter — type python → get python3 not python 2.7.
4. For Data Scientists who want to start using virtualenv within their machine learning projects to have reproducible notebooks — no more fighting between Dev-ops Engineers and Data Scientists.
5. For future me 😎 in case I treat myself again!
First, you need to do is installing the Xcode if you don’t have one already. This step is necessary because MacOs has to set up some local development utilities, including various tools like git.
The second step would be the magic wand of MacOs, which is homebrew. All you have to do is to copy and paste the link into your terminal and hit return, and that’s it.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Please be sure to navigate to the link before installing and reading what will be installed on your machine to ensure that you are ok with it.
Once installed, you can grab a cup of coffee and start installing whatever you want without worry about anything. Don’t jump to conclusions and begin installing python using brew! Instead, install the environment manager pyenv. Pyenv is a tool to manage the python versions installed in your local machine; it is available to many other operating systems, including Ubuntu, windows.
Stick with one version of python across the project workspace.Switch between python versions peacefully.If you want to use a specific python version like 2.7, for instance.Be able to make python3 as the global interpreter.
Stick with one version of python across the project workspace.
Switch between python versions peacefully.
If you want to use a specific python version like 2.7, for instance.
Be able to make python3 as the global interpreter.
Open terminal and type the following command to install pyenv using homebrew.
Open terminal and type the following command to install pyenv using homebrew.
brew install pyenv
Please note that MacOs use zsh as the default shell command. in case you want to move into bash just run chsh -s/bin/bash in the terminal. Also, you can change it using the GUI in MacOs using this guide.
2. Restart the terminal for changes to take effect or run exec "$SHELL".
3. Set up the zlib compression algorithm and SQLite database to avoid any build issues if you intend to develop python packages or frameworks to automate your machine learning pipeline. You can configure them simply by exporting their paths in the .bash_profile
export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/sqlite/lib"export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/sqlite/include"
In case you don’t have a .bash_profile file, you can follow this answer on StackOverflow to create one or this one.
4. The next step is to install xz package using homebrew. This step is optional; however, it may avoid Runtime errors when importing pandas. This error might be raised due to lzma compression that the python installation is not complete. We need to install some dependencies — like xz package, before python installation via the pyenv to avoid those errors. You can run the following line in the terminal:
brew install xz
If you already installed python via pyenv before xz, you can use the pyenv uninstall <the version>. The installation of xz should be prior to pyenv.
5. Now it is finally time to install python 😄. To do so, you need to choose which python version you want to install. you can list all the available python versions supported by pyenv using this command
pyenv install --list
pyenv supports many python versions including anaconda, jython, miniconda, etc.
6. I will install python version 3.8.6(I found pandas installation has some issues with 3.9.0) by running the following command in the terminal :
pyenv install 3.8.6
7. The following command should make the installed version global. This will override python 2.7 shipped with the MacOs by default — no more python3 command👌.
pyenv global 3.8.6
8. To confirm
pyenv which python/Users/salmaelshahawy/.pyenv/versions/3.8.6/bin/python
9. To enable shims and autocompletion, you need to include this configuration in the .bash_profile if you are using bash — as per documentation instruction.
Navigate to the home directory and open the .bash-profile. Then copy and paste the following snippet — check pyenv for zch.
if command -v pyenv 1>/dev/null 2>&1; then  eval "$(pyenv init -)"fi
To avoid the persistent warning about upgrading the pip package. It would be best if you upgrade the pip package.
According to the documentation, you can run the following command in the terminal to upgrade
python -m pip install -U pip
If you still have issues with the upgrade, run the previous command with the sudo -H command, however, this is not preferable.
Thanks to Joe Klemmer suggestion, I was able to use the pyenv-virtualenv plugin instead of the virtualenv package. Utilizing this plugin would automatically activate and deactivate the virtual environment after adding the required configuration in the bash_profile — no more source/bin/activate👍.
If you like to use virtualenvwrapper or virtualenv, I will keep the steps below for future reference.
Begin with installing pyenv-virtualenv via the homebrew
brew install pyenv-virtualenv
To enable automatically activating the virtualenv every time you open a project directory, you need two steps:
Open the .bash_profile and paste the following line.
Open the .bash_profile and paste the following line.
eval "$(pyenv virtualenv-init -)"
2. Create a new environment venv38 from the current pyenv version via
pyenv version3.8.6 (set by /Users/salmaelshahawy/.pyenv/version)pyenv virtualenv venv38 
3. Check if it was created
pyenv virtualenvsvenv38 (created from /Users/salmaelshahawy/.pyenv/versions/3.8.6)
4. Create a project directory with the valid virtualenv environment version
mkdir package cd package touch .python-versionecho "venv38" >> .python-version
Now, you can easily switching between environments easily without worry about activating commands.
It is a good practice to use a virtual environment for your projects so you can worry less about sharing your projects, notebooks, packages, frameworks, etc. So, your team can reproduce the code and easily running your code without issues. I would focus on utilizing vertualenv; however, other methods can automatically initialize the virtualenv every time you create a project directory, virtualenvwrapper, for example. Start by installing virtualenv using the following.
pip install virtualenv
Now, you may start creating a new project folder with a new virtualenv using the following series of commands.
mkdir package cd package virtualenv devenv --system-site-packagessource devenv/bin/activatetouch test.pypip install pandas
Create a new project directory named package and move inside of it.Initialize a new virtual environment named devenv and the flag --system-site-packages to include the preinstalled packages in your system like setuptools and wheels, for instance — the flag could be surpassed, it is a personal preference.Activating the virtual environment using the source command.Create a new python file to start coding.Installing the required packages within this project.
Create a new project directory named package and move inside of it.
Initialize a new virtual environment named devenv and the flag --system-site-packages to include the preinstalled packages in your system like setuptools and wheels, for instance — the flag could be surpassed, it is a personal preference.
Activating the virtual environment using the source command.
Create a new python file to start coding.
Installing the required packages within this project.
As demonstrated in the captured image, the file ran successfully free from any errors. The command deactivate might be used to deactivating the running virtual environment.
It is a good practice to include a requirements.txt file with the project directory to make it easy for others to download and run smoothly. This can be done using the pip freeze command to list all the packages in requirements.txt format — specifying each package's specific version to avoid conflict.
pip freeze > requirements.txt
Also, you can use pip list to list the installed packages — check the main differences between list and freeze commands.
If you use VSCode, make sure that it utilizes the proper interpreter — the created virtual environment. You can follow this guide to switch between the interpreters.
For MacOs, press shift + cmd + p, then navigate to Select Interpreter.
Then select the virtualenv in our case, is devenv
If you want to install a newer version of python via pyenv, you can list all the installed versions using:
ls ~/.pyenv/versions/3.7.1   3.8.6
To remove a specific version
rm -rf ~/.pyenv/versions/3.7.1
This will remove the specified version from your machine.
At this point, the development environment is up and running. That way, you would spend less time debugging intimidating system issues and focus on building instead.
Another way to set up your development environment without worried about writing some shell commands is to use Ansible Playbook. It is a set of configurations for most of the software required for the Mac. It does include Sequal Pro, homebrew, etc.
If you would like to make your terminal more pretty with colors, you can use this configurations to set your bash_profile.
If you would like to make it simple like mine, you can fork or copy my bash_profile config file at this link, including anaconda config, but different python version, this link to the simple bash_profile to follow up with this tutorial, in addition to git-completion.
Finally, I wish this gave a comprehensive guide to set up your python development environment. If you encountered any issues, please list them in the comment section; I would be happy to help. The best way to encourage me is by following me here on Medium, LinkedIn, and Github. Happy learning!