Tom Wojcik personal blog

Fix broken PyCharm environment with docker

☕️ 2 min read

Broken Pycharm Helpers

Every now and then my docker setup in PyCharm is broken. It usually happens when I run debugger and immediately stop it because I found an error. It might result in different error depending on the PyCharm or Docker version but the root of the traceback is always the same, it’s pycharm_helpers that PyCharm is using to setup a docker env.

My full traceback

Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pydev/pydevd.py", line 1477, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/opt/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 11, in execfile
    stream = tokenize.open(file)  # @UndefinedVariable
  File "/usr/local/lib/python3.8/tokenize.py", line 392, in open
    buffer = _builtin_open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/fragment.py'

As Google returns no results for that one, I assume it’s not a very popular problem.

What it is

PyCharm is populating a volume using a container. So it first downloads a pycharm_helpers image with a tag corresponding to your PyCharm build number. Then docker volumes are created from this image. After that you can see /opt/.pycharm_helpers when running your containers from PyCharm. So, in my case, I assume only volume is broken. Still, I’d rather recreate everything from scratch.

How to solve it

  1. Close PyCharm.
  2. Stop all running containers.
docker stop $(docker ps -a -q)
  1. Remove PyCharm helpers container running
docker ps -a | grep -i pycharm | awk '{print $1}' | xargs docker rm
  1. Remove PyCharm helpers image running
docker image ls -a | grep -i pycharm | awk '{print $3}' | xargs docker rmi
  1. Try to remove PyCharm volume with helpers
docker volume ls | grep -i pycharm | awk '{print $2}' | xargs docker volume rm

It will probably fail as helpers were being used in your project so there are dependent containers on pycharm helpers. You will have to remove these containers as well. What I see:

Error response from daemon: remove backend_pycharm_helpers_PY-203.6682.86: volume is in use - [816ccfc6795cbd399147c52bb657673f7c97194ca47b5aa061bd1dcfbab091ea, 99459b242988cc4f0e90d609ee9895af85685653b980ff62776317c098083a8a]
  1. So remove them with
docker rm 816ccfc6795cbd399147c52bb657673f7c97194ca47b5aa061bd1dcfbab091ea 99459b242988cc4f0e90d609ee9895af85685653b980ff62776317c098083a8a
  1. Now rerun
docker volume ls | grep -i pycharm | awk '{print $2}' | xargs docker volume rm

After that, you should be good to go.