<<

howtopython Documentation Release 0.0.0

Kenneth Reitz

Jul 09, 2018

Contents:

1 Getting Started with Python 3 1.1 Twitter Accounts to Follow...... 3 1.2 Getting Python Installed...... 4 1.3 Understanding Dependencies...... 4 1.4 Installing Pipenv...... 4 1.5 Using Pipenv...... 4 1.6 Understanding Source Control...... 5

2 The Interpreter 7 2.1 Running a Script...... 7 2.1.1 Dependencies...... 7 2.2 Python Interpreter Tricks...... 8 2.2.1 _ Trick...... 8 2.2.2 Bytecode Trick...... 8 2.2.3 Interactive Trick...... 9

3 Learning the 11 3.1 The BDFL...... 11 3.2 Python Foundation...... 11 3.3 Python Conferences...... 11 3.4 Python User Groups...... 11 3.5 PEPs...... 12 3.5.1 Notable PEPs...... 12 3.5.2 Submitting a PEP...... 12

4 Text Editors 13 4.1 3...... 14 4.1.1 Sublime Text 3 Plugin Recommendations...... 14 4.1.2 Additionally Useful Plugins...... 14 4.1.3 Sublime Text 3 Tricks...... 15 4.2 (VS Code)...... 16 4.2.1 Microsoft VS Code Extensions Recommendations...... 16 4.2.2 Additionally Helpful Plugins...... 16 4.2.3 Microsoft VS Code Tricks...... 17 4.2.4 Microsoft VS Code Launcher...... 17 4.2.5 Rulers...... 17

i 5 IDEs 19 5.1 PyCharm...... 19

6 Indices and tables 21

ii howtopython Documentation, Release 0.0.0

This guide exists to teach you the very basics of how to effectively use Python and its tooling, as a .

Contents: 1 howtopython Documentation, Release 0.0.0

2 Contents: CHAPTER 1

Getting Started with Python

Welcome to Python! Not only are you entering the new world of a programming language, but you are also entering an ecosystem of professional and hobbyist developers from literally every corner of the globe coming together to the world a better place through software (and make a little bit of money at the same time)! Of course, that last part is optional — you are totally free to code Python on your own and chose not to interact with anyone within the community — but, you’ll be missing out on the best part of what makes Python Python!

1.1 Twitter Accounts to Follow

Twitter is an excellent way to keep in touch with what’s going on with the Python community. Here is a very non–exhaustive (but evolving) list of some suggetions of who to follow on Twitter, to get started: • The Python Software Foundation (@ThePSF) • Kenneth Reitz (@kennehreitz) — Myself, the author of this . I often tweet about Python–related topics, as well as music, photography, and other side-projects I have going on. • Guido Van Rossum (@gvanrossum) — The creator of Python itself. Doesn’t tweet much, but is occassionally accessable. Very kind soul. Keep in mind, he gets a lot of attention. • Nick Coghlan (@ncoghlan_dev) — Core Python developer, very active on Twitter, has very thoughtful thoughts about Open Source and the direction of Python in general. • Lynn Root (@roguelynn) — Closely related to PyLadies. • Armin Ronacher (@mitsuhiko) — The creator of Flask, Click, Sphinx, and many other wonderful Python utilities we all know and love. Mostly found writing iOS and Rust code nowadays. • Corey Benfield (@Lukasaoz) • Alex Gaynor (@alex_gaynor) • Yarko T. (@yarkot) • David Beazley (@dabeaz)

3 howtopython Documentation, Release 0.0.0

• Brett Cannon (@brettsky) • Danny Greenfield (@pydanny) • Raymond Hettinger (@raymondh) • Jeff Forcier (@bitprophet) — The creator of Fabric, and maintainer of many open source libraries.

1.2 Getting Python Installed

Of course, the first thing you need to do is install Python on your machine. If you go to the Python.org website, you may be a bit confused about which version of Python you should be using. The correct answer is: Use the latest version of Python 3. As of the time of this writing, that is version 3.7.0. Here are some great installation guides for various system types: • Installing Python 3 Properly on MacOS • Installing Python 3 Properly on • Installing Python 3 Properly on Windows

