A running Docker container with:
- A volume mounted to your source code (henceforth,
/code
) - SSH setup
- SSH enabled for the
root:password
creds and the root user allowed to login
Refer to docker-compose.yaml
and Dockerfile.dev
on how to achieve this.
- Preferences (CMD + ,) > Project Settings > Project Interpreter
- Click on the gear icon next to the "Project Interpreter" dropdown > Add
- Select "SSH Interpreter" > Host: localhost, Port: 9922, Username: root > Password: password > Interpreter: /usr/local/bin/python, Sync folders: Project Root -> /code, Disable "Automatically upload..."
- Confirm the changes and wait for PyCharm to update the indexes
- Install the Python extension
- Install the Remote - Containers extension
- Open the Command Pallette and type
Remote-Containers
, then select theAttach to Running Container...
and selecet the running docker container - VS Code will restart and reload
- On the
Explorer
sidebar, click theopen a folder
button and then enter/code
(this will be loaded from the remote container) - On the
Extensions
sidebar, select thePython
extension and install it on the container - When prompet on which interppreter to use, select
/usr/local/bin/python
- Open the Command Pallette and type
Python: Configure Tests
, then select theunittest
framework
- Code completion works
- Code navigation works
- Organize imports works
- Import suggestions/discovery works
- (VS Code) Tests (either classes or methods) will have a new line above their definitions, containing two actions:
Run Test | Debug Test
, and will be executed upon clicking on them - (PyCharm) Tests (either classes or methods) can be executed by placing the cursor on them and then using
Ctrl+Shift+R
There's a pretty big problem with this if you're using VScode with WSL2. Once it's attached to the docker container, any new files created are created as
root
user and make git go haywire when trying to commit unless you mod the files beforehand withchown
and reset the owner to be the same as all the rest, and notroot
user.There are some workarounds to this, but they're clunky.