Skip to content

Code in top-level file loses access to variables in Interactive (REPL) session #141870

@palswim

Description

@palswim

Bug report

Bug description:

After upgrade from Python 3.12, code in a top-level file can no longer access variables (like modules, etc.) when the execution begins the interactive session.

In the simplest example, suppose I have a file, code.py that contains the following:

import os

Now, if I run Python with this module interactively (python -i code.py), I can't access the os module in the Interactive (REPL) session:

>>> os.listdir('.')
NameError: name 'os' is not defined. Did you forget to import 'os'?

But, to illustrate why this is a problem, consider if code.py had the following contents:

import os
from threading import Thread

def __nstest():
	import time
	time.sleep(5)
	print(os.listdir('.'))

if __name__ == '__main__':
	Thread(target=__nstest).start()
	print(os.listdir('.'))

Again, still a contrived example, but if my __main__ module is passing a function for use later in execution, but that function can no longer access the modules that the __main__ module has imported, this leads to broken execution of code that formerly worked prior to Python 3.13 in Interactive mode. This sounds like it may relate to the new REPL and namespace issues that have arisen (gh-118908).

CPython versions tested on:

3.13.9

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytopic-replRelated to the interactive shelltype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions