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

python:3.12.0 name not defined error #7992

Closed
5 of 6 tasks
CyberQin opened this issue Oct 8, 2023 · 12 comments
Closed
5 of 6 tasks

python:3.12.0 name not defined error #7992

CyberQin opened this issue Oct 8, 2023 · 12 comments

Comments

@CyberQin
Copy link

CyberQin commented Oct 8, 2023

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
@CyberQin CyberQin added the triage Please triage and relabel this issue label Oct 8, 2023
@rokm
Copy link
Member

rokm commented Oct 8, 2023

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).

@rokm rokm added bug and removed triage Please triage and relabel this issue labels Oct 8, 2023
@rokm
Copy link
Member

rokm commented Oct 8, 2023

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.

@CyberQin
Copy link
Author

CyberQin commented Oct 9, 2023

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].

@rokm
Copy link
Member

rokm commented Oct 9, 2023

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.

@rokm rokm added not-our-bug and removed bug labels Oct 10, 2023
@CyberQin
Copy link
Author

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

@bwoodsend
Copy link
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.

@bwoodsend bwoodsend pinned this issue Oct 12, 2023
rokm added a commit to rokm/pyinstaller that referenced this issue Oct 18, 2023
SciPy is currently broken under python 3.12 and PyInstaller, due
to pyinstaller#7992.
rokm added a commit that referenced this issue Oct 18, 2023
SciPy is currently broken under python 3.12 and PyInstaller, due
to #7992.
@jeffheaton
Copy link

jeffheaton commented Oct 18, 2023

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

@noahyoah
Copy link

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.

@rokm
Copy link
Member

rokm commented Oct 31, 2023

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)

@noahyoah
Copy link

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.

@CyberQin
Copy link
Author

CyberQin commented Nov 9, 2023

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 .

@CyberQin CyberQin changed the title python3.12 name not defined error python:3.12.0 name not defined error Nov 9, 2023
muziing added a commit to muziing/Py2exe-GUI that referenced this issue Nov 12, 2023
由于当前 PyInstaller [不支持 Python 3.12](pyinstaller/pyinstaller#7992 3.12 的支持
@CyberQin
Copy link
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

muziing added a commit to muziing/Py2exe-GUI that referenced this issue Dec 16, 2023
根据 <pyinstaller/pyinstaller#7992 (comment)> ,在 Python 3.12.1中已经可以使用 PyInstaller,故将本项目也恢复对 Python 3.12 的支持;
@rokm rokm unpinned this issue Dec 31, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 9, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants