Skip to content
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

Can't use Eve library with pyotherside #77

Closed
egfconnor opened this issue Jan 11, 2017 · 10 comments
Closed

Can't use Eve library with pyotherside #77

egfconnor opened this issue Jan 11, 2017 · 10 comments

Comments

@egfconnor
Copy link

I'm trying to setup an HTTP API in a QML project and when I run the simple quick start python code of:

from eve import Eve
app = Eve()
app.run()

it works using just Python but when I invoke this .py file with Pyotherside I get the following message:

"PyOtherSide error: Traceback (most recent call last):\n\n File "qrc://EveAPI.py", line 6, in start\n app = Eve()\n\n File "/home/connor/.local/lib/python3.5/site-packages/eve/flaskapp.py", line 138, in init\n self.load_config()\n\n File "/home/connor/.local/lib/python3.5/site-packages/eve/flaskapp.py", line 230, in load_config\n abspath = os.path.abspath(os.path.dirname(sys.argv[0]))\n\nAttributeError: module 'sys' has no attribute 'argv'\n"
qml: python error: Return value of PyObject call is NULL: Traceback (most recent call last):

File "qrc://EveAPI.py", line 6, in start
app = Eve()

File "/home/connor/.local/lib/python3.5/site-packages/eve/flaskapp.py", line 138, in init
self.load_config()

File "/home/connor/.local/lib/python3.5/site-packages/eve/flaskapp.py", line 230, in load_config
abspath = os.path.abspath(os.path.dirname(sys.argv[0]))

AttributeError: module 'sys' has no attribute 'argv'

@egfconnor
Copy link
Author

I fixed it by adding my own

sys.argv = ['']

before the creation of Eve().

@egfconnor
Copy link
Author

egfconnor commented Jan 24, 2017

I seem to have found what may be the root issue here. If I do multiple addImportPath statements before my importModule in QML it will throw weird errors such as this in the Python script. If I only have one addImportPath it behaves as expected.

@egfconnor
Copy link
Author

File "/usr/lib/python3.5/os.py", line 112, in \n sys.modules['os.path'] = path\n\nAttributeError: module 'sys' has no attribute 'modules'\n"

This is the error I get anytime I do addImportPath twice. My goal is to be able to reference other Python modules from my Python script.

@egfconnor
Copy link
Author

Alright I'm able to do multiple addImportPath with absolute paths like: addImportPath('/home/connor/Repos/Test/source/src/Python/domain')

but not like
addImportPath(Qt.resolvedUrl('qrc:/Python/domain'))

@thp
Copy link
Owner

thp commented Jan 28, 2017

Do you have a Short, Self Contained, Correct (Compilable), Example (http://sscce.org) for demonstrating the issue?

@egfconnor
Copy link
Author

@thp I will put together an example as soon as I can.

@egfconnor
Copy link
Author

Here is an example that shows my root import issues:

https://drive.google.com/file/d/0B5CPtCs60_W-U3ZEX2E2T3NpU3c/view?usp=sharing

Am I supposed to always have my Python and QML files in the same directory?

@egfconnor
Copy link
Author

egfconnor commented Apr 5, 2017

I'm still investigating how the importing works. If I take your example project and change it from qrc_example.py to anything else...say test.py, update the .qrc file and .qml file to reference it...it won't work. It will say it had success importing the module but the 'Hello from module!' won't print nor can I call any specific functions.

Is it a requirement every python file you call from a qml file has the exact same name?

@thp
Copy link
Owner

thp commented Apr 14, 2017

No, there is no such requirement, but you might need to tweak the Python import path.

@egfconnor
Copy link
Author

egfconnor commented Oct 18, 2017

So I finally remembered this issue and I've noticed it with other libraries too and it boils down to the embedded Python doesn't get the sys.argv attribute set by default.

Before importing the offending libraries, which for me have been Eve or systemd-python, you can simply set: sys.argv = [''] and it will cause things to work.

I wonder if for wider Python library use in Pyotherside if we should call PySys_SetArgv with an empty array right after you call Py_InitializeEx(0)? This would be preferable to currently where it will just break when importing a certain library.

I have tested this in the Pyotherside library and it works:

Py_InitializeEx(0);

wchar_t** wargv = (wchar_t**) malloc(0 * sizeof(wchar_t*));
//or
wchar_t* wargv[0];

PySys_SetArgv(0, wargv);

void Py_Initialize()
Initialize the Python interpreter. In an application embedding Python, this should be called before using any other Python/C API functions; with the exception of Py_SetProgramName(), Py_SetPythonHome() and Py_SetPath(). This initializes the table of loaded modules (sys.modules), and creates the fundamental modules builtins, main and sys. It also initializes the module search path (sys.path). It does not set sys.argv; use PySys_SetArgvEx() for that. This is a no-op when called for a second time (without calling Py_FinalizeEx() first). There is no return value; it is a fatal error if the initialization fails.

@thp thp closed this as completed in 747f6b2 Nov 8, 2017
thp added a commit that referenced this issue Nov 8, 2017
Initialize sys.argv (Fixes #77)
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

2 participants