Skip to content

Python functions in a script do not inherit enclosing scope #3

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
ctrueden opened this issue Mar 13, 2025 · 4 comments · Fixed by scijava/scyjava#75
Closed

Python functions in a script do not inherit enclosing scope #3

ctrueden opened this issue Mar 13, 2025 · 4 comments · Fixed by scijava/scyjava#75
Labels
bug Something isn't working

Comments

@ctrueden
Copy link
Member

As noted by @ian-coccimiglio here, scripts with functions do not inherit imported modules, nor other globals.

This script fails:

import numpy

def version():
    return numpy.__version__

print(version())

Whereas this script works:

import numpy

def version():
    global numpy
    return numpy.__version__

print(version())

Interestingly, SciJava #@ variables injected into the bindings are put into locals() but not globals(), which means they are inaccessible to functions even when the global keyword is used:

#@ ImageJ ij
print(ij)
print(f"-- Globals? {'ij' in globals()}")
print(f"-- Locals? {'ij' in locals()}")
print(f"-- Dir? {'ij' in dir()}")

yields:

plugin:net.imagej.ImageJ
-- Globals? False
-- Locals? True
-- Dir? True

So there are maybe two different bugs going on here?

@imagesc-bot
Copy link

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/running-napari-alongside-fiji/109949/5

@github-project-automation github-project-automation bot moved this to Backlog in Fiji Fit Mar 13, 2025
@ctrueden ctrueden added the bug Something isn't working label Mar 13, 2025
@karlduderstadt
Copy link
Collaborator

karlduderstadt commented Mar 14, 2025

So I guess you are saying script_locals should instead by called script_globals here and in the exec function call it should be the globals input and the locals input should be empty?

https://github.com/scijava/scyjava/blob/d1583a1bb4b1fc795829bdb877eb2e8ba46af49c/src/scyjava/_script.py#L68-L103

The fact that imported modules are not accessible in methods is more confusing to me. I thought those import statements are included directly in the script passed to the exec function. 🤔

@ctrueden
Copy link
Member Author

@karlduderstadt Yeah, I'm not sure yet what the best solution is. Just wanted to get the issue filed. If anyone wants to chime in about best practices here, it would be helpful. Otherwise, I'll work on this issue as part of rolling out this component to Fiji's new Java-21-based update site, hopefully some time next month.

@ian-coccimiglio
Copy link

ian-coccimiglio commented Mar 20, 2025

So I guess you are saying script_locals should instead by called script_globals here and in the exec function call it should be the globals input and the locals input should be empty?

https://github.com/scijava/scyjava/blob/d1583a1bb4b1fc795829bdb877eb2e8ba46af49c/src/scyjava/_script.py#L68-L103

The fact that imported modules are not accessible in methods is more confusing to me. I thought those import statements are included directly in the script passed to the exec function. 🤔

Wanted to mention this in relation to why the imported modules are not passed to exec().

#Appose > Getting Started with Fiji-Appose @ 💬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants