title: Why are these import errors occurring when running python scripts from cmd or windows task scheduler, but not anaconda?
body: <p>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.</p>
  <p>Here is the issue:</p>
  <p>Windows 10.  Anaconda Python 3.9.7.  Virtual enviromnent.</p>
  <p>I have a script that works fine if I open an anaconda prompt, activate the virtual environment and run it.</p>
  <p>However, this is where the fun starts.  If I try to run the script from the non-anaconda cmd prompt deploying the commands: &quot;C:\Users\user\anaconda3\envs\venv\python.exe&quot; &quot;C:\Users\user\scripts\script.py&quot; if get the following error:</p>
  <pre><code>ImportError: DLL load failed while importing etree: The specified module could not be found.
  Traceback includes:
  &quot;C:\Users\user\anaconda3\envs\venv\lib\site-packages\lxml\html\__init__.py&quot;, line 53, in &lt;module&gt;
  from ..import etree
  </code></pre>
  <p>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:</p>
  <pre><code>ImportError: DLL load failed while importing _imaging: The specified module could not be found.
  Traceback includes:
  &quot;C:\Users\user\anaconda3\envs\venv\lib\site-packages\PIL\Image.py&quot;, line 114, in &lt;module&gt;
  from . import _imaging as core
  </code></pre>
  <p>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:</p>
  <pre><code>ImportError: cannot import name SystemRandom
  </code></pre>
  <p>The oveall issue was noted originally when trying to run various scripts from Windows Task Scheduler with the path to python &quot;C:\Users\user\anaconda3\envs\venv\python.exe&quot; entered as the Program/script and the script &quot;script.py&quot; entered as an argument.  The above errors were produced, then reproduced by running the scripts from a non-anaconda cmd.</p>
  <p>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.</p>
  <hr />
  <p>Update:</p>
  <p>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:</p>
  <pre><code>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 &quot;C:\Users\user\anaconda3\envs\venv\python.exe&quot;
  * The NumPy version is &quot;1.20.3&quot;
  
  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.
  </code></pre>
  <p>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.</p>