Using argparse beside kivy

959 views
Skip to first unread message

RedDevil91

unread,
Jul 28, 2016, 10:58:59 AM7/28/16
to Kivy users support
Hi!

I ran into an issue when tested my kivy app. Kivy somehow "overrides" argparse and I can't use it.
Is there a way to disable Kivy's default argument parser?

Regards,

Attila

M Dammer

unread,
May 16, 2017, 10:33:58 AM5/16/17
to kivy-...@googlegroups.com
OK here is what solved the problem for me:
In the program use
args = parser.parse_args(sys.argv[2:])

And then all command line arguments can be called via
mykivyapp.py - -myarg1 -myarg2 --my_arg3

Andreas Ecker

unread,
May 16, 2017, 12:19:00 PM5/16/17
to kivy-...@googlegroups.com
Not sure if this helps.

Some months ago I had to do a small kivy app that needed a command line interface with extra and non-kivy command line options and parameters. And I also got errors in the kivy import statements because kivy is also checking and interpreting the specified command line args on app startup (when it reaches the kivy import statements in your code).

The only work around I found was to process and then removed my extra/non-kivy command line args from `sys.argv` before I did the `from kivy.... import ...` statements by putting the following statement before the kivy imports:

```
sys.argv = [sys.argv[0]]
```

Please note that changing the `sys.argv` value is not recommended at all, you will also not be able to pass any kivy-command-line-options and can lead to other problems. But for my case it was the only way to work around (and it was only a very small app with no other imports and side-effects).

The kivy command line options are all documented in the following link (scroll down to the kivy_usage() method): https://kivy.org/docs/_modules/kivy.html.

2017-05-16 15:33 GMT+01:00 'M Dammer' via Kivy users support <kivy-...@googlegroups.com>:
BUMP I have the same problem.


On Thursday, July 28, 2016 at 3:58:59 PM UTC+1, RedDevil91 wrote:
Hi!

I ran into an issue when tested my kivy app. Kivy somehow "overrides" argparse and I can't use it.
Is there a way to disable Kivy's default argument parser?

Regards,

Attila

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Geekademy

unread,
May 16, 2017, 2:02:58 PM5/16/17
to kivy-...@googlegroups.com
I've grumbled about this before, it is bad form to take over the command line
and logging, etc, at import time without so much as an init() call.

To work around it in my app, I put all the kivy stuff into its own modules.
Meaning the main.py only has standard python in it, with kivy deferred. Use
argparse's parse_known_args:

https://docs.python.org/3/library/argparse.html#partial-parsing

This will return any extras, which you can pass to kivy when loading it later,
by importing kivy-using modules under a main() function for example. There is
also an environment variable to shut off the logging config.

How I did it (inelegant but works):

args, unknown = parser.parse_known_args()
sys.argv[1:] = unknown

# load kivy app later

Also, I do the argparsing in a loading script that happens before main.py on
linux, but that is skipped on android for example, allowing it to work easily in
both contexts.
Reply all
Reply to author
Forward
0 new messages