Skip to content

Executing from . import * within the REPL will import the _pyrepl package, But executing with the -c argument will result in an error. #127960

Closed
@KrisTHL181

Description

@KrisTHL181

Bug report

Bug description:

My working directory is under MyProject, which includes an __init__.py file and other necessary files to be recognized as a package.
But when I try to run the code from . import * in the shell using Python, there are two different outcomes in the two invocation methods (argument passing and REPL execution).
The folder looks like:
├── main.py
├── init.py
├── _implements.py
└── code.py

(PythonProject) C:\Users\KrisT\Desktop\PythonProject>python -c "from . import *"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from . import *
ImportError: attempted relative import with no known parent package

(PythonProject) C:\Users\KrisT\Desktop\PythonProject>python -q
>>> import sys;print(sys.version_info)
sys.version_info(major=3, minor=13, micro=1, releaselevel='final', serial=0)
>>> from . import *
>>> dir()
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'commands', 'completing_reader', 'console', 'historical_reader', 'input', 'keymap', 'main', 'reader', 'readline', 'simple_interact', 'sys', 'trace', 'types', 'utils', 'windows_console']
>>> print(console)
<module '_pyrepl.console' from 'E:\\Python\\Lib\\_pyrepl\\console.py'>
>>> print(input)
<module '_pyrepl.input' from 'E:\\Python\\Lib\\_pyrepl\\input.py'>
>>> print(keymap)
<module '_pyrepl.keymap' from 'E:\\Python\\Lib\\_pyrepl\\keymap.py'>
>>>

It loaded the _pyrepl package instead of my PythonProject.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-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