“Visually Debugging” is a theme that we plan to touch on repeatedly during 2017. For example, we have tutorial proposals for DjangoCon and EuroPython that expand on the topics in this webinar, conducted in a hands-on, 3-hour format.
About Visual Debugging
PyCharm puts a visual face on debugging and we’d like to help more developers take the plunge and debug. In this webinar, Paul Everitt, PyCharm Developer Advocate, introduced the debugger and went through many of the essentials in the context of writing a 2d game.
This webinar was aimed at developers who have never used the debugger, but even those familiar with PyCharm’s visual debugging will learn some new tricks and see some deeper usages.
1. (3:05) Debugging without PyCharm’s debugger: print and pdb
2. (6:18) First use of the debugger (and the Cython speedups)
3. (12:18) Interactive use
4. (19:47) Breakpoints
5. (29:16) Stepping
6. (40:43) Watch expressions
7. (44:11) Stack Frames
8. (48:17) Debugging during testing
9. (50:22) Attaching to processes
10. (52:11) Extracting type information
Keep up with the latest PyCharm news on our blog and follow us on Twitter @PyCharm.
This tutorial continues where the previous Docker-Compose tutorial left off. If you haven’t read it yet, read about getting Docker-Compose and Flask running in PyCharm first. Like the first tutorial, this one was made on macOS, and although Linux should be similar there may be small differences. Unfortunately, Docker Compose currently isn’t supported on PyCharm for Windows.
We’ll continue by making a Flask version of the hottest web app from the 90s, a guest book:
If you’d like to play along at home, or just have a look at the full code. You can find the code on GitHub. The code in the ‘master’ branch is for the previous tutorial, switch to the ‘with-database’ branch for the code for this tutorial.
Adding the Database
The first thing we need to do is to tell Docker that we would like to have a database as well. So let’s add a postgres service to our compose file. We do this by adding a service, we’ll use the postgres image from Docker hub, and configure it using environment variables. Read the postgres image’s description on Docker Hub for more information on its configuration:
Database service in docker-compose.yml
We’ll also link the database from our web service, so we’re sure that the database is started whenever we start the web service.
Setting Up the Database
The next step is to connect PyCharm to the database. We exposed port 5432 in the compose config to ensure that we can connect to it from our host machine.
The first step is to build our containers, let’s open the terminal in PyCharm (press Alt+F12), and run docker-compose up --build.
After Docker Compose has started the containers, we can connect to it in the databases panel. Open it in View | Tool Windows | Database. Then, add a database connection to a PostgreSQL database:
Now we can configure the connection using the credentials we used in our compose file:
Keep in mind that by default the postgres image will name the database after your username.
If you’re having connection trouble: read the output of the docker-compose up command in the terminal to see if any issues occurred. And if everything looks right, also use lsof -i tcp:5432 and verify whether docker is listening on port 5432.
Now that we’ve got a connection, let’s add a table for our guest book. To make our app fit in slightly better in this century, we’ll add a 140 character limit to the posts. Our table definition should look like this:
id – int – primary key – serial (auto increment)
author – varchar(50)
comment_text – varchar(140)
posted_at – timestamp
Let’s use PyCharm’s database integration to create this table:
After creating the table, we can copy the DDL for the table into an SQL file by using the generate and copy DDL option from the context menu:
After we’ve copied it, create a new file ‘schema.sql‘, and paste it there (Cmd+V). We will just leave it there, if you’d like to learn more about executing these files using Flask, see the Flask documentation.
Writing the Application
Let’s create a simple page where the user can see recent posts, and post something new. We’ll need to connect to the database, so firstly we should add psycopg2 to the requirements file. You could of course use an ORM, but I actually like SQL, so I’ll just use psycopg2 directly. Let’s also add ‘humanize’ to the requirements so we can display the time in a nicer way.
After we’ve added the requirements, we need to rebuild the containers: the requirements are installed in one of the early steps in our Dockerfile. Let’s open the terminal (Alt+F12 within PyCharm), and run docker-compose build.
If we want to make PyCharm aware of the new packages in the Docker container, we need to reload the interpreter paths: go to Settings | Project | Project Interpreter, then use the ‘…’ button next to the selected interpreter and click ‘More’:
Then, at the bottom of the interpreter overview, click the interpreter path button:
On the paths window, click ‘Reload paths’. It will look like nothing happened, but the paths will be reloaded after you’ve closed the settings windows:
And then click ‘Close’ on all of the windows you’ve just opened, PyCharm will now index the new packages.
Let’s rename the hello_world route to show_guestbook, and add some code:
# Let's show all posts from the last week, with a maximum of
# 100 posts
author, comment_text, posted_at
posted_at >= now() - interval '1 week'
Keep in mind that when you’re writing SQL in PyCharm, you can get code completion by pressing Alt+Enter, and then choosing “Inject language or reference”, and then selecting “PostgreSQL”. Similarly, we’ll add an add_post route to add a post to the guestbook:
PyCharm puts a visual face on debugging and we’d like to help more developers take the plunge and debug. In this webinar, Paul Everitt, PyCharm Developer Advocate, introduces the debugger and goes through many of the essentials in the context of writing a 2d game:
– Debugging without PyCharm’s debugger: print and pdb
– First use of the debugger (and the Cython speedups)
– Using the console/IPython at a breakpoint
– Exception and Conditional breakpoints
– All flavors of stepping through code, including filters
– Moving through stack frames
– Setting watches
– Django/Jinja2 template debugging
– Attaching to processes
– Debugging during testing
– Viewing numpy/pandas data frames
– Extracting type information
This webinar is aimed at developers who have never used the debugger, but even those familiar with PyCharm’s visual debugging will learn some new tricks and see some deeper usages.
Paul Everitt is the PyCharm Developer Advocate at JetBrains. Before that, Paul was a co-founder of Agendaless Consulting and a co-founder of Zope Corporation, taking the first open source application server through $14M of funding. Paul has bootstrapped both the Python Software Foundation and the Plone Foundation. Prior to that, Paul was an officer in the US Navy, starting in Python and launching www.navy.mil in 1993.
Back in 2016, we partnered with Stepik to announce a brand-new Python course. It adapted to each student’s level of knowledge and interest and helped them stay more motivated and productive while learning Python. This course was the first step towards adopting Adaptive Learning, which we believe is one of the key trends in the future online education.
After an adoption period, we’ve fixed all the issues in the latest PyCharm Edu 3.5.1. The Adaptive Python course is now polished and ready to use! Let’s explore it in more detail.
The Adaptive Python Course
To start the course, go to File -> New Project -> Educational and choose Adaptive Python from the list:
A couple of issues were resolved in PyCharm 2017.1, and Docker for Mac should now work out of the box. In this blog post we’ll show you how to set up a project with Docker Compose on a Mac. If you’re on Linux, the process is very similar. Unfortunately, we don’t support Docker Compose on Windows at this time. If you’re using Docker Compose on Windows, please let us know in the comments.
In this tutorial we’ll show how to create a very simple ‘Hello World’ Flask application, and then how to run it within a Docker container. For the full code, and to follow along, see GitHub: https://github.com/ErnstHaagsman/flask-compose.
First things first
Before we get started, let’s check a couple of things: make sure you are using PyCharm 2017.1 Professional Edition or later. Docker support is not available in PyCharm Community Edition. Then, please ensure your Docker and Docker Compose are up to date. To check, open a terminal, and run docker -v, and docker-compose -v:
Then, although it is the default setting in PyCharm 2017.1, it never hurts to check that your Docker API URL (Preferences | Build, Execution, Deployment | Docker) is set to unix:///var/run/docker.sock.
Now we can create a new Flask project. Let’s create a virtualenv so that PyCharm can stub out our Flask project before we have Docker configured. You can create a Virtualenv by using the ‘…’ button next to the interpreter dropdown.
After you click create, you should see the standard “Hello World” Flask template. So let’s see if we can get Flask to show us “Hello World” from a Docker container. To do this, we’ll add four files:
requirements.txt, just put “Flask==0.12” here to install Flask
Dockerfile, where we will set up the Python environment for the Flask app
docker-compose.yml to set up how to run the Dockerfile, and add a database
docker-compose.dev.yml to make some changes for local development
In the Dockerfile, we’ll use the ‘python:3’ image, expose port 5000, install packages from requirements.txt, and afterwards copy the rest of our project into /app. See the full file on GitHub.
Then, we’ll create a Compose file where we define the Flask app as our only service (for now). Just use build: . to make Docker Compose build the container from the Dockerfile.
Docker will bake our code into the image, and that way the image is self-contained. If we wanted to, we could stop here and rebuild our image whenever we change the code. It makes development quite a bit faster to mount our code with a volume. So we’ll create an additional Compose file to do that while we’re developing.
In docker-compose.dev.yml, all we’re doing is adding a volume mapping for .:/app for the web service. This overlays the code in the container with a volume, making sure that code changes are applied immediately. Keep in mind that you will need to rebuild the image before pushing it anywhere.
Now that everything is configured, let’s quickly run docker-compose up in the Terminal to make sure that works. Open the terminal with Alt+F12 (in PyCharm) and run docker-compose up.
As we can see, Docker built our container, and Flask is running! Let’s go and check out our ‘Hello World’ message!
A trap for young players!
What happened here? Didn’t docker say that Flask was running?
For security reasons, many modern web frameworks actually limit incoming connections to only come from localhost. This means that our Flask app is running, but only accessible from within the Docker container, not from our Mac host. The easiest way to fix this is to add host='0.0.0.0' as a keyword argument to app.run() in flask-compose.py:
This change tells Flask to listen not only to requests from localhost (in this case the docker container), but on all network connections. In the case of our container this means we can access it from our host, and depending on configuration from other computers in our network.
If we now stop our container (Ctrl+C in the PyCharm Terminal), and rebuild it by running docker-compose up --build. We can see in the Docker Compose output that Flask is now listening on 0.0.0.0 instead of 127.0.0.1:
And reloading the page in Safari actually shows ‘Hello World’ now. So let’s stop the container (Ctrl+C in the Terminal), and then configure PyCharm.
To make our project run using a PyCharm run configuration, we need to set the project interpreter to the Docker Compose service. We can do this from the Project interpreter page in preferences: Preferences | Project: <Project Name> | Project Interpreter.
Click the little button next to the interpreter dropdown (the white one, not the blue one), and choose “Add Remote”. If you don’t see “Add Remote” here, you may be using PyCharm Community Edition, which doesn’t support remote interpreters.
Now choose “docker-compose”, and almost everything will be pre-configured, I only had to add the docker-compose.dev.yml configuration to the list. We need to add it manually as this isn’t part of a standard docker project. You can do this with the ‘+’ button underneath the list of configuration files.
Now, we should set up the path mapping. We are inserting all our project code into the container’s “/app” directory. So let’s add this on the Project Interpreter screen, use the ‘…’ button next to the Path mappings field to change them.
After you close the preferences window, you can just use the regular Run and Debug buttons to start and debug your project:
To celebrate, let’s change “Hello World!” to “Hello from Docker within PyCharm!”
The Python debugger got forty times faster for Python 3.6 projects, and up to two times faster for older versions of Python
We’ve added support for the six compatibility library
Unit test runners for Python have been rebuilt from the ground up: you can now run any test configuration with PyCharm
Zero-latency typing is now on by default: typing latencies for PyCharm 2017.1 are lower than those for Sublime Text and Emacs
Support for native Docker for Mac – no more need to use SOCAT! (only available in PyCharm Professional edition)