1.3 Understanding Dependencies

Applications, scripts, and utilities built with Python typically have dependencies attached to them, which are Python modules they require to run/operate with, that need to be installed before you can use the software. A , like Pipenv (which we’ll cover shortly), or the lower–level pip (in conjunction with virtualenv can be used to install and manage these dependencies, which are typically hosted on either on PyPi (The Python Package Index) or GitHub. You’ll typically see these required packages (and any specific versions) declared in one of the following files: Pipfile, requirements.txt, or setup.py.

1.4 Installing Pipenv

The next step is to install Pipenv, our packaging tool of choice. Package managers allow us to easily manage (resolve, install, uninstall) dependencies and virtual environments for projects. Python.org has a great guide available for installing Pipenv that also covers its usage. If you’re on a Mac, and have homebrew installed, the following command will suffice:

$ brew install pipenv

Here’s a great blog post covering the basic concepts presented by Pipenv, and why it’s an excellent choice for your first project.

1.5 Using Pipenv

First, $ cd into your new project directory (after you $ mkdir and $ init it, of course), and simply run $ pipenv install requests to install the requests , which is one of our favorites.

4 Chapter 1. Getting Started with Python howtopython Documentation, Release 0.0.0

Then, run $ pipenv shell to run a shell that will have a $ python available in which import requests will function properly. Pretty simple :) For further instructions, check out the Pipenv documentation.

1.6 Understanding Source Control

In the Python community, we typically use git, a system. Tools, like git, are used to store all changes to code files, so you can go back to any point in history and reference yourself later if needed. They are also very useful for collaborating with others. Git is a bit tricky to get started with, but here’s a great guide that will get you setup and teach you the basics. It’s a requirement if you’re ever going to work with Python in a professional environment.

1.6. Understanding Source Control 5 howtopython Documentation, Release 0.0.0

6 Chapter 1. Getting Started with Python CHAPTER 2

The Interpreter

Python is an interpreted language, similar in the way to how operates, with bytecode. However, unlike Java, you will normally work with *.py files directly, instead of compiled bytecode files (*.pyc). To fire up the Python interpreter, open up your terminal/, and type python or python3 (depend- ing on your installation). You should see something that looks like this:

Python 3.6.3 (default, Oct3 2017, 21:45:48) [GCC 7.2.0] on linux Type"help","copyright","credits" or "license" for more information. >>>

This is known as the Python interpreter, and is a REPL (Read–Eval–Print Loop). This allows you to type Python code, import modules, and interact with code you’ve written without having to write files to disk, in an interactive manner. This interactive mode is one of Python’s super-powers, compared some other programming languages.

2.1 Running a Script

To run a Python script (that you’ve either written or downloaded from the internet), simply run the following:

$ python3 name-of-script.py

This will tell Python to load and execute the script provided.

2.1.1 Dependencies

Sometimes, a script will not run because it does not have the necessary dependencies installed. You’ll normally see this come across as a ModuleNotFoundError, embedded within a confusing (if you’re new) exception message:

Traceback (most recent call last): File"name-of-script.py", line1, in (continues on next page)

7 howtopython Documentation, Release 0.0.0

(continued from previous page) import requests ModuleNotFoundError: No module named'requests'

To resolve those dependencies (in this case, requests), ensure you have Pipenv installed (covered in the previous section), and run the following:

$ pipenv install requests

Note: If you downloaded a directory of files, check for a Pipfile or a requirements.txt file in the root directory. If it is present, simply running $ pipenv install will install all of the dependencies needed, without you needing to specify them.

This will install requests into a virtual environment. Then, you have two options for running your script: The first option is to use $ pipenv run:

$ pipenv run python name-of-script.py

The second option is to run $ pipenv shell, which will spawn a new shell where python is the one from your virtual environment:

$ pipenv shell (project-vHd3e) $ python name-of-script.py

Note, that if you run pipenv shell, its effects only last during your current terminal session. The next time you load your terminal, you’ll have to run $ pipenv shell again.

2.2 Python Interpreter Tricks

There are a few tricks the interpreter has up its sleeve that I recommend you utilize on a regular basis.

