Wrapping up with workplace optimization

Bob Roebling
Jul 18 · 10 min read

In Part 16 of Beginning Python Programming, we covered testing.

Hopefully, you will use what you’ve learned to ensure that your code is safe from bugs.

In this piece, we’ll talk about optimizing your workflow. You already know everything you need to get started on your own, but if you are looking for hints and tips on setting up a development environment to make your workflow more comfortable, then stick around.

A friend and work colleague helped me get started developing professional Python programs; it is only fair that I credit him for these tips. Thanks, Shawn!

As a disclaimer, this is just a workflow that works for me. If you find it too difficult or too limited change it up to what works for you.

I recently formatted my laptop just for this and I’ll do my best to translate it between macOS, Windows, and Linux. I don’t expect these instructions to change much so change the version number to fit your system.

A note before we move forward: no matter which system you are developing on, you should always download the highest version of Python available to the production system.

This means that if you have three systems where you are deploying your code, you should use the lowest version of Python available between all three. For example, CentOS 7 only uses Python 3.6.5, whereas Fedora uses 3.7.4, so you should download Python 3.6.5.

If you want to download 3.7.4 for Fedora-only applications, feel free, you’ll get features introduced in 3.7 that weren’t available in earlier versions.


macOS and Linux

Prerequisites for macOS

First, we need to install the developer command-line tools. If you have Xcode installed and have run it at least once, these are already installed. If not, open a terminal window (/Applications/Utilities/Terminal.app) and type the following command:

This will cause a dialog box to appear, asking if you want to download and install the command-line tools. Go ahead and wait for the installation to complete.

Next, we need to install the macOS SDK headers. When you install the command-line tools, these are downloaded but not installed. To run the installation, use the following command:

This will install the required dependencies to build the Python interpreter.

Next, we need to install Homebrew. Homebrew is a package manager for macOS similar to Chocolatey on Windows or Apt/Yum on Linux. We can do this using the command on the Homebrew website:

This should be run as a non-privileged user. It will ask for your password during the install.

Once the install completes, we can install pyenv. pyenv allows us to install multiple versions of Python in a way that they do not conflict with each other or the system. We can also choose which Python version we want to use for our user account without affecting the system Python version. We will use Homebrew to install this:

Homebrew will automatically pull any dependencies, such as OpenSSL or Readlines, and install those at the same time. If all goes well, you will see a few success messages. Sometimes we get helpful text explaining what we need to do next. In this case, we need to add pyenv to our bash profile, so it is available next time we launch our terminal:

This probably looks complicated to you if you’ve never seen bash scripting before. Essentially this checks to see if pyenv is installed, if it is, we call the command eval $(pyenv init -). If it doesn’t exist, then skip over this command.

Now that pyenv is installed we need to restart our shell, go ahead and close out this terminal session and open a new one.

Prerequisites for Linux

Install the following prerequisite packages for pyenv as required using yum (RHEL/CentOS 7):

Using apt-get (Ubuntu/Debian):

Python

We can now install any Python version we want. When we first started this series, we were on 3.7.3; now we are on 3.7.4.

Run the following command to install Python 3.7.4:

If all goes well, you will see a successful message.

I had the following error because Xcode was not installed (macOS only):

ModuleNotFoundError: No module named 'pyexpat'

Since I don’t want an 11 GB program just sitting there doing nothing, I needed to find a workaround:

First, I installed xz using brew, then I set the SDKROOT to the command-line tools SDK and set the deployment target to my current macOS version (10.14). I put this in my bash profile, so I wouldn’t have to type this in every time. If I ever run into issues with it, I’ll comment the export… line out using # before the command and create a new terminal session. (You can also use unset SDKROOT to perform the same action for this session.)

Once Python is successfully installed, use the following commands to set your Python version and test it:

If you want to install a different version you can use pyenv install -l to list all versions available. Be mindful there are many, but your interests will most likely be versions containing 3.x.x.

Pip Packages

With your Python version of choice installed and set, we can install a few pip packages to help our workflow. Here are a few that I like:

  • pipx — Installs pip packages into their own virtual environment, then links to your user path.
  • black — Python code formatter that has few options, one less thing you need to think about.
  • httpie — super simple Python HTTP client.

First, we upgrade our default pip and setuptools to the latest version and install pipx while we are at it.

Pipx does not allow installing more than one package at a time right now, so we will install black and httpie with the following commands:

You might get some warnings about ~/.local/bin not found in your path. Just use the following command to fix this:

Finally, I create a directory in my home folder called Developer. This provides a folder with a developer tools image watermarked on it (macOS only). It makes it easy to find later. All of my projects live in here.

Since I use Jetbrains products, I typically keep the generated projects folder name the same, so I can easily find all projects created by an IDE (e.g., ~/Developer/PycharmProjects/testapp/).

This is all we need to do. Any application that you use for Python development, unless otherwise specified, will use the version of Python you installed with pyenv. Near the end of this article, I have included a list of software that I use. Below that, I’ve included a list of tutorials to help you get started with web frameworks.

Windows

This is based on my development workflow on my work PC.

