Skip to content

Unable to embed python in C# application on Ubuntu 16.04: System.EntryPointNotFoundException: Py_IsInitialized #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mikeoconnor0308 opened this issue Sep 25, 2018 · 9 comments

Comments

@mikeoconnor0308
Copy link

Environment

  • Pythonnet version: github master
  • Python version: 3.5
  • Operating System: Ubuntu 16.04
  • Mono version: 5.4.1.6
  • Clang version: 3.8.0-2ubuntu4
  • gcc version: 5.2

Details

  • Describe what you were trying to get done.

Based on the issue #470, I am attempting to compile pythonnet from source.

I have installed clang and glib2-dev as advised on the wiki, and build as indicated below.

python setup.py bdist_wheel
  • If there was a crash, please include the traceback here.
gcc -pthread -shared -B /home/mike/anaconda3/compiler_compat -L/home/mike/anaconda3/lib -Wl,-rpath=/home/mike/anaconda3/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.6/src/monoclr/pynetinit.o build/temp.linux-x86_64-3.6/src/monoclr/clrmod.o -o build/lib.linux-x86_64-3.6/clr.cpython-36m-x86_64-linux-gnu.so -L/usr/lib/pkgconfig/../../lib -lmono-2.0 -lm -lrt -ldl -lpthread -lglib-2.0
/home/mike/anaconda3/compiler_compat/ld: cannot find -lm
/home/mike/anaconda3/compiler_compat/ld: cannot find -lrt
/home/mike/anaconda3/compiler_compat/ld: cannot find -ldl
/home/mike/anaconda3/compiler_compat/ld: cannot find -lpthread
/home/mike/anaconda3/compiler_compat/ld: cannot find -lgcc_s
/home/mike/anaconda3/compiler_compat/ld: cannot find -lpthread
/home/mike/anaconda3/compiler_compat/ld: cannot find -lc
/home/mike/anaconda3/compiler_compat/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
error: command 'gcc' failed with exit status 1

As a workaround, I added '-L/usr/lib/x86_64-linux-gnu/' and '-L/usr/lib/gcc/x86_64-linux-gnu/5' to the setup.py extra_link_args field, after which it builds successfully. Manually editing setup.py does not seem like the advised strategy, can anyone advise on a better solution?

Cheers,

@mikeoconnor0308
Copy link
Author

In what I believe must be a related issue of path configurations, if I build and install pythonnet as above, embedding C# within python works. However, when I attempt to embed python within a C# application:

using(Py.GIL())
{
    dynamic sys = Py.Import("sys");
    sys.path.append(qmlDir);
    QmlPredict = Py.Import("predict");
}

I get the following error:

Unhandled Exception:
System.EntryPointNotFoundException: Py_IsInitialized
  at (wrapper managed-to-native) Python.Runtime.Runtime:Py_IsInitialized ()
  at Python.Runtime.Runtime.Initialize () [0x00000] in <fd547cae3881478db0cb870640e39b43>:0 
  at Python.Runtime.PythonEngine.Initialize (System.Collections.Generic.IEnumerable`1[T] args, System.Boolean setSysArgv) [0x00014] in <fd547cae3881478db0cb870640e39b43>:0 
  at Python.Runtime.PythonEngine.Initialize (System.Boolean setSysArgv) [0x00005] in <fd547cae3881478db0cb870640e39b43>:0 
  at Python.Runtime.PythonEngine.Initialize () [0x00000] in <fd547cae3881478db0cb870640e39b43>:0 
  at Python.Runtime.Py.GIL () [0x00007] in <fd547cae3881478db0cb870640e39b43>:0 

I believe this again must be a path problem related to using a conda python installation, but the solution suggested in this issue #489 did not do the trick for me.

@den-run-ai
Copy link
Contributor

@mikeoconnor0308 have you tried cpython instead of anaconda? There are other problems with conda/anaconda on linux such as this one:

https://groups.google.com/a/continuum.io/forum/#!topic/anaconda/z7RboI3Pir8

Also related issues:

https://github.com/pythonnet/pythonnet/search?q=System.EntryPointNotFoundException%3A+Py_IsInitialized&type=Issues

@mikeoconnor0308
Copy link
Author

mikeoconnor0308 commented Sep 25, 2018

Using cpython does compile successfully.

I still have the problem of System.EntryPointNotFoundException: Py_IsInitialized, despite trying the solutions proposed in #303 and #489. I could open this as a separate issue?

@den-run-ai
Copy link
Contributor

@mikeoconnor0308 just change the title of this issue and update the headline. can you document which suggestions you tried?

@den-run-ai
Copy link
Contributor

@mikeoconnor0308 did you try this?

#245 (comment)

@mikeoconnor0308 mikeoconnor0308 changed the title Unable to link required libraries when building from source via an anaconda environment. Unable to embed python in C# application on Ubuntu 16.04: System.EntryPointNotFoundException: Py_IsInitialized Sep 26, 2018
@mikeoconnor0308
Copy link
Author

@denfromufa I have tried the symbolic link suggestion proposed here: #303 (comment)

ln -s /usr/lib/python3.5/config-x86_64-linux-gnu/libpython3.5m.so .

and the LD_PRELOAD method suggested here: #489 (comment)

LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libpython3.5m.so 

finally, i have now tried #245 (comment)

PY_LIBDIR=$(python -c 'import sysconfig; print(sysconfig.get_config_var("LIBDIR"))')
export LD_LIBRARY_PATH=$PY_LIBDIR:$LD_LIBRARY_PATH

None of these tricks resulted in any notable changes, for either the default python3 installed with apt-get, or an anaconda installation. My guess is that during compilation it has not correctly set the libpython library name, or another dependent path is missing? But I do not know how to diagnose it.
The output from a log produced by setting MONO_LOG_LEVEL=debug (attached as csharp_embod.log) is unclear, the issue seems to be:

Mono: DllImport searching in: '__Internal' ('(null)').
Mono: Searching for 'Py_IsInitialized'.
Mono: Probing 'Py_IsInitialized'.
Mono: Could not find 'Py_IsInitialized' due to 'mono: undefined symbol: Py_IsInitialized'.

If we compare that to embedding the CLR in python, (attached as python_clr.log) everything is fine:

Mono: DllImport searching in: '__Internal' ('(null)').
Mono: Searching for 'Py_IsInitialized'.
Mono: Probing 'Py_IsInitialized'.
Mono: Found as 'Py_IsInitialized'.

csharp_embed.log
python_clr.log

@den-run-ai
Copy link
Contributor

@dmitriyse probably understands this more

@filmor
Copy link
Member

filmor commented May 1, 2019

The problem with the Anaconda Python distribution is that it builds the python.exe statically and additionally provides a dynamic libpython. I'm not quite sure why we even check this at build time, it doesn't make much of a difference IIRC. If you remove the respective section from setup.py (sth with enable_shared) it should work fine.

@filmor
Copy link
Member

filmor commented Aug 17, 2021

This is not relevant anymore.

@filmor filmor closed this as completed Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants