As mentioned before in my article about which technologies to master and learn in 2020 one of the most promising language to learn is definitely Python. As a novice Python developer myself, one of the things I struggled the most was setting up Python environment in OSX.
Installing Python in OSX
OSX already comes with Python, but naturally, it will never come with the latest version available. Also both Python 2.x and Python 3.x are still widely used and it is better to have both versions installed in the same machine. There are many ways to achieve this, but I will show you how to manage it the way I found to be easier.
If you are a developer using OSX there is a good chance you know what Brew is, otherwise you should do some research since it is an exceptional tool that will make life easier for you. Since OSX comes with some version of Python 2.x, we are using brew to install 3.x.
For installing things using Brew it is recommended that you have the latest version of Xcode installed in your system, as well as the Apple Command Line Tools, to install them run the command:
$ xcode-select --install
Once you have brew installed in your system, run this command in your terminal:
$ brew install python@3.8
Brew will handle all the installation process including any needed dependency. At the end of the installation you should have 2 version of Python in your system, to test this if you type in the terminal:
$ python --version
You should get some 2.x version, the default for your OSX installation, and by typing:
$ python3 --version
You should get some 3.8.x since this is the one we just installed. This way you have both versions available.
My anaconda don’t
Now comes the fun part, we will be using Conda as our environment manager, that way you can create different working environments with different modules installed depending on you need, and make sure that when you are moving your code to a server you have all the dependencies needed for your project to work as intended. Anaconda is a full featured python distribution with over 150 packages of things you don’t even know how to use yet and you don’t need, Miniconda is just the basic installer and in my opinion, a better fit to understand the basics of how Python works, so we will be using Miniconda.
To install Miniconda, you can go into their website and download an install the packaged version, or run this commands in your terminal:
$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O ~/miniconda.sh
$ bash ~/miniconda.sh -b -p $HOME/miniconda
After the installer is done you should be able to run the command:
$ conda init
Now, this is where many people have some trouble, depending on your OSX version and if you use bash or zsh as your command line shell of choice, you can get an error where the command conda is not being recognized. If you get this, go to where miniconda is installed /opt/miniconda3/bin/ and from there run the command:
$ conda init // for bash
$ conda init zsh // for zsh
Dont know which one you are using? run this command to know, then the one that applies…
$ echo $0
This should add the needed paths to your .bash_profile or .zshrc files respectively.
Setting up a new Python environment
Now that we have Conda it is very easy to create environments for your projects, this simple command will create an environment named ENVNAME that uses Python 3.6:
$ conda create --name ENVNAME python=3.6
To activate a created environment:
$ conda activate ENVNAME
To deactivate it:
$ conda deactivate ENVNAME
To delete the whole environment:
$ conda remove --name ENVNAME --all
You are set, this is all you for setting up Python environments in OSX, start creating different environments for your test projects and refer to the official Conda documentation to see how to backup, restore and many useful things you can do to facilitate working with different Python environments.