Last active
August 12, 2019 14:56
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"path" : "C:\\downloaded\\ptvsd-4.2.9\\src", | |
"attach" : true, | |
"host" : "localhost", | |
"port" : 5678, | |
"wait" : true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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