UPDATE POST Added details about virtualenv to complement this post, per Thomas’ comment in the comments section below! Read this post for MacOS specific system Python setup, then read that for more options on how to set things up right!
Recently I sat down to get started on some Python work, specifically on MacOS. I’ve written code in Python before and tried out 2.x and 3.x before. I’m going with 3.x for this and upcoming articles but one thing became apparent. The Python tech stack is still ferociously fragmented in a number of ways, and this post is to provide some clarity if this is your first endeavor into the stack.
First off, 2.7 and 3.8 are the versions that exist in some state as the latest versions. 2.7 is obviously not the latest but is still installed, coded in, and used in a vast number of projects where Python is used. On MacOS for example, it’s the default still, and it should be left alone and the latest installed with appropriate Python environment, PIP for dependency, and related ecosystem necessities. As I walk through these steps, that’s important to know.
Whatever the system environment starting, check out what is on the machine. If on Windows, it’s safe to bet there is nothing on the machine. On Linux or MacOS there will be some version of Python already installed. Running
python --version to determine which version of the language is installed.
If you have a version you can then check out
which python to figure out where it is installed. This can come in handy if things get a bit confusing, or verifying where the install is currently setup or pointing to within the file structure.
First Step: Get a tool to install Python
On MacOS get brew before doing anything else. For details on Windows check out the docs here. There’s also a great Digital Ocean post by Lisa Tagliaferri “How to Install python 3 and Set Up a Local Programming Environment on Windows 10” if you’re installig Python 3 on Windows 10. For Linux specifically check out these docs.
brew install pyenv
Second Step: Install the Latest Python
With pyenv install a sepcific version of Python with the following command.
pyenv install 3.8.0
If there is a new version, install that version instead. On MacOS there is also, of course, already version 2.7 but with pyenv it’ll get the appropriate pieces remapped so that this latest can become the defacto used Python version to work with. To get pyenv to setup this latest installed version us the CLI to set it as the global Python.
pyenv global 3.8.0
One final step to ensure pyenv can be used when and where necessary, now and ongoing in development needs, set the following in your .zshrc.
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.zshrc
If you’re using bash still, make the change accordingly to your
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bash_profile
Third Step: Get PIP Installed.
To check the version of
pip -V. This command not only shows you which version of pip is installed, but also what version of Python pip is installed to oeprate on (with/on/at/to??). The command and result would look something like this.
pip 19.0.3 from /Users/adron/.pyenv/versions/3.7.3/lib/python3.7/site-packages/pip (python 3.7)
Technically, when brew (Homebrew) installed the latest Python has also installed pip and it should be mapped accordingly as shown above in the
pip -V command. However this can be set correctly if it isn't mapped already. Get the path where pip3 is installed by issuing a which command.
The path returned, likely
/usr/local/bin/pip3, add it to a command to echo it into the startup script of your choice. For example, to add it to the zshrc file.
echo "alias pip=/usr/local/bin/pip3" >> ~/.zshrc
If using bash add it as such.
echo "alias pip=/usr/local/bin/pip3" >> ~/.bash_profile
Fourth Step: Run Some Python Code!
Alright, now that this is done, let’s run some actual Python code and make sure things are installed and setup correctly. For this I created a directory I simply called
python-examples.py. Then added the first line of code, a simple print statement.
print("Hello World! BOOM!")print("These examples point out how these variables can be declared and printed.")
Executing that, just by executing the file
python python_examples.py and the following results display.
Hello World! BOOM!
These examples point out how these variables can be declared and printed.
Alright, with that confirmed, we’re all set for further Python development! Enjoy!
- Python Docs
- Python v3 Tutorial
- Not discussed here, but I love the JetBrains IDE’s, and they’ve got one for Python called PyCharm.
- More details about setting up and other ways to put things together by Matthew Broberg @mbbroberg and Moshe Zadka @moshezadka in the post “The Right and Wrong Way to Set Python 3 as Default on the Mac”.
My Blog! I have a personal blog at https://compositecode.blog/ that you can also subscribe to where I post additional collected material, thoughts, music, meetups, conferences, videos (like Twitch @ Thrashing Code), open source projects, database work, data science, and much more.