Skip to content

python:3.12.0 name not defined error #7992

Closed

Description

@CyberQin

scipy stats module report name 'obj' not defined

Context information (for bug reports)

  • Output of pyinstaller --version: 6.0.0

  • Version of Python: 3.12.0

  • Platform: Windows (zh-CN)

  • How you installed Python: conda

  • Did you also try this on another platform? Does it work there?: have tried ubuntu22.04, the same error reported

    • start with clean installation
    • use the latest development version
    • Run your frozen program from a command window (shell) — instead of double-clicking on it
    • Package your program in --onedir mode
    • Package without UPX, say: use the option --noupx or set upx=False in your .spec-file
    • Repackage you application in verbose/debug mode. For this, pass the option --debug to pyi-makespec or pyinstaller or use EXE(..., debug=1, ...) in your .spec file.

A minimal example program which shows the error

from scipy.stats import weibull_min


if __name__ == '__main__':
    cc = weibull_min(c=2.5, loc=0, scale=1)
    print(cc.cdf(0.001))

Stacktrace / full error message

Traceback (most recent call last):
  File "main.pyw", line 15, in <module>
  File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "scipy\stats\__init__.py", line 608, in <module>
  File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "scipy\stats\_stats_py.py", line 46, in <module>
  File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "scipy\stats\distributions.py", line 8, in <module>
  File "<frozen importlib._bootstrap>", line 1354, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1325, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 929, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "scipy\stats\_distn_infrastructure.py", line 360, in <module>
NameError: name 'obj' is not defined
[26600] Failed to execute script 'main' due to unhandled exception!

I read the source code in scipy, the 'obj' located at scipy.stats._distn_infrastructure.py line 360. 'obj' is not defined before but only in some comprehensive list.

It totally ok when python vesion=3.11, but when i change to python 3.12, it report error. I was wondering if its a python 3.12 import system problem. Seems pyinstaller + python 3.11 won't import this 'del obj', but pyinstaller + python 3.12 will do.

# clean up all the separate docstring elements, we do not need them anymore
for obj in [s for s in dir() if s.startswith('_doc_')]:
    exec('del ' + obj)
del obj

Activity

  1. added
    triagePlease triage and relabel this issue
    on Oct 8, 2023
  2. rokm commented on Oct 8, 2023

    @rokm
    Member

    Hmm, this definitely looks like a bug, although I will need to dig further to see if it is ours or python's (or some weird combination of both).

  3. added and removed
    triagePlease triage and relabel this issue
    on Oct 8, 2023
  4. rokm commented on Oct 8, 2023

    @rokm
    Member

    The behavior is definitely bizarre, as demonstrated by following minimal program:

    import sys
    
    if len(sys.argv) != 2:
        print(f"usage: test <dir|locals|globals>")
        sys.exit(1)
    mode = sys.argv[1]
    
    # The comprehension must use same variable name as the code that attempts `del`.
    _allvalues = ''.join([myobj for myobj in ['a', 'b', 'c']])
    
    myobj = None  # for del below
    if mode == 'dir':
        print("DIR():", dir())
    elif mode == 'locals':
        print("LOCALS():", locals())
    elif mode == 'globals':
        print("GLOBALS():", globals())
    
    del myobj

    The codepaths that call either dir() or locals() end up with myobj undefined when we try to delete it.

    So far, all evidence points towards PyInstaller.building.utils.strip_paths_in_code being the culprit. Making that no-op makes both the above example and an import scipy.stats program work. So there's probably some incompatibility with 3.12 in there that will need to be investiagted and fixed.

  5. CyberQin commented on Oct 9, 2023

    @CyberQin
    Author

    https://docs.python.org/3.12/whatsnew/3.12.html#pep-709-comprehension-inlining

    maybe python3.12 pep709 affect it?

    Comprehension iteration variables remain isolated and don’t overwrite a variable of the same name in the outer scope, nor are they visible after the comprehension. Inlining does result in a few visible behavior changes:
    
    There is no longer a separate frame for the comprehension in tracebacks, and tracing/profiling no longer shows the comprehension as a function call.
    
    The [symtable](https://docs.python.org/3.12/library/symtable.html#module-symtable) module will no longer produce child symbol tables for each comprehension; instead, the comprehension’s locals will be included in the parent function’s symbol table.
    
    Calling [locals()](https://docs.python.org/3.12/library/functions.html#locals) inside a comprehension now includes variables from outside the comprehension, and no longer includes the synthetic .0 variable for the comprehension “argument”.
    
    A comprehension iterating directly over locals() (e.g. [k for k in locals()]) may see “RuntimeError: dictionary changed size during iteration” when run under tracing (e.g. code coverage measurement). This is the same behavior already seen in e.g. for k in locals():. To avoid the error, first create a list of keys to iterate over: keys = list(locals()); [k for k in keys].
    
  6. rokm commented on Oct 9, 2023

    @rokm
    Member

    Looks like a bug in python's code.replace(), which we use in strip_paths_in_code. I've opened a bug report with python.

  7. CyberQin commented on Oct 12, 2023

    @CyberQin
    Author

    although it's not pyinstaller's bug, but should pyinstaller treat python 3.12.0 as a not full compability py version.

  8. bwoodsend commented on Oct 12, 2023

    @bwoodsend
    Member

    We can add a sentence saying as much in the readme but that's all we can do. It's impossible to withdraw support for a Python version once we've marked one release as supporting it. I'll pin the issue for now – that tends to keep people from raising duplicate issues.

  9. pinned this issue on Oct 12, 2023
  10. jeffheaton commented on Oct 18, 2023

    @jeffheaton

    FYI, Running into this one as well. Only see it in Pyinstaller too, only in 3.12. Will revert Python versions.

  11. noahyoah commented on Oct 31, 2023

    @noahyoah

    Good day everyone!

    I am reaching out to determine if anyone has found a work around for this issue. I am attempting create an executable using auto-py-to-exe and unfortunately it is failing due to 'obj' not defined.

    Additionally, I believe that this same issue is happening with the module statsmodels.api as well.

    Any information you provide would be greatly appreciated.

  12. rokm commented on Oct 31, 2023

    @rokm
    Member

    I am reaching out to determine if anyone has found a work around for this issue

    The obvious work-around would be not to use python 3.12 until they fix code.replace(), would it not?

    Other than that, you could also:

    • patch your local copy of scipy and comment that attempt at removing obj (here)
    • patch your local copy of PyInstaller to make strip_paths_in_code no-op, i.e., immediately return original co (if non-anonymized paths are not a problem for you)
  13. noahyoah commented on Oct 31, 2023

    @noahyoah

    Rokm,

    I appreciate your response. I used an older version of python and was able to create the executable. Thank you for pointing me in the correct direction.

  14. CyberQin commented on Nov 9, 2023

    @CyberQin
    Author

    Good News, A bug fix have been merged to python:main and python:3.12 branch 9 hours ago, maybe 3.12.1 will sove this problem! python/cpython#111866 .

  15. changed the title [-]python3.12 name not defined error[/-] [+]python:3.12.0 name not defined error[/+] on Nov 9, 2023
  16. added a commit that references this issue on Nov 12, 2023
  17. CyberQin commented on Dec 11, 2023

    @CyberQin
    Author

    python 3.12.1 released , this bug has been fixed ,pls upgrade your python version to avoid this issue.

    Actually pyinstaller not work well only with python=3.12.0

  18. added a commit that references this issue on Dec 16, 2023
  19. unpinned this issue on Dec 31, 2023
  20. locked as resolved and limited conversation to collaborators on Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions