DEV Community

Cover image for Getting Started With Python Right!
Adron Hall
Adron Hall

Posted on • Updated on

Getting Started With Python Right!

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:

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)

Collapse
 
sobolevn profile image
Nikita Sobolev

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.

GitHub logo wemake-services / wemake-python-styleguide

The strictest and most opinionated python linter ever!

wemake-python-styleguide

wemake.services Supporters Build Status Coverage Status Github Action Python Version wemake-python-styleguide


Welcome to the strictest and most opinionated python linter ever.

wemake-python-styleguide logo

wemake-python-styleguide is actually a flake8 plugin with some other plugins as dependencies.

Quickstart

pip install wemake-python-styleguide

You will also need to create a setup.cfg file with the configuration.

We highly recommend to also use:

  • flakehell for easy integration into a legacy codebase
  • nitpick for sharing and validating configuration across multiple projects

Running

flake8 your_module.py

This app is still just good old flake8 And it won't change your existing workflow.

invocation resuts

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.

flake8 pylint black mypy wemake-python-styleguide
Formats code?
Finds style issues? 🤔 🤔
Finds bugs? 🤔
Collapse
 
adron profile image
Adron Hall

Will check that out Nikita, thanks for referencing it!

Collapse
 
tlockney profile image
Thomas Lockney

The part where you show aliasing pip is likely to cause some problems:

  1. Since you're using pyenv, the version of pip you use should always be the one associated with the current version of Python, which won't be the case when you later switch versions.
  2. People who aren't clear on what's going on will likely copy the code you included verbatim, so instead of aliasing the version of pip referenced in the pyenv path, it's going to always point at the brew installed version.
  3. You really should use virtualenv for pretty much everything and try to avoid ever installing libraries into your global Python environment. If you need tools accessible outside a virtualenv, check out pipx.
Collapse
 
adron profile image
Adron Hall

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.