Error running FreeCAD script using python2 on Ubuntu

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Error running FreeCAD script using python2 on Ubuntu

Post by umardastgir »

Hello everyone,

I am having problem understanding how FreeCAD runs using Python. So I have FreeCAD installed on a remote Linux Machine (Runs Ubuntu 18.04). I installed freecad using the following two commands:

Code: Select all

sudo add-apt-repository ppa:freecad-maintainers/freecad-stable
sudo apt-get install freecad
After installing I tried running a small script I wrote:

Code: Select all

import sys
PATHTOFREECADBINFOLDER = "/usr/lib/freecad/lib/"
sys.path.insert(0, PATHTOFREECADBINFOLDER)
import FreeCAD
sys.exit()
I tried running this command using Python 2 (sudo python script.py)
However, I always get the following error:

Code: Select all

import FreeCAD
ImportError: dynamic module does not define init function (initFreeCAD)
I am not sure whats going on as I have freecad installed (directory: /usr/lib/freecad) and it has all the required folders.

Please note that I have tried running this using Python3 and it runs fine! The /usr/lib/ also contains freecad-python3, and if I change the PATHTOFREECADFOLDER to point to this, it run (after creating some symbolic links, as explained in the last comment in the link below)

Code: Select all

https://forum.freecadweb.org/viewtopic.php?t=33684
Can someone please guide me on how to get FreeCAD up and running using python 2?
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Error running FreeCAD script using python2 on Ubuntu

Post by kkremitzki »

First off, you generally should not run sudo python or sudo pip, and in this particular case you definitely don't need to do so.

Secondly, with the version you're running, /usr/lib/freecad/lib is a symlink managed by sudo update-alternatives --config freecad, which points to either /usr/lib/freecad-python2/lib or -python3/lib. Try ls -l /etc/alternatives/freecadlib--I'm guessing it's pointed at Python 3, which would explain it working one way and not the other. If that's the case, run the update-alternatives command and pick the other version.

Edit: Also, don't forget to clean up whatever symlinks you created...

Edit2: You also don't need the sys.exit() call in the end, your script will just end and exit normally. The situation where you'd need to use that would be something like:

Code: Select all

if early_exit_condition:
  sys.exit()
 # proceed with normal execution
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Error running FreeCAD script using python2 on Ubuntu

Post by umardastgir »

Thanks for your reply.

I tried running the command sudo update-alternatives --config freecad. However, I get the following response:

Code: Select all

There is only one alternative in link group freecad (providing /usr/bin/freecad): /usr/lib/freecad/bin/freecad-python3
Nothing to configure.
How can I change it to point to python 2?
User avatar
NormandC
Veteran
Posts: 18589
Joined: Sat Feb 06, 2010 9:52 pm
Location: Québec, Canada

Re: Error running FreeCAD script using python2 on Ubuntu

Post by NormandC »

Code: Select all

sudo apt install freecad-python2
Then use the update-alternatives command again.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Error running FreeCAD script using python2 on Ubuntu

Post by umardastgir »

Thank for the response NormandC. I have installed freecad using apt-get install freecad-python2 and now I have another folder in the /usr/lib directory (freecad-python2). I ran the update-alternatives --config freecad command and chose Python 2 . Now, when I execute the script, I get:

Code: Select all

No modules found in /usr/lib/freecad-python2/Mod
During initialization the error No module named freecad occurred
In the freecad-python2 directory, I only have the lib directory. I also have /usr/lib/freecad-python2 and /usr/lib/freecad directories and /usr/lib/freecad-python3 also contains only lib sub-directory. However, in the /usr/lib/freecad I have other directories (Lib Mod Gui and Ext). Do I need to make symlinks to proceed and solve this error?
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Error running FreeCAD script using python2 on Ubuntu

Post by kkremitzki »

Is PATHTOFREECADBINFOLDER still "/usr/lib/freecad/lib/"?
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Error running FreeCAD script using python2 on Ubuntu

Post by umardastgir »

No. I modified it to PATHTOFREECADBINFOLDER = "/usr/lib/freecad-python2/lib/" but one strange thing I noticed is that it was giving me the same error for both paths (PATHTOFREECADBINFOLDER = "/usr/lib/freecad-python2/lib/ and PATHTOFREECADBINFOLDER = "/usr/lib/freecad/lib/). Anyways, I created symlinks of the /usr/lib/freecad-python2/Ext/ and /usr/lib/freecad-python2/Mod/ directories to /usr/lib/freecad/Ext/ and /usr/lib/freecad/Mod/ and now everything seems to be working perfectly.
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Error running FreeCAD script using python2 on Ubuntu

Post by kkremitzki »

It may be working but you've manually modified a system location which is going to cause you problems down the road. Can you try removing the symlinks and using the original value for that variable, /usr/lib/freecad/lib?
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
umardastgir
Posts: 32
Joined: Mon Apr 22, 2019 9:29 pm

Re: Error running FreeCAD script using python2 on Ubuntu

Post by umardastgir »

I removed the symlinks and changed the variable name to /usr/lib/freecad/lib and now I get the following error:

Code: Select all

No modules found in /usr/lib/freecad-python2/Mod
During initialization the error No module named freecad occurred
Traceback (most recent call last):
  File "3DFileFormatConversion.py", line 127, in <module>
    import importDAE
ImportError: No module named importDAE
User avatar
kkremitzki
Veteran
Posts: 2515
Joined: Thu Mar 03, 2016 9:52 pm
Location: Illinois

Re: Error running FreeCAD script using python2 on Ubuntu

Post by kkremitzki »

Can you paste your full script and full traceback/command output from running it? The error messages are not making sense with regards to what you've shared on the 1st post.
Like my FreeCAD work? I'd appreciate any level of support via Patreon, Liberapay, or PayPal! Read more about what I do at my blog.
Post Reply