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

Win10系统Python3.10环境下运行含numpy的.py文件报错,但命令行交互模式正常的问题求助

Fix for Numpy Import Error in Python 3.10 (Windows 10)

Hey Pavel, I’ve run into similar weird environment mismatches before—let’s break down what’s going on and fix this step by step. The key clue here is that numpy works in the interactive shell but fails in a script, which points to a Python environment mismatch or standard library interference, not an issue with numpy itself.

Step 1: Check for Conflicting Filenames

First, make sure you don’t have any files in your script directory (C:\TESTY\PY4E) that share names with Python’s standard library modules. For example:

  • Do you have an os.py or socket.py file here?
  • Also check for compiled .pyc versions of these files.

If you find any, delete them immediately. Python prioritizes files in the current working directory over the system’s standard library, so having a custom os.py would break all imports of the real os module.

Step 2: Ensure You’re Running the Correct Python Version

Sometimes multiple Python versions are installed on Windows, and the python command might point to a different version than the interactive shell.

  1. Open Command Prompt and run:
    python --version
    
  2. Compare this to the version you see when you launch the interactive shell (it should say Python 3.10.x).
  3. If they don’t match, run your script explicitly with Python 3.10:
    py -3.10 numpy_test.py
    
    The py launcher is the safest way to target specific Python versions on Windows.

Step 3: Repair Python’s Standard Library and Numpy

Your error traces show issues with platform.py and the socket module, which suggests either a corrupted standard library or a broken numpy installation:

  1. Force-reinstall numpy to ensure it’s properly linked to your Python 3.10 installation:
    py -3.10 -m pip install --force-reinstall numpy
    
  2. If that doesn’t work, check if your platform.py file is modified. Navigate to C:\Users\pawel\AppData\Local\Programs\Python\Python310\lib\platform.py and verify the uname() function has fallback logic for Windows (since os.uname() doesn’t exist on Windows). If the file looks tampered with, do a clean reinstall of Python 3.10:
    • Uninstall Python via Settings > Apps > Apps & Features.
    • Delete the Python310 folder at C:\Users\pawel\AppData\Local\Programs\Python.
    • Delete the Python310 folder at C:\Users\pawel\AppData\Roaming\Python.
    • Download the official Python 3.10 installer, check "Add Python to PATH", and do a full installation.

Step 4: Test a Minimal Script to Isolate the Issue

Create a tiny script called test_env.py with this content:

import os
import socket
print("OS module name:", os.name)
print("Hostname:", socket.gethostname())

Run it with py -3.10 test_env.py. If this throws the same AttributeError, the problem is definitely with your Python standard library, not numpy. In that case, the clean reinstall from Step 3 is your best bet.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 11:32:30