You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac OSX执行Python脚本遇Error [2]:找不到文件或目录

Hey Sam, let's get that hello.py script running—this is a typical file path issue on macOS, and it's easy to fix once we straighten out the details!

Why you're seeing the "No such file or directory" error

When you run python hello.py in Terminal, the shell looks for hello.py only in your current working directory. If you're not in the folder where the file lives, or if you're typing an incomplete/incorrect path, Terminal can't find it. Your previous attempts used either folder paths instead of the file itself, or truncated paths that didn't point directly to hello.py.

Step-by-step fixes

Let's go through the most reliable ways to resolve this:

  1. First, confirm the exact path to hello.py
    Open Terminal and run this command to locate the file automatically:

    find ~/Documents -name "hello.py"
    

    This will spit out a full path like /Users/sam/Documents/Python/hello.py—that's the exact location we need to use.

  2. Option 1: Navigate to the file's folder first

    • Use the cd command to move into the directory where hello.py is stored. Using the path from step 1, that would be:
      cd ~/Documents/Python
      
    • Now you can run the script directly:
      python hello.py
      
      Note: If you get a "command not found" error for python, try python3 instead—macOS defaults to Python 2 for the python command, but most modern tutorials use Python 3.
  3. Option 2: Run the script using its full path
    If you don't want to navigate folders, you can run the script by typing its complete path right after python (or python3):

    python ~/Documents/Python/hello.py
    

Quick note on your previous attempts

  • python ~/Users/sam/Documents/Python/Google points to a folder, not the hello.py file—Terminal can't run a folder as a script!
  • python exercises/google-python-exercise... uses a truncated path, so Terminal can't resolve where the actual hello.py is located. Always use the full, exact path to the file.

Give these steps a try, and you should have that script running in no time!

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:30:26