Python入门者遇无法导入任何模块问题,求解决方案
Hey there! Let's clear up the confusion here because it looks like you're mixing up two different concepts: importing Python modules and reading text files. That's super common when you're just starting out—no worries at all!
First, why you're getting ModuleNotFoundError
When you run import info, Python is looking for a Python module (a file named info.py) in its search paths, not a regular text file. If you're trying to read a text file (like info.txt), using import is the wrong approach entirely. That's why you're seeing that error—Python can't find a module named info because you don't have an info.py file (or it's not in the right place).
How to correctly read a text file
If your goal is to read and print the contents of a text file, use Python's built-in open() function instead. Here's a simple example:
# Replace "info.txt" with your actual text file name/path with open("info.txt", "r") as file: file_content = file.read() print(file_content)
- Make sure the text file is in the same folder as your Python script, or use the full file path (like
C:/Users/YourName/Documents/info.txton Windows or/home/YourName/Documents/info.txton macOS/Linux) if it's somewhere else.
If you did mean to import a Python module (info.py)
If you actually have an info.py file you want to import, double-check these things:
- The
info.pyfile is located in the same directory as the script you're running. - You haven't named your main script
info.py(that would cause a circular import issue). - The file isn't saved with a typo (like
info.txtinstead ofinfo.py—file extensions matter!).
Just a quick note: Adding Python to your PATH is only for making sure your system can find the python command when you run it. Since you're able to execute your code and get an error message, your PATH setup is probably fine—this issue is about how you're trying to access your file.
内容的提问来源于stack exchange,提问作者Phobos