2.2.1 _ Trick

The _ variable can be used to reference the last thing that was eval’ (e.g. automatically printed to the screen). For example:

>>> 1 1 >>> a=_ >>> print(a) 1

Very useful when using the interactive interpreter!

2.2.2 Bytecode Trick

The second trick is PYTHONDONTWRITEBYTECODE. If you set this environment variable (e.g. in your ~/.bashrc file or similar), Python won’t write *.pyc files to disk, which is great, because they are incredibly annoying. I’ve been using this environment variable for 7+ years for development and have never had any problems because of it.

8 Chapter 2. The Interpreter howtopython Documentation, Release 0.0.0

2.2.3 Interactive Mode Trick

The third trick is to run a Python script, say hello.py in “interactive” mode, with the $ python -i hello.py flag. This flag will run the script, like usual, but will then drop you into a REPL afterwards that has all the locals and globals from the script loaded, for your interactive pleasure. Very useful.

2.2. Python Interpreter Tricks 9 howtopython Documentation, Release 0.0.0

10 Chapter 2. The Interpreter CHAPTER 3

Learning the Basics

We’ve already covered that Python is more about the people than it is about the code. So, let’s learn a bit about the organizations and processes these people participate in.

3.1 The BDFL

Guido van Rossum, the creator of Python, is often referred to as the BDFL — the Benevolent Dictator For Life.

3.2 Python Software Foundation

The Python Software Foundation is a non–profit organization that is responsible for holding and protecting the intel- lectual property of Python, the Package Index, PyCon, and related. It heavily contributes towards the sustainability of the language and its ecosystem both financially and through infrastructure. Learn More about the PSF.

3.3 Python Conferences

The major events for the Python community are developer conferences. The two most notable conferences are PyCon, which is held in the US, and its European sibling, EuroPython. A comprehensive list of conferences is maintained at pycon.org.

3.4 Python User Groups

User Groups are where a bunch of Python developers meet to present or talk about Python topics of interest. A list of local user groups is maintained at the Python Software Foundation Wiki.

11 howtopython Documentation, Release 0.0.0

3.5 PEPs

PEPs (Python Enhancement Proposals) are the documents/process through which Python gets enhanced and changed, over time. The most infamous PEP is PEP8, which documents how to properly style Python code within the community. Anyone can propose a PEP, even you! It’s a long process, for good reasons — you have to convince a lot of people that your idea is a good one. There are three different types of PEPs (as defined by PEP 1): Standards Describes a new feature or implementation. Informational Describes a design issue, general guidelines, or information to the community. Process Describes a process related to Python.

3.5.1 Notable PEPs

There are a few PEPs that could be considered required reading: • PEP 8: The Python Style Guide. Read this. All of it. Follow it. • PEP 20: The Zen of Python. A list of 19 statements that briefly explain the philosophy behind Python. • PEP 257: Docstring Conventions. Gives guidelines for semantics and conventions associated with Python docstrings. You can read more at The PEP Index.

3.5.2 Submitting a PEP

PEPs are peer-reviewed and accepted/rejected after much discussion. Anyone can write and submit a PEP for review. Here’s an overview of the PEP acceptance workflow:

../_static/pep-0001.png

12 Chapter 3. Learning the Basics CHAPTER 4

Text Editors

Writing Python code typically involves what it known as a . Our recommendation for a text editor is either Sublime Text 3, which costs money but is free to use, or Microsoft VS Code. The functional difference between these two editors is small — they both accomplish the same things, effectively. Which one you chose is a matter of personal taste. There are other editors, like , , or , which you may chose to use instead. Whatever works best for you is best.

I personally use and vastly prefer Sublime Text 3 to all other options available. It’s easily my favorite editor. If it didn’t exist though, I’d be using VS Code.

Note: There are a few “soft requirements” for a text editor to have, when working professionally (or recreationally), with Python code: • Support for “soft tabs” • Support for visible whitespace (this is crucial when working with Python files) Nice–to–haves: • Support for “rulers”, which show a vertical line after the 79th character, as PEP8 recommends. • Built-in linter for showing syntax errors as you type. • Built-in support for Flake8, which enforces PEP8 standards as you type.

