Creating a Development Environment on Windows 10 using WSL and Visual Studio Code

Further to my recent trip "down the rabbit hole" using VS Code as an editor for Autodesk's Fusion 360 Python Add In's (Autodesk is really "picky" about the version of MS Python it needs) and having "broken" the Fusion integration a couple of times trying to set up an environment for OctoPrint development I decided another approach was needed.

Previous experience building a GitHub hosted website using Jekyll for TV Rename and a number of years as a Linux admin pushed me in the direction of WSL.

Here are the steps I took to build a usable WSL environment for both Python2 and Python3 on Windows 10. I thought I'd post them here as an "aide memoire" which may be of use to others...

Install Visual Studio Code (an existing installation will do).

Install Linux from the Microsoft Store (I used an existing installation of Ubuntu 18.04 LTS, but others would probably work), launch it and create a username and password.

Run:

sudo apt update;sudo apt dist-upgrade
sudo apt install build-essential
sudo apt install python
sudo apt install virtualenv
sudo apt install python3-pip
sudo apt install python-pip

This gave me the following:

python --version
	Python 2.7.17
python3 --version
	Python 3.6.9
virtualenv --version
	15.1.0
pip3 --version
	pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
pip --version
	pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)

Create a development directory in your home directory:

 cd ~; mkdir devel; cd devel

Clone Octoprint:

git clone -b devel https://github.com/foosel/OctoPrint.git

cd OctoPrint

Create the Python 3 virtual environment:

virtualenv --python=python3 venv3
source venv3/bin/activate
pip install --upgrade pip
pip install -e .[develop,plugins]
deactivate

Create the Python 2 virtual environment:

virtualenv --python=python2 venv2
source venv2/bin/activate
pip install --upgrade pip
pip install -e .[develop,plugins]
deactivate

Activate whichever environment you wish with either:-

source venv3/bin/activate

or:

source venv2/bin/activate

and then:

octoprint serve

Point a windows based browser (on the same machine!) at "http://localhost:5000" and Voila!

To switch environments:

deactivate

Activate the other environment and reload the server.

To add the Virtual Printer edit "~/.octoprint/config.yaml" and add or expand the "devel" section with:

devel:
    virtualPrinter:
        enabled: true

and reload the server.

To edit with Visual Studio Code cd to "~/devel/OctoPrint" and type "code". Visual Studio Code will open and load the WSL Extension and from there you can play to your hearts content.

CODA: After getting fed up with killing octoprint and deactivating the virtual environment (discussion elsewhere) to change python releases I came up with this .bash_aliases file: -

#
# Aliases for OctoPrint
#
# "octo2" and "octo3" are a really "messy" way of creating an octoprint  2 or octoprint 3 server instance in a new "window" that
# can be treated as a background session for the duration until the alias "octokill" is executed, which will kill both the server
# and the window.
#
# The original "bash" instance can be used to "fiddle", launch VS Code as a "lite" IDE etc...
#
# if launching VS Code cd to the devel/OctoPrint directory and then run "code ." the "." is important it tells the VS Code
# instance to treat the current directory as a workspace.
#
alias octo2='cmd.exe /c start 1>/dev/null 2>&1 ubuntu1804 run '\''cd ~/devel/OctoPrint;source venv2/bin/activate;octoprint serve;deactivate'\'''
alias octo3='cmd.exe /c start 1>/dev/null 2>&1 ubuntu1804 run '\''cd ~/devel/OctoPrint;source venv3/bin/activate;octoprint serve;deactivate'\'''
alias octokill='pkill octoprint'
1 Like

Hi @andy-b,

how do you setup the launch-configuration if you want to start/stop and debug your python-plugin?

From what I see:

  • stop (either): octokill
  • start the Py2 version: octo2
  • alternately, start the Pi3 version: octo3
  • logging: tail ~/.octoprint/logs/octoprint.log

I'm not sure if there are any debugging features like watch that's available but others can weigh in.

Good question, unfortunately I can't go any further with this at the moment (though I'm pretty sure you can do it within VS Code). I've just trashed my laptop with WSL 2 :frowning_face:

Sweet. Now you can put Ubuntu on it. ha

Actually I can't, I have a load of my late wife's design stuff in Photoshop, CorelDRAW and QCADPro on our NAS devices that I want/need to keep access to...

True, true...

I did install Ubuntu on my HP laptop, then installed a VM and installed Windows 10 to that. It has less RAM in that session as it did before but I still have access to that on the rare occasions when I need it.

Chiming in here as a development "noob"... but maybe this is better for a new topic here.

I have been interested in getting started with a bit of dabbling with octoprint and specifically plugins - mainly just working with or contributing to existing plugins. (There was a bug in a particular plugin that I found last week and it seemed like a simple fix).

So I am wondering if there are any examples or walkthroughs of how to set up either VS Code or Pycharm (Pro) to be able to work with plugins AND be able to debug them. I found this reference last week and was able to stumble my way through some changes to said plugin (NOT Octolapse) by using the section titled "Running on a Raspberry Pi", with adjustments to the scripts in that section to suit the plugin that I was working on (using PyCharm Pro).

I did this by just using the script to upload the plugin to a remote Pi running a test octoprint server. The problem with this method was that I couldn't actively debug the changes I was making. It was more of a "try this - see if it works by looking at the web interface" approach. I ended up having a mismatched parenthesis in my code that was screwing things up and I didn't catch it for quite a while. Had I been able to see some sort of debug info I might have realized my error much quicker.

So this is where my question lies - I am a bit more familiar with PyCharm, but only because I tinkered with it over the weekend as I worked on this. The thing that PyCharm seemed to lack was the ability to work completely remotely - having the code ALL on the pi and debugging across the SSH connection. In doing some research, it seems that VS Code has more capability in this regard, so I was trying to replicate what I was doing on PyCharm with VS Code, but I am not sure how to replicate the same functionality that PyCharm has for running bash scripts (with its Bash plugin), and even just setting up Run configurations seems like something I can't figure out how to do with VS Code.

I can see how one could "run" octoprint from perhaps the same "project" in either VS Code or PyCharm, but how does one actually debug the PLUGIN? It doesn't seem like you can "run" it like you run (or start) Octoprint, so I am stuck not knowing how to move forward.

I actually managed to submit my first pull request on github this weekend using the "trial and error" approach above, but I hope there is a better way for future endeavors!

Does anyone have any suggestions? I can do this with EITHER windows or linux (I have Ubuntu 20.04 on my laptop in a Dual Boot config). I also sort of regret installing 20.04 since it seems to be lacking some of the Python2 functionality - specifically I can't get the python2 version of virtualenv working correctly.

Just hoping someone out there might be able to share their workflow in a way that a total noob can understand.

Well, in continuing to stumble my way through this little endeavor, I managed to get a remote octoprint server running with VS Code by experimenting with the debugger and run configurations.

So I have the octoprint code running on my Raspberry Pi, but the actual plugin functionality is still a mystery. I installed the plugin (via the web interface) that I was working on (the merged pull request was released today), and I see that it is running. I still don't understand how (or if) you could debug the plugin specifically - ie watch variables, etc.