Persistent SyntaxError ("name used prior to global declaration") on macOS M4 with Python 3.9/3.10, even in minimal scripts and new virtual environments, not resolved by typical troubleshooting

Even with minimal reproducible test code, such as:
import os
import logging
import traceback
import torch # Import torch

Configure logger

print(“\nConfiguring logger…”)
logging.basicConfig(
level=logging.INFO,
format=“%(asctime)s - %(levelname)s - %(message)s”,
force=True,
)
logger = logging.getLogger(name)
print(“Logger configured successfully”)

Data path and device configuration (not used in this test)

DATA_PATH = “/Users/olivierroy/Documents/cleaned_lotto_results.csv”

Global variables

global_X_tensor = None
global_y_tensor = None
global_time_tensor = None
global_frequencies = None
global_inverted_co_occurrence_matrix_regressed = None
global_intervals_matrix = None
global_interval_sums_regressed = None

Simplified initialization function

def init_globals():
global global_X_tensor, global_y_tensor, global_time_tensor
global global_frequencies, global_inverted_co_occurrence_matrix_regressed
global global_intervals_matrix, global_interval_sums_regressed

print("Starting initialization...")

# Create dummy tensors
global_X_tensor = torch.randn(10, 6)
global_y_tensor = torch.randn(10, 6)
global_time_tensor = torch.randn(10, 1)
global_frequencies = torch.randn(49)
global_inverted_co_occurrence_matrix_regressed = torch.randn(49, 49)
global_intervals_matrix = torch.randn(49, 49)
global_interval_sums_regressed = torch.randn(49)

print("Global variables initialized")
return True

Main execution

if name == “main”:
global global_X_tensor, global_y_tensor, global_time_tensor
global global_frequencies, global_inverted_co_occurrence_matrix_regressed
global global_intervals_matrix, global_interval_sums_regressed

print("Starting main execution...")
init_success = init_globals()

if init_success:
    print(f"global_X_tensor shape: {global_X_tensor.shape}")
    print(f"global_y_tensor shape: {global_y_tensor.shape}")
else:
    print("Failed to initialize global variables")

Error log output is:
(test_env) %n@%m %1~ %# python3 “/Users/olivierroy/Documents/tensors testing.py”
File “/Users/olivierroy/Documents/tensors testing.py”, line 50
global global_X_tensor, global_y_tensor, global_time_tensor
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: name ‘global_X_tensor’ is assigned to before global declaration
(test_env) %n@%m %1~ %#

Tried running in VS code, Apple Terminal,s ame error appears in both.

I use macOS 15.1.1 (24B2091) with Apple M4 chip, the same error appears in both

Here’s a summary of what was ruled out so far:

  • Outdated bytecode: Cleared by removing .pyc files and __pycache__ directories.
  • VS Code extensions: The error persists even with extensions disabled.
  • Hidden characters: The hex editor showed no unusual characters.
  • Your main script’s code logic: The error occurs even in the highly simplified tensors testing.py script, which doesn’t involve any of your original data loading or processing code.
  • Virtual environment: The error is reproducible in multiple virtual environments, including a brand-new one.
  • Python 3.10.10 specifically: You’ve tested with both 3.10.10 and 3.9.10, and the error happens with both.
  • Rosetta 2: We’ve confirmed that Rosetta 2 is likely installed and working.

Thanks for any help!

if __name__ == “__main__”:
    global global_X_tensor, global_y_tensor, global_time_tensor

This is wrong I think. It happens at module scope so all the variables are global already by default. It also happens after you’ve done global_X_tensor = None.

You only need to tell Python that something’s global inside a function/class (where variables will be local by default)

5 Likes

Hi Olivier, and welcome to the forums.

The code you posted is presented badly by the forum software. If you follow this advice About the Python Help category it will come out readable.

1 Like