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 installing Python 3 on Windows 10. For Linux specifically check out these docs.
Once brew is installed issue the command to install the "Simple Python Version Management" pyenv. If this app gives flashbacks to Ruby, it is indeed forked from rbenv so there's that.
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 ~/.bash_profile
.
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
run 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 -V
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.
which pip3
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!
References:
- 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), open source projects, database work, data science, and much more.
Top comments (4)
The next step is to create a new
.venv
and to install wemake-python-styleguide to lint all your python code!It will force you to learn all the best practices just be obeying the linter rules. Kind of a tutor that is always with you.
wemake-services / wemake-python-styleguide
The strictest and most opinionated python linter ever!
wemake-python-styleguide
Welcome to the strictest and most opinionated python linter ever.
wemake-python-styleguide
is actually a flake8 plugin with some other plugins as dependencies.Quickstart
You will also need to create a
setup.cfg
file with the configuration.We highly recommend to also use:
Running
This app is still just good old
flake8
And it won't change your existing workflow.See "Usage" section in the docs for examples and integrations.
We also support Github Actions as first class-citizens Try it out!
What we are about
The ultimate goal of this project is to make all people write exactly the same
python
code.Will check that out Nikita, thanks for referencing it!
The part where you show aliasing pip is likely to cause some problems:
Hey Thomas, thanks for pointing that out. Reviewing virtualenv right now and am conjuring up some text to edit this post and write up a subsequent post on virtualenv.