13 howtopython Documentation, Release 0.0.0

4.1 Sublime Text 3

Sublime Text doesn’t support all of these things by itself, but it comes with a great system called Package Control, which allows for easy installation/management of third-party plugins.

4.1.1 Sublime Text 3 Plugin Recommendations

• Package Control Sublime’s Package Manager — can be installed via ** • Anaconda Fantastic “Python IDE” for Sublime Text, which offers code linting, PEP8 support, and auto–completion. Works great with Pipenv (and the subl launcher). Highly recommended.

4.1.2 Additionally Useful Plugins

• GitSavvy Allows you to git push/pull/stage right from Sublime Text. • GitGutter Places the current git status of edited lines in your gutter. Very useful.

14 Chapter 4. Text Editors howtopython Documentation, Release 0.0.0

4.1.3 Sublime Text 3 Tricks subl Launcher

Once installed, the command–line subl launcher will allow you to easily open up projects from your terminal (e.g. $ subl .). In addition, it also will inform plugins like Anaconda of which Python interpreter to use, based on the currently activated virtual environment. To enable this shortcut, if you’re on a Mac, do the following:

$ ln -s "/Applications/Sublime Text 3.app/Contents/SharedSupport/bin/subl" /usr/local/

˓→bin/subl

Then, the subl command will be available to you, always.

PEP8 Rulers

According to PEP8, all lines of Python should typically be set to a maximum length of 79 characters (72 if documen- tation). Let’s configure Sublime Text to display these rulers. Go into Preferences Settings, and add the following JSON:

"rulers":[ 72, 79, ],

Now, you’ll see nice vertical lines at 72 and 79 characters, informing you of how long your lines are!

4.1. Sublime Text 3 15 howtopython Documentation, Release 0.0.0

4.2 Code (VS Code)

Microsoft VS Code guides you through setting up the Python package, built by Microsoft themselves, the first time you run it. VS Code comes built in with git support and a package system that allows you to customize the editor to your liking. It is built upon Electron (just like Github’s Atom editor) and thus works on various platforms.

4.2.1 Microsoft VS Code Extensions Recommendations

• Python Extension for Visual Studio Code by Microsoft Linting, (multi-threaded, remote), Intel- lisense, code formatting, refactoring, unit tests, snippets, Data Science (with Jupyter), PySpark and more. • vscode-flake8 • MagicPython Syntax highlighter for cutting edge Python. • Python Extension Pack Don Jayamanne’s extension pack installs all the useful Python packages for VS Code in one fell swoop.

4.2.2 Additionally Helpful Plugins

• Jupyter Data Science with Jupyter on Visual Studio Code.

16 Chapter 4. Text Editors howtopython Documentation, Release 0.0.0

• Django Template Django template language support for Visual Studio Code. • Django Snippets Common Django snippets for everyday use.

4.2.3 Microsoft VS Code Tricks

Select a version of Python using Ctrl + Shift + P: “Python: Select Interpreter” You can run your code using the integrated terminal. Open it using Ctrl + ‘.

4.2.4 Microsoft VS Code Launcher

Mac: After installing VS Code, running code from the terminal as easy as typing code

4.2.5 Rulers

PEP8 compliant rulers can be added to VS Code by adding the following lines to your User Settings:

"editor.rulers":[72, 79]

4.2. Microsoft Visual Studio Code (VS Code) 17 howtopython Documentation, Release 0.0.0

18 Chapter 4. Text Editors CHAPTER 5

IDEs

Some people prefer working in IDEs (Integrated Development Environment), as opposed to text editors. Some people use both, from time to time, depending on the type of work they are doing. My recommendation for an IDE is PyCharm.

5.1 PyCharm

19 howtopython Documentation, Release 0.0.0

20 Chapter 5. IDEs CHAPTER 6

Indices and tables

• genindex • modindex • search

21 howtopython Documentation, Release 0.0.0

22 Chapter 6. Indices and tables Index

P Python Enhancement Proposals PEP 1, 12 PEP 20, 12 PEP 257, 12 PEP 8, 12

23