Are python modules loaded or executed?

This subforum is specifically to discuss packaging issues on different platforms (windows, mac, linux), and using different packaging systems (conda, etc...)
hobbes1069
Posts: 219
Joined: Wed Nov 09, 2011 3:49 pm

Are python modules loaded or executed?

Postby hobbes1069 » Sat Nov 16, 2019 1:22 pm

Looking at the lint output from my last build on Fedora I see this:

Code: Select all

freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/AddonManager/AddonManager.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/AddonManager/AddonManager.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/Part/MakeBottle.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/Part/MakeBottle.py 644 python 
freecad.x86_64: E: wrong-script-end-of-line-encoding /usr/lib64/freecad/Mod/Part/MakeBottle.py
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/DistanceBolt.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/DistanceBolt.py 644 python 
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/Epitrochoid.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/Epitrochoid.py 644 python 
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/FilletArc.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/FilletArc.py 644 python 
freecad.x86_64: E: wrong-script-end-of-line-encoding /usr/lib64/freecad/Mod/PartDesign/Scripts/FilletArc.py
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/Parallelepiped.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/Parallelepiped.py 644 python 
freecad.x86_64: E: wrong-script-end-of-line-encoding /usr/lib64/freecad/Mod/PartDesign/Scripts/Parallelepiped.py
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/RadialCopy.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/RadialCopy.py 644 python 
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/PartDesign/Scripts/Spring.py python 
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/PartDesign/Scripts/Spring.py 644 python 
freecad.x86_64: E: wrong-script-end-of-line-encoding /usr/lib64/freecad/Mod/PartDesign/Scripts/Spring.py
freecad.x86_64: E: incorrect-fsf-address /usr/lib64/freecad/Mod/PartDesign/fcgear/fcgear.py
freecad.x86_64: E: incorrect-fsf-address /usr/lib64/freecad/Mod/PartDesign/fcgear/fcgeardialog.py
freecad.x86_64: E: incorrect-fsf-address /usr/lib64/freecad/Mod/PartDesign/fcgear/svggear.py
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/Robot/MovieTool.py 644 /usr/bin/python 
freecad.x86_64: E: wrong-script-end-of-line-encoding /usr/lib64/freecad/Mod/Robot/MovieTool.py
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/Spreadsheet/importXLSX.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/Spreadsheet/importXLSX.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DHatchTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DHatchTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DProjGroupTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DProjGroupTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DVAnnoSymImageTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DVAnnoSymImageTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DVDimensionTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DVDimensionTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DVPartTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DVPartTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/TechDraw/TDTest/DVSectionTest.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/TechDraw/TDTest/DVSectionTest.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/Test/testmakeWireString.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/Test/testmakeWireString.py 644 /usr/bin/env python
freecad.x86_64: E: wrong-script-interpreter /usr/lib64/freecad/Mod/Test/unittestgui.py /usr/bin/env python
freecad.x86_64: E: non-executable-script /usr/lib64/freecad/Mod/Test/unittestgui.py 644 /usr/bin/env python
So are these files "run" by FreeCAD or "executed" directly? If they're not executed then they don't need a shebang, period. If they are then I'll have to update the files to "#!/usr/bin/python3"

Thanks,
Richard
vocx
Posts: 1850
Joined: Thu Oct 18, 2018 9:18 pm

Re: Are python modules loaded or executed?

Postby vocx » Sat Nov 16, 2019 6:15 pm

hobbes1069 wrote:
Sat Nov 16, 2019 1:22 pm
So are these files "run" by FreeCAD or "executed" directly? If they're not executed then they don't need a shebang, period. If they are then I'll have to update the files to "#!/usr/bin/python3"
...
I'm not sure what you mean "run" by FreeCAD or "executed" directly. Please clarify.

Python files are executed whenever they are first imported. The second time you try to import them, they no longer run because the filename, that is, the name of the module, is already stored in the module cache.

Why is Python running my module when I import it, and how do I stop it? (Stackoverflow)

The shebang is never required to import the files. The shebang is only a convenience string to be able to run a Python file as an executable, using the operating system's terminal.

Therefore, FreeCAD does "execute" the modules, but this is done by importing them.

Code: Select all

# inside FreeCAD
import module
import Draft
import Arch
import Part
It doesn't do something like this

Code: Select all

freecad Draft.py
freecad Arch.py
bash mymodule.py
hobbes1069
Posts: 219
Joined: Wed Nov 09, 2011 3:49 pm

Re: Are python modules loaded or executed?

Postby hobbes1069 » Sat Nov 16, 2019 6:36 pm

Yes, I should have chosen different nomenclature...

What I mean is are the script run directly (like you would in a shell) or just imported. My guess was imported since they are installed without the executable bit set. That means that the shebangs on the first line of the scripts are useless and should be removed.

Thanks,
Richard
vocx
Posts: 1850
Joined: Thu Oct 18, 2018 9:18 pm

Re: Are python modules loaded or executed?

Postby vocx » Sat Nov 16, 2019 9:53 pm

hobbes1069 wrote:
Sat Nov 16, 2019 6:36 pm
...That means that the shebangs on the first line of the scripts are useless and should be removed.
I think in general, you are right.

However, perhaps more files could include an example showcasing the module itself. This was done with ArchFence.py.

At the end of it, it has an example that is guarded the usual way

Code: Select all

if __name__ == '__main__':
    # For testing purposes. When someone runs the File as a macro a default fence will be generated
    ...

    section = buildSection()
    path = buildPath()
    post = buildPost()
    colorizeFaces(post)

    print(makeFence(section, post, path))
These types of modules perhaps could be run as individual programs, and thus would require a shebang. But that would be on a case by case basis. In general, they don't need the shebang.
hobbes1069
Posts: 219
Joined: Wed Nov 09, 2011 3:49 pm

Re: Are python modules loaded or executed?

Postby hobbes1069 » Sat Nov 16, 2019 9:56 pm

Ok, in that case I have the ability to ignore the lint output using a configuration file, probably the best option in this instance.

Thanks,
Richard