vscode setup for python module import to work

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
drmacro
Veteran
Posts: 9009
Joined: Sun Mar 02, 2014 4:35 pm

vscode setup for python module import to work

Post by drmacro »

Edit: this thread was split from here since it really discusses another vscode setup detail:
https://forum.freecadweb.org/viewtopic.php?f=22&t=40673


I am now using vscode to develop python and while I can see variables during debug, when just editing, it gives lots of errors about un-found modules and such.

I'll have a look at the set up described in this project, but the first glance appears the instructions of what to do in vscode are too cursory and assume a great deal of understanding of vscode configuration concepts.

I hesitate to use this as I see the paths are shown as 0.19 and I'm using 0.20.

In addition, I find vscode has SO many setting options (half the time it gets the indent tab/space conversion wrong...and I've scoured the web and messed with env file forever.) And I found myself in settings hell so many times I'm afraid to change things once things are near what works. :lol:
Last edited by drmacro on Wed May 05, 2021 10:49 pm, edited 1 time in total.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
drmacro
Veteran
Posts: 9009
Joined: Sun Mar 02, 2014 4:35 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by drmacro »

I have the following in .env:

Code: Select all

FREECAD_LIB=/home/mac/freecad-build/lib
FREECAD_MOD=/home/mac/freecad-build/Mod
PYTHONPATH=${FREECAD_Mod}:${FREECAD_LIB}:${PYTHONPATH}
And the following in settings.json

Code: Select all

{
    //"python.pythonPath": "/usr/bin/python"
    "python.envFile": "${workspaceFolder}/.env"
}
And all imports in my code have this error:

Code: Select all

Unable to import 'FreeCAD'
Unable to import 'FreeCADGui'
...
Any ideas what I'm doing wrong? :(
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
ABeton
Posts: 150
Joined: Tue Sep 03, 2019 6:39 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by ABeton »

Would it be difficult to make this work with Atom on Windows? Also just a question, whenever I change the code of a macro I have to restart FreeCAD to make it work properly. Is there any way in which I would not have to do it?
edwilliams16
Veteran
Posts: 3192
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by edwilliams16 »

You can reload files with importlib.reload() but it can be tricky:
https://forum.freecadweb.org/viewtopic. ... 24#p405128
ABeton
Posts: 150
Joined: Tue Sep 03, 2019 6:39 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by ABeton »

It is always tricky :lol: I think I will stick with copy/paste for now until I understand things better :lol:
drmacro
Veteran
Posts: 9009
Joined: Sun Mar 02, 2014 4:35 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by drmacro »

the ptvsd connection works fine and I can debug, single step, explore variables, etc.

it's editing code with autocomplete that is the issue, that and always having 64 errors because vscode can't find the FreeCAD modules.

I am running from source, so I don't have anything in /usr/lib/...

I think I knew where the actual module that 'import FreeCAD' lives I could fix this. But I can only find FreeCAD.so, not FreeCAD. py.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Modeler
Posts: 43
Joined: Fri Feb 19, 2021 4:42 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by Modeler »

drmacro wrote: Tue May 04, 2021 12:34 pm I have the following in .env:

Code: Select all

FREECAD_LIB=/home/mac/freecad-build/lib
FREECAD_MOD=/home/mac/freecad-build/Mod
PYTHONPATH=${FREECAD_Mod}:${FREECAD_LIB}:${PYTHONPATH}
And the following in settings.json

Code: Select all

{
    //"python.pythonPath": "/usr/bin/python"
    "python.envFile": "${workspaceFolder}/.env"
}
And all imports in my code have this error:

Code: Select all

Unable to import 'FreeCAD'
Unable to import 'FreeCADGui'
...
Any ideas what I'm doing wrong? :(
So for what it's worth, I am am completely new in FreeCAD development. I am also new to Python too but I have done MVC web development and plenty of C/C++/shell script Unix work and spent years as an SAP developer so I am not brand new and more importantly I have this working both ways in FreeCAD. ( all I have to do is now learn some python and how FreeCAD is coded :( )

This is perhaps a bit long but I personally don't find instructions that presume I can connect undocumented esoteric dots that helpful.
=====

I guess it depends what your trying to do and how your trying to do it. What I demonstrated in the video was how to take an existing body of code, execute that code in FreeCAD and attach to it with the VS Code IDE debugger. The example was simple but this afternoon I was using that same procedure to try and investigate a problem in another work bench. I stuck the 3 lines of code below into one of the files,

Code: Select all

import ptvsd
ptvsd.enable_attach(address=('localhost', 5678))
ptvsd.break_into_debugger()
I restarted FreeCAD and ran the workbench it broke into the file right away and I was able to debug it quite nicely. I did not need the .env file. I checked by removing it. The process still worked.

If you develop in that manner and start the code in FreeCAD then your letting FreeCAD bootstrap the program and find the libraries.

On the .env file...

You want to check if the files and libraries you are looking for are where you think they are. If not add the directories as lib paths. This is not always straight forward. There are different ways to include files and that determines what the path must be. More below.

This is my .env file.

Code: Select all

FREECAD_BIN=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/bin
FREECAD_LIB=C:/Colin/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/lib
FREECAD_MOD=C:/Colin/Downloads/FreeCAD-0.19.1.a88db11/Config/AppData/Roaming/FreeCAD/Mod
FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/
PYTHONPATH=${FREECAD_BIN};${FREECAD_LIB};${FREECAD_MOD};${FREECAD_EXT}
The FreeCAD.pyd file and FreeCADGui.pyd files were in the bin directory not the lib directory.

Within FreeCAD this line works.

Code: Select all

from PySide import QtCore, QtGui
but in VS Code it does not work unless you have

Code: Select all

FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/
QTCore is found in C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/PySide

If you wanted to put in

Code: Select all

import QtCore
Import QtGui
Then you would have to have

Code: Select all

FREECAD_EXT=C:/Downloads/FreeCAD-0.19.1.a88db11/FreeCAD/EXT/PySide
You also need this code in your launch.json

Code: Select all

{
    "configurations": [       
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },        
        {
            "name": "Python: Remote Attach",
            "type": "python",
            "request": "attach",
            "redirectOutput": true,
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "c:/temp"
                }
            ]
        }
    ]          
}
You need to select the debugger profile for the configuration you want to use in the Debug tab on the left hand side.


