This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: import searches for package even after file was found successfully
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.8
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: MichaelSapphire, eric.smith
Priority: normal Keywords:

Created on 2020-06-19 21:06 by MichaelSapphire, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
index.py MichaelSapphire, 2020-06-19 23:30
Screenshot at 2020-06-19 18-26-53.png MichaelSapphire, 2020-06-19 23:31
Messages (5)
msg371902 - (view) Author: Michael J. (MichaelSapphire) Date: 2020-06-19 21:06
Hello,
Earlier today, I was developing a program and I wanted to check its variables after it finished running. Simply going into a terminal, entering my program's directory, and executing "python3 index.py" would return control to the command line before I would have a chance to examine the data, so I ran "python3", got to the shell, and then imported my file.
My script executed, but after it finished, the intepreter raised a "ModuleNotFoundError: No module named 'index.py'; 'index' is not a package."
Is this supposed to happen after the interpreter finishes importing a file?
Thanks,
Michael
msg371907 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-06-19 22:28
Please provide a small script that demonstrates this behavior.
msg371909 - (view) Author: Michael J. (MichaelSapphire) Date: 2020-06-19 23:30
Attached I have a file containing the script for my program. It's obviously incomplete, but it will work as a sample file for demonstrating the output and behavior of the interpreter. (1/2)
msg371910 - (view) Author: Michael J. (MichaelSapphire) Date: 2020-06-19 23:31
This is a screenshot of the output I received when I ran index.py in a terminal. As you can see, it imports the script OK, but then it tries to find index as a package, and it fails and prints an error. (2/2)
msg371911 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2020-06-19 23:37
You should just be using "import index". By using "import index.py", you're telling the interpreter to first import index, execute the code in it, then look for a sub-module named "py" (full name: index.py). Since no such sub-module exists, and index is not a package, you get the exception you're seeing.

It's like "import os.path": first import "os", then look for "path" inside of that.
History
Date User Action Args
2022-04-11 14:59:32adminsetgithub: 85214
2020-06-19 23:37:50eric.smithsetstatus: open -> closed
resolution: not a bug
messages: + msg371911

stage: resolved
2020-06-19 23:31:19MichaelSapphiresetfiles: + Screenshot at 2020-06-19 18-26-53.png

messages: + msg371910
2020-06-19 23:30:11MichaelSapphiresetfiles: + index.py

messages: + msg371909
2020-06-19 22:28:10eric.smithsetnosy: + eric.smith
messages: + msg371907
2020-06-19 21:06:22MichaelSapphirecreate