On Windows, I don’t use pyenv or pipx. My workflow is pretty simple.

I store all of my projects in C:\Dev, then I create a quick access shortcut to this location.

I install all versions of Python that I need directly from python.org into C:\Python\ where the folder is named 363x86 for 32-bit Python 3.6.3. This way, I know which version and architecture of Python I am about to use for my virtual environment.

Using this, I can easily create new folders in C:\Dev and invoke C:\Python\372x64\Python -m venv C:\Dev\myproject\venv to create new virtual environments for my applications.

If I am deploying code to Linux, then I set up a remote interpreter in PyCharm to automatically upload a copy of my code to this server. This way, I can invoke my code directly in a Linux environment.

If I only write for a specific version of Python, I’ll set my user path in the system environment to the directory containing the version of Python I use most often.


Software

Editors

I use multiple editors, not because I like my life complex, but because I use each differently. I’ll briefly cover the strengths and weaknesses of each and order them by usefulness.

Pycharm is truly the tool that got me hooked on Python. It provides many tools to extend your workflow. My favorite tools are the remote ssh interpreter, built-in git, and an autocompletion unparalleled to any other Python IDE I’ve used. It also has a great plug-in marketplace that can help extend the IDE even further. It also has built-in support for other languages including HTML editing features that are better than some of the editors that were built for creating HTML.

The only bad thing about this is the cost for the Professional version at $200 annually; this is easily offset by the community version, which removes a few features. Unless you are doing serious Django, Flask, or remote development, you aren’t missing out on anything. It also has an Edu version that includes a built-in Python tutorial.

Microsoft has done a favor for the Python community. With their Python Server available through an extension, autocompletion is useful. It’s also a familiar editor (almost full IDE) that is great for quick edits or for machines that have a hard time opening two instances of PyCharm at the same time. Oh, and it’s free and very extensible.

My only complaint about this is that it runs on Electron which can be sluggish when editing large files.

Sublime Text is pretty amazing for editing large files. While it doesn’t have near the extensions of VS Code or PyCharm, it makes up for this in how fast it can manipulate files megabytes in size. Don’t let the image fool you, it’s available on macOS, Windows, and Linux, just like the other two editors. I use Sublime Text for super quick edits or reviewing large result sets. You are free to evaluate this forever, but you’ll get prompted to buy it every time you save a file. If you feel like paying for it, it costs $100 for a major version license.

Version Control

PyCharm and VS Code already have version control features built-in. Sublime Text offers Sublime Merge which is ok, but hard to justify $100 for the major version license.

Github Desktop seems to have most of the features of Sublime Merge, but it’s free. It keeps track of changes and has full-featured support for GitHub. If you use BitBucket or GitLab, your mileage may vary, but the essential functions should still work.

Fun fact — Linus Torvalds hated using other version control mediums so much that he ended up creating the Git version system to assist with the Linux kernel. Everyone else wanted this too. If you find GUI clumsy, this is the command-line tool you’ve been looking for. This is the basic git client that can also host a git repository on a remote server. I use this more than GitHub desktop, but I still use PyCharm’s version control more. This is when I need to do quick maintenance to git repos, such as rebasing multiple repos or pulling down the latest changes on stale projects.


Communities

Everyone loves Slack, so I’ll recommend this Python community. It used to be called Python Developers, but seems it has gone through a renaming. Have a question? Ask. With over 1,000 members, you usually get a quick response.

The Python forums are suitable for those who don’t want to create another account on the web. Answers may be slower, but you can be sure to find a lot of history here.

You knew this was coming. Stack Overflow is still a great place to go for answers, and Python is no exception to this rule.


Tutorials

We are always thirsty for more, or at least we should be. Here are some excellent resources to take your learning further.

Flask is a more comfortable web framework to use than Django, which is why I posted it first. It’s great for websites that do not require a database backend (although it does a good job). You should use Flask for small websites and basic APIs.

Django Girls is a non-profit organization that has a focus on teaching girls and women how to code. While it is oriented towards women, it can also be an excellent resource for everyone. They have a great tutorial on how to get started with Django. Some of the steps are repeated when you first start, but are worth going through a few times.

Django also has their tutorial, but I found it’s easier to follow along after you get the basics from Django Girls. Django is a web framework that has the batteries included. It’s best used when you are creating a website that is backed by a database.

William Vincent has created a small blog that covers a little bit of everything, with a focus on Python and Javascript. There are podcasts and tutorials scattered throughout, and it’s worth going through.

While I’ve given you these resources, they are to teach you how to get started using these libraries. Ultimately, it’s on you to experiment and move out of “tutorial mode.”


Summary

This concludes this series on Python. I hope you learned something new. We wrapped this up by providing examples of development workflows, a list of tools, and tutorials that will keep you going.

Until next time, keep practicing!

I am not affiliated with any products listed above outside of being a consumer. The views expressed here are my own. I am a basic member of pythondev.slack.com.

Better Programming

Advice for programmers.

Bob Roebling

Written by

Bob Roebling is a Senior Systems Administrator and tech evangelist with a background in multiple programming languages.

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade