Skip to content

Commit c4b193f

Browse files
committed
Support for ptvsd version 4 + Other Improvements
- Fixes #8 - Improved python path searching code. - Added documentation regarding usage with virtual environments. - Added link to single file scripts workaround issue. Closes #4
1 parent 054b6bc commit c4b193f

File tree

3 files changed

+75
-78
lines changed

3 files changed

+75
-78
lines changed

ISSUE_TEMPLATE.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
<!-- If filing a bug report please follow the troubleshooting steps in the README. If none of those helped please fill out the following: -->
2-
3-
OS:
4-
5-
Add-on Version:
6-
7-
Blender Version:
8-
9-
VS Code Version:
10-
11-
VS Code Python Extension Version:
12-
13-
<!-- ...or Visual Studio Version: -->
14-
15-
PTVSD Version:
16-
17-
Paths listed by `where python` or the corresponding command:
18-
19-
Does VS Code / Visual Studio connect to the troubleshooting script?
20-
21-
Python path/interpreter detected/selected by VS Code / Visual Studio: <!-- See Connecting VS Code in the README -->
22-
23-
## Notes:
24-
1+
<!-- Before reporting a bug please follow the troubleshooting steps in the README first. -->
2+
<!-- If none of those helped please fill out the following: -->
3+
4+
OS:
5+
6+
Add-on Version:
7+
8+
Blender Version:
9+
10+
VS Code Version:
11+
12+
<!-- ...or Visual Studio Version: -->
13+
14+
VS Code Python Extension Version:
15+
16+
PTVSD Version:
17+
18+
Paths listed by `where python` or the corresponding command:
19+
20+
Does VS Code / Visual Studio connect to the troubleshooting script?
21+
22+
Python path/interpreter detected/selected by VS Code/Visual Studio: <!-- See Connecting VS Code in the README -->
23+
24+
## Notes:
25+
2526
<!-- Describe your problem and the steps to reproduce it, including any specific errors. -->

README.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Blender Debugger for VS Code (and Visual Studio)
1+
# Blender Debugger for VS Code (and Visual Studio)
22

