Skip to content

Instantly share code, notes, and snippets.

@robomojo
Last active August 12, 2019 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robomojo/26b6495e075da271f82535dd52b3f43e to your computer and use it in GitHub Desktop.
Save robomojo/26b6495e075da271f82535dd52b3f43e to your computer and use it in GitHub Desktop.
Using VSCode for python development can require specific versions of PTVSD. Dropping this module into the project and using the environment variable can make things easier.
{
"path" : "C:\\downloaded\\ptvsd-4.2.9\\src",
"attach" : true,
"host" : "localhost",
"port" : 5678,
"wait" : true
}
'''
A setup for using ptvsd in any python environment.
Assign the env variable PTVSD_JSON to a json file and
modify to your needs.
Example:
{
"path" : "C:\\downloaded\\ptvsd-4.2.9\\src",
"attach" : true,
"host" : "localhost",
"port" : 5678,
"wait" : true,
"timeout" : 3
}
'''
import sys
import os
import json
class DebugWire(object):
'''
loads from json in PTVSD_JSON
'''
def __init__(self):
'''
setup with values from json or default
'''
self.jsonData = self.LoadJson()
self.path = self.GetD(attr='path', dValue=None)
self.attach = self.GetD(attr='attach', dValue=False)
self.host = self.GetD(attr='host', dValue='localhost')
self.port = self.GetD(attr='port', dValue=5678)
self.wait = self.GetD(attr='wait', dValue=False)
self.timeout = self.GetD(attr='timeout', dValue=0)
def LoadJson(self):
'''
load from disk if exists
'''
jsonpath = os.getenv('PTVSD_JSON')
if os.path.exists(jsonpath):
with open(jsonpath, 'r') as jFile:
return json.load(jFile)
return {}
def GetD(self, attr=None, dValue=None):
'''
get from json data or passed default
'''
if attr in self.jsonData.keys():
return self.jsonData[attr]
else:
return dValue
def Startup(self):
'''
begin ptvsd behavior
'''
if self.path is not None and os.path.exists(self.path):
if self.attach:
if self.path not in sys.path:
sys.path.append(self.path)
import ptvsd
ptvsd.enable_attach(address=(self.host, self.port), redirect_output=True)
if self.wait:
ptvsd.wait_for_attach(timeout=None if self.timeout <= 0 else self.timeout)
# start the instance
DebugWire().Startup()
print ('PTVSD setup complete')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment