杰瑞科技汇

Python不存在?为何仍有人使用它?

Of course. You've typed python doesnotexist, which is a command-line instruction. Let's break down what this means and what the likely outcome is.

Python不存在?为何仍有人使用它?-图1
(图片来源网络,侵删)

The Command: python doesnotexist

This command consists of two parts:

  1. python: This is the command to start the Python interpreter.
  2. doesnotexist: This is an argument—a file name or a script name—that you are telling Python to run.

What Happens When You Run This?

When you type this command into your terminal (like Command Prompt on Windows, or Terminal on macOS/Linux) and press Enter, the following sequence of events occurs:

  1. The Shell Looks for python: Your operating system's shell (the program that reads your commands) first tries to find the python executable. It will look in its system's PATH environment variable to find where Python is installed.

    • If Python is installed and in your PATH, it will start the Python interpreter.
    • If Python is not installed or not in your PATH, you will see an error like 'python' is not recognized as an internal or external command... or zsh: command not found: python.
  2. Python Tries to Open doesnotexist: Assuming the first step succeeded, the Python interpreter now takes the next argument, doesnotexist, and tries to find and execute it as a script.

    Python不存在?为何仍有人使用它?-图2
    (图片来源网络,侵删)
  3. The Error Occurs: Python searches its current working directory and any other specified paths for a file named doesnotexist. Since this file does not exist, Python cannot run it.

The Resulting Error Message

You will almost certainly see an error message that looks like this:

Traceback (most recent call last):
  File "doesnotexist", line 1, in <module>
ModuleNotFoundError: No module named 'doesnotexist'

Or, on older versions of Python (2.x), it might be:

Traceback (most recent call last):
  File "doesnotexist", line 1, in <module>
ImportError: No module named doesnotexist

What This Error Means:

  • Traceback: This is Python's way of saying, "An error occurred, and here is the step-by-step history of what was happening when it happened."
  • File "doesnotexist", line 1: Python is telling you the error happened while trying to process the file doesnotexist. The line 1 part is a bit misleading here; it's a default placeholder because the file couldn't even be opened.
  • ModuleNotFoundError / ImportError: This is the core of the problem. Python tried to import the file as a module (a script), but since it couldn't find it, it raised an error.

How to Fix This (Common Scenarios)

The error means you're trying to run something that doesn't exist. Here are the most common reasons and how to solve them.

Python不存在?为何仍有人使用它?-图3
(图片来源网络,侵删)

Scenario 1: You Misspelled the Filename

This is the most likely cause. You might have intended to run a file named my_script.py or main.py.

Problem: You have a file named my_script.py in your current folder.

# Your folder contains:
# - my_script.py
# - some_other_file.txt
# You type this (incorrectly):
python my_script.py

You would get the same error because the name doesn't match.

Solution: Check the exact name of your file (including capitalization) and type it correctly.

# The correct command:
python my_script.py

Scenario 2: The File is Not in the Current Directory

Python, by default, only looks for files in the "current working directory" (the folder you are currently in).

Problem: Your script is located in a different folder.

# You are in your home directory: /Users/yourname/
# Your script is in your projects folder: /Users/yourname/projects/app.py
# You type this (from your home directory):
python app.py

You will get a ModuleNotFoundError because Python doesn't know where to find app.py.

Solution 1: Navigate to the correct directory first. Use the cd (change directory) command.

# First, go to the folder where the script is
cd /Users/yourname/projects
# Now, run the script from there
python app.py

Solution 2: Provide the full path to the file. You can tell Python exactly where the file is located.

# Run the script from your home directory
python /Users/yourname/projects/app.py

Scenario 3: You Meant to Install a Python Package

Sometimes, people confuse running a local script with installing a third-party library from the Python Package Index (PyPI). For example, you might have wanted to use a library named requests.

Problem: You want to use the requests library, but you haven't installed it yet. You mistakenly try to run it like a script.

# You type this, thinking it will install or run the 'requests' library
python requests

This will fail because requests is not a file in your directory; it's a package that needs to be installed.

Solution: Use pip (Python's package installer) to install the package first.

# Install the package
pip install requests
# Now you can import and use it inside a Python script or interactive session
# For example, in a file called get_data.py:
import requests
response = requests.get('https://api.github.com')
print(response.status_code)

Then you would run your own script:

python get_data.py
分享:
扫描分享到社交APP
上一篇
下一篇