Embedding FreeCAD in python script - python

I am trying to run FreeCAD modules from a python script. Here is the code I have written to import the FreeCAD modules according with this tutorial" (eventhoug I don't have any FreeCAD.dll installed on my HD):
FREECADPATH = "C:\\Program Files (x86)\\FreeCAD 0.16\\bin"
import sys
sys.path.append(FREECADPATH)
import FreeCAD
import Part
import Mesh
...then the FreeCAD commands
But i got the following error:
" import Part
ImportError: No module named Part"
I also tried with a 64bit version of the FreeCAD
C:\Program Files\FreeCAD 0.16\bin
Any suggestion?

Related

cannot import name '_imaging' from 'PIL' error

I am using Pillow-6.1.0 for my python project and I am trying to import the ImageGrab() function inside it however I keep getting this following error:
cannot import name '_imaging' from 'PIL'
I am currently using Python 3.7 and editing the code on Visual Studios (Unsure if the IDE may play part in the error as I do not have much experience on it)
I am also running Windows so some of the solutions I have seen which require Linux does not work for me.
What I have tried:
Uninstalling and reinstalling Pillow
Deleting everything with "PIL" in the Python37-32/Lib/site-packages folder and uninstalling Pillow and reinstalling
Moving the Pillow module directly into my project folder
Moving ImageGrab.py directly into my project folder
Updated my Pillow module
All my reinstallations of Pillow is via the pip command
Other possibly relevant info:
ImageGrab() is not the only module that I cant import, I also cant import the Image module
I can import the PIL module successfully but I cannot access the modules within it via the command "from PIL import some_PIL_module" in my IDE
I only get the "cannot import name '_imaging' from 'PIL'" error when I am running my code through the Visual Studios IDE, if I type the command "from PIL import ImageGrab" directly into the python terminal it runs successfully with no errors
This is the line that gives me the error in my code
from PIL import ImageGrab;
I expected it to import with no problems but instead, I get the following error:
cannot import name '_imaging' from 'PIL'

importing an external c module to python

I'm trying to run a python code (test.py) which uses an external module written in C (swapped). Here is the directory structure,
test.py
setup.py
swapped.so
ext_src/
c_swapped.c
c_swapped.h
swapped.c
In order to use the swapped module, I first run setup.py, which is configured as following:
from distutils.core import setup
from distutils.extension import Extension
from distutils.command import build_ext
import numpy as np
import sys
sys.argv[1:] = ['build_ext', '--inplace']
ext_modules = [Extension(
name="swapped",
sources=["ext_src/swapped.c", "ext_src/c_swapped.c"],
language="c", include_dirs=[np.get_include()])
]
setup(
name = 'RLScore',
ext_modules = ext_modules,
)
And in test.py, swapped is imported as:
import swapped
So when I run these on python 2.7.6, test.py works just fine, but on python 3.4.5, I get the following error:
import swapped
File "swapped.pyx", line 2, in init rlscore.utilities.swapped (ext_src/swapped.c:3533)
System error parent module '' not loaded cannot perform relative import
I wish to run this code on python 3.4.5, so is there a way that I can make swapped module working?
Thanks a lot!
Edit: I installed virtual environment as python=2.7, so it works now. But still, if there is a way to run the package without using virtualenv, I'd like to learn.

Error "No such file or directory" when i import a ".so" file and that file is available in python

I have a python code and some nao files(naoqi.py,_inaoqi.so, ...) in a folder in a raspberry pi 3 model B v1.2 with armv7l. my code has some import line:
import sys
from naoqi import ALProxy
import time
import almath
import os
import socket
when i run this code i see "cannot open shared object file: No such file or directory" error from second line :
from naoqi import ALProxy
and in below line in naoqi.py (in line import _inaoqi):
try:
import _inaoqi
except ImportError:
# quick hack to keep inaoqi.py happy
if sys.platform.startswith("win"):
print "Could not find _inaoqi, trying with _inaoqi_d"
import _inaoqi_d as _inaoqi
else:
raise
this file is available, but i see "cannot open shared object file: No such file or directory" error.
Why such an error occurs.
What can i do?
Just dumping the inaoqi files into your program directory isn't sufficient, you have to package them properly as a "python module." Is there an installer available for an inaoqi package, or can it be installed using pip?
Also, if you're running Python on Windows, the .so file isn't going to do you any good. The C or C++ code for the module on Windows will be in a .dll file, so again, check to see if an installer for the module is available for your platform.

Jython ImportError: No module named * (only on Windows Server 2012)

I have a CreateDomain.py script for weblogic. And now I want to import a java class in the python script. I exported the class com.swisslog.python.util.FileNameUtil to a jar and added it to the class path before calling the python script.
Here's the start of the script:
from wlstModule import *
from java.io import FileInputStream
from java.util import Properties
import jarray.array
import os
import shutil
import traceback
import sys
import getopt
from com.swisslog.python.util import FileNameUtil
I start the script from a bat file:
set CLASSPATH=%CLASSPATH%;wm6-python-util.jar
%MW_HOME%\wlserver\common\bin\wlst.cmd common\CreateDomain.py %1 %2 %3 %4
This works on Windows 7. But on Windows Server 2012 I get following error:
ImportError: no module named swisslog
Also using the full path doesn't work (jython ImportError: No module named)
Any help would be appreciated.
I found a stupid workaround. I added a main method which calls the real methods and prints the result with system.out.print. Then I can start the jar from the python script without an import:
result = os.popen(['java', '-jar', 'python-util.jar', 'arg').read()

cv loading for executable

I had imported cv2 library in my code and made an executable by using py2exe through GUI2EXE software.
Py2exe has created cv2.pyc file which is run every time the application is started and creates an error of 'Import Error: DLL load failed.Cannot find the module specified' at line mod = imp.load_dynamic(name,path). It has also made other files like numpy.core_sort which give the same error when run.
My question is that py2exe is only running the cv2.pyc file while running the application. Why is it doing so? It should have just imported the cv2 library.
The code in the cv2.pyc file is given below
def __load():
import imp, os, sys
try:
dirname = os.path.dirname(__loader__.archive)
except NameError:
dirname = sys.prefix
path = os.path.join(dirname, 'cv2.pyd')
#print "py2exe extension module", __name__, "->", path
mod = imp.load_dynamic(__name__, path)
## mod.frozen = 1
__load()
del __load
My code calls libraries in this manner :-
import wx
import wx.lib.buttons
import numpy as np
import os
import cv2.cv as cv #Import functions from OpenCV
from numpy import *
from PIL import Image #python imaging library
Looking at cv2.cpp it has #include "numpy/ndarrayobject.h"
so it looks like it loads numpy, which is not surprising.
As py2exe does not see the c++ source code, it will not detect this. So I think "the module specified" that can't be found is numpy.
Adding the following line to your setup.py should fix your problem:
import numpy

Categories

Resources