This will run the debugger using the current file.
DebuggerRunningCurrentFile.png
DebuggerRunningCurrentFile.png (15.08 KiB) Viewed 3983 times

This will run the debugger using remote attach mode
DebuggerRunningInRemoteAttach.png
DebuggerRunningInRemoteAttach.png (13.48 KiB) Viewed 3983 times

Hope this helps. Feel free to PM me if you need more details.
Modeler
Posts: 43
Joined: Fri Feb 19, 2021 4:42 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by Modeler »

drmacro wrote: Tue May 04, 2021 11:33 pm the ptvsd connection works fine and I can debug, single step, explore variables, etc.

it's editing code with autocomplete that is the issue, that and always having 64 errors because vscode can't find the FreeCAD modules.

I am running from source, so I don't have anything in /usr/lib/...

I think I knew where the actual module that 'import FreeCAD' lives I could fix this. But I can only find FreeCAD.so, not FreeCAD. py.
Your FreeCAD.so file is dll file made from a python module on a Linux system. In Windows this is the FreeCAD.pyd file. That is in essence your FreeCAD.py file. In your .env file enter the individual paths in separate lines( just like my previous post) where ever a .so file or a .py file is found. String them all together like:

Code: Select all

PYTHONPATH=${FREECAD_BIN}:${FREECAD_LIB}:${FREECAD_MOD}:${FREECAD_EXT}
Note Linux builds a path separated by ":" while windows uses ";"

Try that.
drmacro
Veteran
Posts: 9009
Joined: Sun Mar 02, 2014 4:35 pm

Re: Please demonstrate using a remote text editor to substitute the Python editor in FreeCAD

Post by drmacro »

Modeler wrote: Wed May 05, 2021 2:40 am ...
From my home directory, find .so files:

Code: Select all

find . -iname '*.so' 2>/dev/null | grep freecad-
./freecad-build/Mod/Draft/DraftVecUtils.py
...
./freecad-build/lib/FreeCAD.so
...
workspace settings.json:

Code: Select all

{
    //"python.pythonPath": "/usr/bin/python"
    "python.envFile": "${workspaceFolder}/.env"
}
workspace .env:

Code: Select all

FREECAD_LIB=/home/mac/freecad-build/lib/
FREECAD_MOD=/home/mac/freecad-build/Mod/
PYTHONPATH=${FREECAD_MOD}:${FREECAD_LIB}:${PYTHONPATH}
(I tried this with and without the trailing '/' )

vscode:

Code: Select all

import FreeCAD
...
import DraftVecUtils
Unable to import 'FreeCAD'
Unable to import 'DraftVecUtils'

:(
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
chrisb
Veteran
Posts: 54303
Joined: Tue Mar 17, 2015 9:14 am

Re: vscode setup for python module import to work

Post by chrisb »

This topic was split on drmacros suggestion from https://forum.freecadweb.org/viewtopic. ... 20#p501571.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
Post Reply