首页 > 解决方案 > 使用 python 可执行文件从 Windows 命令行运行 .py 文件时出现 Numpy 导入错误

问题描述

我最近编写了一些代码来处理一些我需要每天同时完成的数据。因此,我研究了各种方法并尝试使用 Windows 任务调度程序来实现这一点。对于“启动程序”字段,我在计算机 C:\Users...\Anaconda3\python.exe 中输入了 python.exe 文件的路径,并在“添加参数”字段中输入了 . py 文件我想每天运行“C:\Users........\daily_python_file.py”。这不起作用,所以我尝试使用在任务计划程序字段中输入的相同路径从命令行运行 .py 文件,并收到以下错误:

  from . import _distributor_init
Traceback (most recent call last):
  File "C:\Users\...\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
    from . import multiarray
  File "C:\Users\...\Anaconda3\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
    from . import overrides
  File "C:\Users\...\Anaconda3\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
    from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\...\...\...\...\...\...\daily_python_file.py", line 7, in <module>
    import numpy as np
  File "C:\Users\...\Anaconda3\lib\site-packages\numpy\__init__.py", line 140, in <module>
    from . import core
  File "C:\Users\...\Anaconda3\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
    raise ImportError(msg)
ImportError:

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "C:\Users\...\Anaconda3\python.exe"
  * The NumPy version is: "1.19.2"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.

从 anaconda 提示符命令行运行 .py 文件时,它运行良好。我还创建了一个测试 python 文件,它只包含一个打印函数,从 Windows 命令行运行没有问题,(我假设因为这个 .py 文件没有依赖项(即 numpy、pandas 等)。我知道它可能与模块的安装位置(哪个 anaconda 环境)有关,但我不确定如何将其转化为解决方案。网上也有一些与此类似的问题,但我还没有找到任何具体的解决方案。

对于某些背景,我正在运行 Windows 10,并且在 anaconda 基础环境 python 3.8.5、numpy 1.19.2 中。

对此问题的任何帮助将不胜感激。

更新

我设法使用 .bat 文件解决了这个问题。我知道一旦 anaconda 基础环境被激活,那么 python.exe 文件就会运行有问题的 .py 文件,所以我编译了一个 .bat 如下:

call C:\Users\Stanley\Anaconda3\Scripts\activate base
C:\Users\...\Anaconda3\python.exe "C:\Users\...\...\...\...\...\...\daily_python_file.py"

然后使用任务调度程序每天运行它。调用线激活基础环境,然后python文件运行没有问题。这并没有说明为什么它以前不起作用,但是它确实提供了一个很好的选择。

标签: pythonnumpyanacondaexeimporterror

解决方案


推荐阅读