I am trying to Call a python function from C++ VS 2017 but I am getting an exception.
I used Cmake to compile pybind11 in VS2017 . I included the headers but I had to link python separately
( I used Cmake to compile pybind11 in VS2017 . I included the headers but I had to link python separately)
Python function :
from scipy.optimize import minimize, rosen, rosen_der
def min_rosen(x0):
res = minimize(rosen, x0)
return res
I used pyInstaller to create .exe file pyinstaller --onefile min_rosen.py
C++ Call :
py::scoped_interpreter guard{};
py::function min_rosen =
py::reinterpret_borrow<py::function>(py::module::import("min_rosen.exe").attr("min_rosen");
py::object result = min_rosen(std::vector<double>{1, 2, 3, 4, 5});
Any kind of help is appreciated.
I have a similar problem when I tried to import a python module written by me. In my case, it worked when I put the python file to my solution folder, current working direcory. I think this problem came from python module location.
Related
I am trying to install matplotlib-cpp in my C++ environment (Visual Studio 2019) in order to do data visualization. I configured all the "include" files, "additional include" files and the "library" file to connect Visual Studio with my Python directory.
I try to plot an example figure:
#include "matplotlibcpp.h"
namespace plt = matplotlibcpp;
int main()
{
plt::plot({1, 2, 3});
plt::show();
}
But I get an error:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.9 from "C:\Users\xxxxx\source\repos\ProjectName\x64\Debug\ProjectName.exe"
* The NumPy version is: "1.21.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: No module named 'numpy.core._multiarray_umath'
Python version: 3.9.0
NumPy version : 1.21.2
I have already upgrade/reinstalled NumPy and Matplotlib with pip.
import sys
sys.path.append(r'E:\Python')
from hypy import htj
tD = np.array([[1, 2, 3],[4, 5, 6]])
x = 0.1
sD, _ = htj.dls_array(np.array([x]), tD)
AttributeError: 'module' object has no attribute 'htj'
When I run the above code in Spyder Python (version 3.8), I get the result. However, I have difficulty running it in the ArcGIS Desktop Python (version 2), where I need to run it.
hypy folder is inside E:\Python.
htj.py is a script having dls_array function located inside the hypy folder.
"_" after sD is with some purpose.
Please suggest a way to handle it as I cannot switch from ArcGIS Desktop to ArcGIS Pro Python due to software unavailability.
Try these:
Make sure you have a __init__.py empty file in folder E:\Python
Try to explicite say from .hypy import htj
Try: from hypy.htj import dls_array and then use dls_array without htj.
4 Most important: Try to avoid using Python2.
I am running the following code:
pyinstaller --onefile main.py
main.py looks like:
import sys
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
import_pythonpkg.py looks like:
from astroML.density_estimation import EmpiricalDistribution
import calendar
import collections
from collections import Counter, OrderedDict, defaultdict
import csv
....
By running the pyinstaller on main.py, main.exe file is created successfully.
But when I run main.exe it gives error with astroML. If I move astroML to main.py from import_pythonpkg.py, there is no error with astroML. Now I get error with csv.
i.e. if I change my main.py to look as:
import sys
from astroML.density_estimation import EmpiricalDistribution
import os
sys.path.append(r'C:\Model\Utilities')
from import_pythonpkg import *
......
The astroML error is no longer present when I run main.exe.
There is no error with import calendar line in import_pythonpkg.py at all.
I am not sure how to handle this random error with packages when running main.exe after pyinstaller run.
import_pythonpkg is located at r'C:\Model\Utilities'
Edit:
Error with main.exe looks as following even though the original main.py runs fine. Pyinstaller was even able to let me create the main.exe without error.
Traceback (most recent call last):
File "main.py", line 8, in <module>
File "C:\Model\Utilities\import_pythonpkg.py", line 1, in <module>
from astroML.density_estimation import EmpiricalDistribution
ImportError: No module named astroML.density_estimation
[29180] Failed to execute script main
I believe PyInstaller is not seeing import_pythonpkg. In my experience, when adding to the path or dealing with external modules and dlls, PyInstaller will not go searching for that, you have to explicitly tell it to do so. It will compile down to an .exe properly because it just ignores it, but then won't run. Check to see if there are any warnings about missing packages or modules when you run your PyInstaller command.
But how to fix it...If indeed this is the issue (which I am not sure that it is) you can try 3 things:
1) move that package into your working directory and avoid using sys.path.append. Then compile with PyInstaller to so see if this works, then you know the issue is that pyinstaller is failing to find import_pythonpkg. You can stop there if this works.
2) explicitly tell PyInstaller to look there. You can use the hidden-import tag when compiling with PyInstaller to let it know (give it the full pathname).
--hidden-import=modulename
for more info, check here: How to properly create a pyinstaller hook, or maybe hidden import?
3) If you use the spec file that PyInstaller creates, you can try adding a variable call pathex to tell PyInstaller to search there for things:
block_cipher = None
a = Analysis(['minimal.py'],
pathex=['C:\\Program Files (x86)\\Windows Kits\\10\\example_directory'],
binaries=None,
datas=None,
hiddenimports=['path_to_import', 'path_to_second_import'],
hookspath=None,
runtime_hooks=None,
excludes=None,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,... )
coll = COLLECT(...)
for more information on spec files: https://pyinstaller.readthedocs.io/en/stable/spec-files.html
(notice you can also add hiddenimports here)
This answer may also prove helpful: PyInstaller - no module named
It is about to module which loaeded on your computer. If your IDE is different from your environment, you have to load same modules on your device via pip. Check the modules on CMD screen and complete the missing modules.
Sometimes you must load the modules all IDEs on your device. In my case, there were two IDEs (pycharm and anaconda). I used pycharm but pyinstaller used anaconda's modules so i unistalled anaconda and tried again. now it works..
Cython no longer works appropriately on my version of Ubuntu, this appears to be related to me installing either Clion or Pycharm (both professional editions), as building worked fine previously. Anaconda version = 3.5.2 using python 3.5.2, The code is simple:
# setup.py
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
source = ["cythonsrc/testing.pyx"]
extensions = [Extension("testing", source, language='c++',
extra_compile_args=["-std=c++11", "-g"],
extra_link_args=["-std=c++11"])]
setup(
ext_modules = cythonize(extensions)
)
pyx file
#testing.pyx
#!python
#cython: language_level=3, boundscheck=false
# distutils: language=c++
def test2():
print("HEF")
Build results
python cythonsrc/setup.py build_ext --inplace
/home/name/anaconda3/lib/python3.5/site-packages/Cython/Distutils/old_build_ext.py:30: UserWarning: Cython.Distutils.old_build_ext does not properly handle dependencies and is deprecated.
"Cython.Distutils.old_build_ext does not properly handle dependencies "
running build_ext
Module import results
>>> import testing
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: /home/name/Documents/CythonTest/testing.cpython-35m-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE9_M_createERmm
Not sure what would cause this issue, but google is failing me, I've seen people talk about g++ versions causing issue with this, however this doesn't appear to be my problem as no one has had this issue with the g++ version currently installed, and it wouldn't matter anyway I need the build process to be cross platform (and thus can't enforce g++ versions). Other people with similar undefined symbol errors are due to setup.py user error, however I've used this process with other projects on other systems and it worked, and now previous projects that were building fine no longer build on my system with out editing the code.
My inclination is that I must have downloaded or updated something (such as the Jetbrains products) that have caused some issue with the normal build or environment variables on my system.
EDIT:
I should mention the actual C++ build process works fine for .cpp .h files outside the context of Cython, I've read people mentioning python 2.7 has issues that might cause these issues but couldn't see any solutions out of that (in addition to me not using 2.7).
I have a program which uses a tkinter GUI along with the tktable module (I use it to display the results of a SQL query). It works fine when running it as a .py file, but compiling it into a .exe results in an error with the program not being able to locate tktable.
I am using pyinstaller to make my .exe and I know that tktable is not on the list of supported modules. Is there a way to make this work? I made an attempt to use py2exe as well using the following code:
from distutils.core import setup
import py2exe
options = {'py2exe': {
'packages': ['pyodbc','tktable'],
'includes': 'decimal',
'compressed':1,
'bundle_files': 1,
'dist_dir': "exe/dist/dir"
'dll_excludes' }}
setup(console=['<PYTHON FILE>'], options=options,zipfile = None)
but the compiled executable simply crashes (works fine for pyodbc, but not for tktable). Is there support out there for compiling the tktable module to be used with an executable?