为什么从CMD或Windows任务调度程序运行Python脚本时会出现这些导入错误,而不是Anaconda?

发布于 2025-01-25 17:19:34 字数 2559 浏览 2 评论 0原文

我概述了导入错误,但是只有在从CMD或Windows Task Scheduler运行Python脚本时(实际上与我假设的问题相同)。我已经研究了答案,并尝试了各种解决方案(下面详细介绍),但尚未奏效。无论如何,我都需要了解问题,以便将来可以管理类似的事情。

这是问题:

Windows10。AnacondaPython 3.9.7。虚拟环境。

如果我打开Anaconda提示,请激活虚拟环境并运行它,我的脚本可以正常工作。

但是,这是乐趣开始的地方。如果我尝试从非Anaconda CMD提示中运行脚本,请部署命令:“ C:\ Users \ User \ user \ anaconda3 \ enconda \ ency \ venv \ venv \ python.exe”“ c:\ user \ user \ user \ user \ scripts \ script.py.py”如果获取以下错误:

ImportError: DLL load failed while importing etree: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\lxml\html\__init__.py", line 53, in <module>
from ..import etree

这不像未安装的一个特定模块那样简单,因为当然会从Anaconda提示室中运行脚本,并且虚拟环境可行。当我运行其他脚本时,也会发生类似的情况。我看到的其他错误包括:例如:

ImportError: DLL load failed while importing _imaging: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\PIL\Image.py", line 114, in <module>
from . import _imaging as core

我认为这可能以某种方式相关。从虚拟环境中的python解释器内部导入numpy(1.22.3),但是当我尝试运行一个测试脚本以导入numpy的测试脚本时,它会从anaconda和cmd失败,并带有以下

ImportError: cannot import name SystemRandom

错误尝试从Windows任务调度程序运行各种脚本时,请使用Python“ C:\ Users \ User \ user \ Anaconda3 \ enconda3 \ enconda \ envs \ venv \ python.exe”作为程序/脚本和脚本“ script.py”输入的路径。争论。产生上述错误,然后通过从非Anaconda CMD运行脚本来复制。

我希望了解这里正在发生的事情,以及可以有效地从Windows任务调度程序中从虚拟环境中运行脚本的解决方案。


更新:

我使用Conda卸载并重新安装了Numpy(和Pandas)。这使VENV的numpy == 1.20.3(并且pandas = 1.4.2)。尝试重新运行其中一个脚本时,它可以从Anaconda的Venv内部运行良好,但是在尝试从CMD或Windows任务调度程序中运行时会产生以下错误:

ImportError: Unable to import required dependencies:
numpy:

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

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

We have complied 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.9 from "C:\Users\user\anaconda3\envs\venv\python.exe"
* The NumPy version is "1.20.3"

and make sure that they are the versions you expect.
Please carefull 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.

我已经研究了建议的解决方案,但是我仍然完全不知所措,尤其是关于脚本在一个地方从VENV运行的原因,而不是另一个地方。

I am encoutering import errors, but only when running my python scripts from cmd or windows task scheduler (effectively the same issue I assume). I have researched answers already and attempted various solutions (detailed below), but nothing has worked yet. I need to understand the problem in any case so that I can manage anything like it in the future.

Here is the issue:

Windows 10. Anaconda Python 3.9.7. Virtual enviromnent.

I have a script that works fine if I open an anaconda prompt, activate the virtual environment and run it.

However, this is where the fun starts. If I try to run the script from the non-anaconda cmd prompt deploying the commands: "C:\Users\user\anaconda3\envs\venv\python.exe" "C:\Users\user\scripts\script.py" if get the following error:

ImportError: DLL load failed while importing etree: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\lxml\html\__init__.py", line 53, in <module>
from ..import etree

This is not as simple as one specific module not being installed, because of course running the script from within the anaconda prompt and the virtual environment works. Similar also happens when I run other scripts. Other errors I have seen include, for example:

ImportError: DLL load failed while importing _imaging: The specified module could not be found.
Traceback includes:
"C:\Users\user\anaconda3\envs\venv\lib\site-packages\PIL\Image.py", line 114, in <module>
from . import _imaging as core

Also, I think this may be somehow related. Importing numpy (1.22.3) from within the python interpreter in the virtual environment works fine, but when I try to run a test script that imports numpy it fails both from anaconda and the cmd with the following error:

ImportError: cannot import name SystemRandom

The oveall issue was noted originally when trying to run various scripts from Windows Task Scheduler with the path to python "C:\Users\user\anaconda3\envs\venv\python.exe" entered as the Program/script and the script "script.py" entered as an argument. The above errors were produced, then reproduced by running the scripts from a non-anaconda cmd.

I am looking to understand what is happening here and for a solution that can get the scripts running from the virtual enviroment from Windows Task Scheduler effectively.


Update:

I have uninstalled and reinstalled numpy (and pandas) using conda. This has left the venv with numpy==1.20.3 (and pandas=1.4.2). On attempting to re-run one of the scripts, it runs fine from within the venv in anaconda, but produces the following error when attempting to run from cmd or from within Windows Task Scheduler as above:

ImportError: Unable to import required dependencies:
numpy:

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

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

We have complied 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.9 from "C:\Users\user\anaconda3\envs\venv\python.exe"
* The NumPy version is "1.20.3"

and make sure that they are the versions you expect.
Please carefull 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.

I have looked into the solutions suggested, but am still completely at a loss, especially as to why the script runs from the venv in one place, but NOT the other.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2025-02-01 17:19:34

当未设置Anaconda的环境变量时,出现了错误。检查路径中是否添加了3个环境变量,请按照以下步骤

:转到 Windows &gt; 您帐户的编辑环境变量

b。到路径,单击编辑

c。然后通过单击 new ,根据安装Anaconda

”

X:\ProgramData\Anaconda3  
X:\ProgramData\Anaconda3\Scripts
X:\ProgramData\Anaconda3\Library\bin

The error arises when the environment variables are not setup for Anaconda. Check if you have added 3 environment variable in your PATH, follow the below steps:

a. Go to Windows> Edit environment variables for your account

b. Got to PATH, Click Edit
enter image description here

c. Then add 3 paths to your Anaconda Directory by clicking New, change it based on where you installed anaconda

enter image description here

X:\ProgramData\Anaconda3  
X:\ProgramData\Anaconda3\Scripts
X:\ProgramData\Anaconda3\Library\bin
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文