fusion-engineering / inline-python

Inline Python code directly in your Rust code
https://docs.rs/inline-python
BSD 2-Clause "Simplified" License
1.15k stars 37 forks source link

sys.argv is empty #31

Open optozorax opened 4 years ago

optozorax commented 4 years ago

I'm trying to run this code:

python! {
    import matplotlib.pyplot as plt

    x = [1, 2, 3]
    y = [2, 4, 1]

    plt.plot(x, y)

    plt.xlabel("x - axis")
    plt.ylabel("y - axis")

    plt.title("Line graph!")

    plt.savefig("foo.png")
}

But get only this error:

thread 'main' panicked at 'python!{...} failed to execute'

But this code works as single python file.

It will be fine to get errors from python interpreter why code is failed to execute.

Also, is import supported?

optozorax commented 4 years ago

Hmmm, sorry for being husty. I'm discovered that simple print() inside inline python didn't show anything on my console.

I use Windows 10 + Git Bash.

On PowerShell python errors and print showed correctly.

Also, the original error is this:

baseName = os.path.basename(sys.argv[0])
IndexError: list index out of range

So, this hack works:

import sys
sys.argv = ["./myprog"]

Therefore Original issue expanded to two problems:

  1. Nothing shows on Windows 10 + Git Bash
  2. sys.argv is empty

I think by default sys.args[0] should contain program address.

m-ou-se commented 4 years ago

Interesting. Here on Linux, sys.argv is [''], not []. Your example runs fine here.

Apparently something about sys.argv is different on Windows.

m-ou-se commented 4 years ago

I don't have a Windows computer here to test, but looking at the CPython source code, it looks like it should be impossible for sys.argv to be empty:

    /* sys.argv must be non-empty: empty argv is replaced with [''] */
    assert(config->argv.length >= 1);

source

So not sure what's happening in your case.

Milchdealer commented 3 years ago

This is also the documented behaviour:

If no script name was passed to the Python interpreter, argv[0] is the empty string.