Use C++ dll in pycharm - python

I’m writing some python code for which I need to access a c++ dll(.so file)
I’m working on pycharm and I’ve already added the library path to my project by following these steps: (settings>project interpreter>Add new interpreter path)
when i run the code
self._bridgeLink = cdll.LoadLibrary(‘foo.so’)
Or
self._bridgeLink = cdll.LoadLibrary(‘./foo.so’)
It gives me an error saying
OSError: foo.so : cannot open shared object file: No such file or directory

The error you are posting suggests that your .so file is not next to your working directory. Try an absolute path to the .so file for testing for now.
You should also check which compiler was used to compile python, you should use the same major version is the Compiler (there are some exception, but that's not the problem for now)

Related

Incorrect Python path used in Visual Studio Python Extension Module (C++)

I'm trying to write a Python library in C++ using Python Native Development Tools' Python Extension Module Template.
However, after creating a project from the template, the line
#include <Python.h>
gives the error "cannot open source file".
If I then go to the project properties, the "additional include directories" parameter reads "$(PythonHome)/include". If I change this to the actual location of my Python.h (C:/anaconda3/include), it then recognises the file, so it seems the value Visual Studio is using for PythonHome is incorrect. The project still doesn't run, as the value seems to be used in other places in the configuration. My question is: how do I change this value? I've tried fiddling with Tools>Python>Python Environments, but it doesn't seem to change it.

How to create a .so file on windows for C++ Library to be used in Python

I use cygwin tools.
But when I compile a .cpp file and load the .so file in Python command line, I get this error message:
OSError: [WinError 126] The specified module could not be found.
But simple .dll files are loaded fine.
I think the fault is in Code or Compiling process. What do I need to write, something like export functions in .dll?
Just simple "hello world" is OK. Please give me a sample, and compile command too.

Compiling the Python Interface for Caffe using CMake

On Ubuntu, I have downloaded Caffe to ~/caffe, and compiled it using cmake with the CMakeLists.txt file that it comes with. I now want to start using the python interface. According to the examples, I need to add ~/caffe/python to the PYTHONPATH variable in my .bashrc file. Then, I need to use use import caffe in a python script. However, this gives me the error: ImportError: No module named 'caffe'.
If I use the line import caffe in my python script, this means that there must exist a file called caffe.py in the directories defined by PYTHONPATH. Is that correct? However, I cannot find any files called caffe.py on my system. But the CMakeLists.txt file is supposed to build the python files, and has the line add_subdirectory(python).
Does anybody have any experience with this? Had anybody got the python interface working by compiling with cmake?
Thanks.
You do not need to have caffe.py in ~/caffe/python. In this case you should have a subfolder ~/caffe/python/caffe that should have __init__.py file in this subfolder. Make sure you have ~caffe/python/caffe/_caffe.so file - this shared object should be created during compilation.
It may be the case that you added the right folder to your .bashrc, but have you loaded/sourced the file after updating it?

Python: wrapper for C++ requires access to .dll files

I made a Python wrapper for an SDK written in C++ using SWIG and built using Microsoft Visual Studio 2010 in Windows 7.
It's been a success, with the module.py and _module.pyd being generated. However, this wrapper heavily depends on a couple .dll files located in the SDK folder outside PythonXY folder.
After I moved the module.py and module.pyd into PythonXY/Lib/site-packages/, and tried running import module, the following error occurs:
ImportError: DLL load failed: The specified module could not be found.
Then, I tried to change the Python working directory to the directory where the .dll files exist, using os.chdir() method from Python module os. This time, running import module output no error. I continued to access the wrapped C++ classes and functions and they worked just fine.
My questions are:
How can I "link" the path containing the .dll files so whenever I want to use module, I don't have to change the working directory?
Is there any way to append additional working directory aside from Python default working directory from within the Python source file? (i.e. not from PATH configuration on Windows)
Is there any way I can link the wrapper with those .dll files dynamically? Meaning that let say the files are copied to another machine and the .dll files are stored in different absolute path directory, the import module would still work?
The point is I want to make this wrapper as portable as possible, across multiple Windows machine.
Thank you.
I found a solution to let module.py find _module.pyd.
Basically, create a new file called module.pth and save it in the same directory as module.py. In this case, the directory is PythonXY/Lib/site-packages/.
Let say you place your _module.pyd inside a directory called _modulelib (PythonXY/Lib/site-packages/_modulelib/), then the content of module.pth should be like this:
_modulelib

Fatal Python error when trying to run an executable Python script

I have a Python script that I have turned into an executable using cx-freeze-4.3.4.win32-py3.4. I have Python 3.4 installed on a Windows 7 64-bit machine.
Here is my simple setup.py file:
from cx_Freeze import setup, Executable
setup( name = "myfilename" ,
version = "0.1" ,
description = "This is my file" ,
executables = [Executable("myfilename.py")] , )
I ran python setup.py build from command prompt in the C:\Python34 folder with both the script I was trying to convert and the setup.py file.
This created another folder called build within was another folder called exe.win32-3.4. In that folder I found my executable file, a bunch of .pyd files, a single .dll file, and a zipped archive called library of a bunch of .pyc files.
If I run the executable from within the exe.win32-3.4 with the library zip archive it executes fine. However, without the library archive of .pyc files (basically if I try just to run the .exe by itself, which is what I am supposed to be able to do) the executable throws out this error:
Fatal Python error: cannot get zipimpirter instance
Current thread 0x000001b48 (most recet call first):
I did some preliminary searching around the web for potential resolutions to the issue but could not find anything substantial. If anyone knows how to troubleshoot this issue that would be much appreciated.
From the docs:
Single-file executables
cx_Freeze does not support building a single file exe, where all of the libraries for your application are embedded in one executable file.
For a single-file solution using py2exe and others, see this question.
In 3.5 there is also the new zipapp module, although the basic functionality has been around for a while.

Categories

Resources