Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Py.Import returns null when importing tensorflow #299

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
snarb opened this issue Dec 2, 2016 · 12 comments
Closed

Py.Import returns null when importing tensorflow #299

snarb opened this issue Dec 2, 2016 · 12 comments

Comments

@snarb
Copy link

snarb commented Dec 2, 2016

Py.Import returns null when importing tensorflow at the same time python from command line or from IDE works with that import without any problems.

@den-run-ai
Copy link
Contributor

den-run-ai commented Dec 2, 2016 via email

@filmor
Copy link
Member

filmor commented Dec 3, 2016

It could help debugging to do a throw new PythonException() immediately after the Py.Import. In fact, I think this should be done in any case, instead of returning null here.

@snarb
Copy link
Author

snarb commented Dec 4, 2016

That is anaconda with python 3.5 + tensorflow. I can give you remote access with team viewer to that env with installed VS and pythonnet sources. I can debug and see that in pythonnet hook routine looks like Py.Import return some error. But it structure is very unclear for new guy in this topic, like me.

@den-run-ai
Copy link
Contributor

Using importlib I found the exception thrown in tensorflow.python.platform.flags


using System;
using Python.Runtime;

namespace tensorflowinclr
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic tf;
            dynamic gpb;
            dynamic np;
            using (Py.GIL()) {
                np = Py.Import("numpy");
                gpb = Py.Import("google.protobuf");
                tf = Py.Import("tensorflow");
                //tf = Py.Import("tensorflow.python.platform.flags");
                tf = Py.Import("importlib");
                tf = tf.import_module("tensorflow");
                if (tf == null) {
                    Console.WriteLine("bad import");
                }
            }
        }
    }
}
Unhandled Exception: Python.Runtime.PythonException: AttributeError : module 'sys' has no attribute 'argv'
   at Python.Runtime.PyObject.Invoke(PyTuple args, PyDict kw) in C:\Users\denfromufa\Documents\pythonnet\src\runtime\pyobject.cs:line 688
   at Python.Runtime.PyObject.InvokeMethod(String name, PyTuple args, PyDict kw) in C:\Users\denfromufa\Documents\pythonnet\src\runtime\pyobject.cs:line 756
   at Python.Runtime.PyObject.TryInvokeMember(InvokeMemberBinder binder, Object[] args, Object& result) in C:\Users\denfromufa\Documents\pythonnet\src\runtime\pyobject.cs:line 1002
   at CallSite.Target(Closure , CallSite , Object , String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
   at tensorflowinclr.Program.Main(String[] args) in c:\users\denfromufa\documents\visual studio 2015\Projects\tensorflowinclr\tensorflowinclr\Program.cs:line 24
Press any key to continue . . .

I think the problem is that in pythonnet we are not setting sys.argv, like suggested in this question.

http://stackoverflow.com/questions/12230210/attributeerror-module-object-has-no-attribute-argv-when-using-python-h

@filmor what do you think?

@den-run-ai
Copy link
Contributor

@pkonovalov-softheme I'm able to import tensorflow in pythonnet with this temp fix.

make a file setsysargv.py and save it in site-packages folder:

import sys
setattr(sys,"argv",[""])

import it before tensorflow:

using System;
using Python.Runtime;

namespace tensorflowinclr
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic tf;
            dynamic gpb;
            dynamic np;
            dynamic sys;
            using (Py.GIL()) {
                np = Py.Import("numpy");
                gpb = Py.Import("google.protobuf");
                sys = Py.Import("setsysargv");
                tf = Py.Import("tensorflow");
                if (tf == null) {
                    Console.WriteLine("bad import");
                }
            }
        }
    }
}

@filmor
Copy link
Member

filmor commented Dec 5, 2016

Maybe we could expose a SetArgv function on Py?

@den-run-ai
Copy link
Contributor

@filmor it is already is, but it is not used in initialize:

PySys_SetArgv(int argc, IntPtr argv);

den-run-ai pushed a commit that referenced this issue Dec 6, 2016
@den-run-ai
Copy link
Contributor

I put together a pull request to resolve this issue:

#301

@snarb
Copy link
Author

snarb commented Dec 27, 2016

Can be closed. Works. Thank you!

@vmuriart vmuriart mentioned this issue Jan 25, 2017
9 tasks
@filmor filmor mentioned this issue Jan 31, 2017
@filmor filmor closed this as completed Feb 1, 2017
@den-run-ai
Copy link
Contributor

den-run-ai commented Feb 1, 2017 via email

@vmuriart
Copy link
Contributor

vmuriart commented Feb 1, 2017

idk about another release, though I'm getting the hang of them and should be less painful now that the long description gets written from the readme.md.

@den-run-ai
Copy link
Contributor

den-run-ai commented Jul 1, 2017

here is example how to call into tensorflow from C# using pythonnet: #473

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

4 participants