33
Inspired by [Blender-VScode-Debugger](https://github.com/Barbarbarbarian/Blender-VScode-Debugger) which was itself inspired by this [remote_debugger](https://github.com/sybrenstuvel/random-blender-addons/blob/master/remote_debugger.py) for pycharm as explained in this [Blender Developer's Blog post](https://code.blender.org/2015/10/debugging-python-code-with-pycharm/).
44

@@ -17,6 +17,8 @@ Now it can:
1717

1818
I have made a video (click the image below) for those who just started messing with python in Blender or programming in general, but if you're semi-familiar with Python, VS Code, and the command line the following should make sense. If you have any questions or suggestions, don't hesitate to file an issue.
1919

20+
<strong>NOTE: ptvsd doesn't have to be version 3.0.0 anymore, so you can now ignore that and just `pip install ptvsd`</strong>
21+
2022
<p align="center" style="position:relative;">
2123
<a href="https://www.youtube.com/watch?v=UVDf2VSmRvk" title="Click to go to Video">
2224
<img alt="youtube video" src="https://img.youtube.com/vi/UVDf2VSmRvk/maxresdefault.jpg" height="300" style="margin:0 auto;" />
@@ -32,10 +34,11 @@ If you download the add-on from the releases page, please make sure to rename th
3234
## Installing Python and Getting PTVSD
3335

3436
Install Python 3 with pip and check add to PATH.<sup id="n1">[1](#f1)</sup>
35-
- If you already have python installed and you can run it from the command line (aka PATH is set), the addon should find it. It uses `where python` or `whereis python` or `which python` depending on the OS to determine where python is and uses the first path given<sup id="n2">[2](#f2)</sup>.
37+
- If you already have python installed and you can run it from the command line (aka PATH is set), the addon should find it. It checks `where python` or `whereis python` or `which python` to try and determine where python is and uses the first path given<sup id="n2">[2](#f2)</sup>.
38+
- If you are using something like Conda and want to use a virtual environment, to have the addon auto-detect the path you can: activate the environment, run Blender from the command line, and it should work.
3639

37-
`pip install ptvsd==3.0.0`
38-
- Newer versions will not work, the add-on will warn you in the console if the version is above 3.0.0. Later versions aren't supported yet in VS Code, and it will throw an error when trying to connect. See [Debugging Python with VS Code](https://code.visualstudio.com/docs/python/debugging#_remote-debugging) and [#514](https://github.com/Microsoft/vscode-python/issues/514).
40+
`pip install ptvsd`
41+
- The following is no longer the case, the latest ptvsd version should work just fine now. <strike>Newer versions will not work, the add-on will warn you in the console if the version is above 3.0.0. Later versions aren't supported yet in VS Code, and it will throw an error when trying to connect. See [Debugging Python with VS Code](https://code.visualstudio.com/docs/python/debugging#_remote-debugging) and [#514](https://github.com/Microsoft/vscode-python/issues/514).</strike>
3942
- For Visual Studio, later versions should work depending on the Visual Studio version. I have never used Visual Studio, but you can find more info on setting everything up here: [Remotely Debugging Python Code on Linux](https://docs.microsoft.com/en-us/visualstudio/python/debugging-python-code-on-remote-linux-machines#connection-troubleshooting). (it is not Linux specific)
4043

4144
## Setting up your Addon
@@ -68,7 +71,7 @@ Open up Blender's search (default shortcut: space), type "Debug".
6871

6972
Click `Debug: Start Debug Server for VS Code`. Note: you can only start the server once. You cannot stop it, at least from what I understand. If you run it again it'll just tell you it's already running and start the timer again to check for a confirmation.
7073

71-
## Connecting VS Code
74+
## Connecting the Editor
7275

7376
Open your addon folder (e.g. "C:\Code\Blender Stuff\addons\myaddon").
7477

@@ -86,7 +89,6 @@ Go to the Debugging tab and add a configuration. Pick Python. You'll want the co
8689
"localRoot": "${workspaceFolder}",
8790
"remoteRoot": "${workspaceFolder}",
8891
"port": 3000,
89-
"secret": "my_secret",
9092
"host": "localhost"
9193
},
9294
```
@@ -99,7 +101,7 @@ At this point you should be able to add a breakpoint and when you trigger it in
99101

100102
Note though that if you make changes to the file, Blender will not detect them. Have open `User Preferences > Addons` so you can toggle your addon on and off when you make changes. If anyone knows any way to improve this I'd love to know.
101103

102-
### Editing Source Code
104+
### Debugging/Editing Source Code
103105

104106
It is possible to edit the Blender source code but it can be a bit tricky to get it to detect changes (nevermind live editing is buggy anyways).
105107

@@ -108,10 +110,16 @@ From blender you can right click just about anything and click "Edit Source" to
108110
Open the file in VS Code, connect to the debugging server, make a change and save it.
109111

110112
Now in Blender the text editor will show this little red button in the top left. Click that and reload the file. Then in `Text Editor > Properties` turn on `Live Edit` if you haven't already. Now to actually get Blender to detect any changes you made just type a single character (like add a space anywhere) and *then* it will detect your changes.
113+
114+
115+
### Debugging/Editing Scripts
116+
117+
See [Issue #4](https://github.com/AlansCodeLog/blender-debugger-for-vscode/issues/4) for a workaround.
118+
In the future if I have some time, I might see if there's something I can do to make this easier.
111119

112120
# Troubleshooting
113121

114-
- Check you installed the correct ptvsd version.
122+
- Check you installed the correct ptvsd version. With VS Code this should no longer be an issue, but I believe different versions of Visual Studio need different versions of ptvsd (see [Installing Python Support](https://docs.microsoft.com/en-us/visualstudio/python/installing-python-support-in-visual-studio)).
115123
- To determine whether the problem is on Blender's side or your editor's: Close Blender and download/copy this [test script](https://gist.github.com/AlansCodeLog/ff1b246a8e31938e1c3dbfdcbb90522f) and run it with Python, and then try to connect to the server with your editor. If you're still getting problems then the problem is with VS Code, try:
116124
- Check your detected your Python install, or set it manually.
117125
- For VS Code try reinstalling the VS Code Python extension.
@@ -120,6 +128,6 @@ Otherwise, if nothing works, don't hesitate to file an issue.
120128

121129
# Notes
122130

123-
<a id="f1" href="#n1">1.</a> Technically, the add-on will work with Python 2 as well since it doesn't use Python itself, just the ptvsd package, so it doesn't really matter whether you installed it with Python 2 or 3 because the package is compatible with both. On the VS Code side though, the Python extension does need to know where Python is (though not ptvsd), but it still will connect if it's using Python 2, just IntelliSense recommendations the will be wrong.
131+
<a id="f1" href="#n1">1.</a> Technically, the add-on will work with Python 2 as well since it doesn't use Python itself, just the ptvsd package, so it doesn't really matter whether you installed it with Python 2 or 3 because the package is compatible with both. On the VS Code side though, the Python extension does need to know where Python is (though not ptvsd), but it will still connect if it's using Python 2, just IntelliSense recommendations the will be wrong.
124132

125-
<a id="f2" href="#n2">2.</a> The addon also detects python if PYTHONPATH is set (because Blender will add it to sys.path) or if you used the Python bundled with Blender to install ptvsd (but that's a bit of a pain because it doesn't have pip installed unless you want to install it manually).
133+
<a id="f2" href="#n2">2.</a> The addon also detects python if PYTHONPATH is set (because Blender will add it to sys.path) or if you used the Python bundled with Blender to install ptvsd (but that's a bit of a pain because it doesn't have pip installed, you would have to install it manually).

__init__.py

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
bl_info = {
2222
'name': 'Debugger for VS Code',
2323
'author': 'Alan North',
24-
'version': (0, 1, 1),
24+
'version': (0, 2, 0),
2525
'blender': (2, 79, 0),
2626
"description": "Starts debugging server for VS Code.",
2727
'location': 'In search (default shortcut:space) type "Debug"',
@@ -37,46 +37,38 @@
3737
import subprocess
3838
import re
3939

40-
# check path for ptvsd
40+
# finds path to ptvsd if it exists
4141
def check_for_ptvsd():
42-
#check for python with where/whereis
43-
python_exe = None
44-
try:
45-
python_exe = subprocess.Popen(
46-
["whereis", "python"],
47-
shell=False,
48-
stdout=subprocess.PIPE,
49-
stderr=subprocess.PIPE
50-
)
51-
except Exception:
52-
pass
53-
if python_exe is None:
54-
try:
55-
python_exe = subprocess.Popen(
56-
["where", "python"],
57-
shell=False,
58-
stdout=subprocess.PIPE,
59-
stderr=subprocess.PIPE
60-
)
61-
except Exception:
62-
pass
63-
if python_exe is None:
64-
try:
65-
python_exe = subprocess.Popen(
66-
["which", "python"],
67-
shell=False,
68-
stdout=subprocess.PIPE,
69-
stderr=subprocess.PIPE
70-
)
42+
#commands to check
43+
checks = [
44+
["where", "python"],
45+
["whereis", "python"],
46+
["which", "python"],
47+
]
48+
location = None
49+
for command in checks:
50+
try:
51+
location = subprocess.Popen(
52+
command,
53+
shell=False,
54+
stdout=subprocess.PIPE,
55+
stderr=subprocess.PIPE
56+
)
7157
except Exception:
72-
pass
73-
if python_exe is not None:
74-
python_exe = str(python_exe.communicate()[0], "utf-8")
75-
match = re.search(".*(\\\\|/)", python_exe).group()
76-
if os.path.exists(match+"lib\site-packages\ptvsd"):
77-
return match+"lib\site-packages"
78-
79-
#check in our path
58+
continue
59+
if location is not None:
60+
location = str(location.communicate()[0], "utf-8")
61+
#normalize slashes
62+
location = re.sub("\\\\", "/", location)
63+
#extract path up to last slash
64+
match = re.search(".*(/)", location)
65+
if match is not None:
66+
match = match.group()
67+
if os.path.exists(match+"lib/site-packages/ptvsd"):
68+
match = match+"lib/site-packages"
69+
return match
70+
71+
#check in path just in case PYTHONPATH happens to be set
8072
for path in sys.path:
8173
if os.path.exists(path+"\ptvsd"):
8274
return path
@@ -177,13 +169,9 @@ def execute(self, context):
177169
global ptvsd #so we can do check later
178170
import ptvsd
179171

180-
version = re.sub("\.", "", ptvsd.__version__)
181-
if int(version) > 3000:
182-
print("Warning: PTVSD version " + ptvsd.__version__ + " is greater than 3.0.0, it will not work with VS Code.")
183-
184172
# can only be attached once, no way to detach (at least not that I understand?)
185173
try:
186-
ptvsd.enable_attach("my_secret", address = ("0.0.0.0", 3000))
174+
ptvsd.enable_attach(("0.0.0.0", 3000), redirect_output=True)
187175
except:
188176
print("Server already running.")
189177

0 commit comments

Comments
 (0)