1

I'm a rank novice, please bear with me.

I've inherited a python script from another engineer. For convenience, I want to be able to launch the script from a Windows bat file, but initially am trying to debug by running from Windows command line.

Whenever I start the script from CMD, it seems to start OK and then immediately fails with errors.

My environment: Windows7 Pro and Windows10 Pro (same errors occur), Anaconda 3.7 , Spyder 3.3.2

When I run the script from inside Spyder, script runs fine, no errors.

When I try running from Windows CMD:

  C:\Windows\system32>  "%programdata%\Anaconda3\python.exe"   "B:\IcCharData\B1505_Process_Data_20190214.py"

I get these errors:

Traceback (most recent call last):
  File "B:\IcCharData\B1505_Process_Data_20190214.py", line 21, in <module>
    import pandas as pd # Dataframe library
  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\__init__.py", line 19, in <module>
    "Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']

Below is the first part of the script, with actual line #s, where errors seem to be originating. Internet searches have been fruitless.

What could be the problem(s)?
Again, note the script runs fine from inside Spyder

[snipped some irrelevant comments]

20  # Load the necessary libraries
21  import pandas as pd # Dataframe library
22  import numpy as np # Numeric library
23  import glob # Files related
24  import os # Operating System related
25  import sys #Operating System related
26  import re # regular expression related
27  import sqlite3 # database
28  import datetime
29  import subprocess # for running external programs like JMP from python
30  import logging # enables logging to both screen and a file
31  import statsmodels.api as sm # Modeling library used for linear regression

33  # Logging settings
34  logfilename = "./3_OutputData/B1505_Data_Process_Log_" + datetime.datetime.now().strftime("%Y-%m-%d-%H-%M") + '.txt'
35  level = logging.INFO
36  format = '  %(message)s'
37  handlers = [logging.FileHandler(logfilename), logging.StreamHandler()]
38  logging.basicConfig(level = level, format = format, handlers = handlers)

[snipped remaining 300+ lines of code]

ADDED on 2019-02-24, in response to AJNeufeld's comment:

Ran in Spyder :

import sys
print(sys.path)

runfile('B:/Desktop/untitled0.py', wdir='B:/Desktop')    # TH: apparently because spyder prompted me to save the script here#
[
'C:\\Users\\th',                 # TH: line not present with Anaconda Prompt#
'C:\\ProgramData\\Anaconda3\\python37.zip', 
'C:\\ProgramData\\Anaconda3\\DLLs', 
'C:\\ProgramData\\Anaconda3\\lib', 
'C:\\ProgramData\\Anaconda3', 
'', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\IPython\\extensions', # TH: line not present with Anaconda Prompt#
'C:\\Users\\th\\.ipython'                       # TH: line not present with Anaconda Prompt#
]

Ran in Anaconda Prompt:

(base) C:\Users\th>python
Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.

>>> import sys
>>> print(sys.path)

[
'', 
'C:\\ProgramData\\Anaconda3\\python37.zip', 
'C:\\ProgramData\\Anaconda3\\DLLs', 
'C:\\ProgramData\\Anaconda3\\lib', 
'C:\\ProgramData\\Anaconda3',
'C:\\ProgramData\\Anaconda3\\lib\\site-packages', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\win32\\lib', 
'C:\\ProgramData\\Anaconda3\\lib\\site-packages\\Pythonwin'
]
9
  • Can you check the answers at Installing NumPy via Anaconda in Windows if any applies to your case? Commented Feb 24, 2019 at 1:29
  • That doesn't seem relevant, to my novice eyes. Note that I have only one python install, the "full" Anaconda 3.7 installed a couple weeks ago. It should have all the required packages, and I've also updated with command "conda update anaconda" Commented Feb 24, 2019 at 1:54
  • Could you print(sys.path) from a script both inside Spyder and from Windows CMD? I suspect they will be different, indicating different environments based on the launch method. Commented Feb 24, 2019 at 4:51
  • Can you try running: C:\Windows\system32> "%programdata%\Anaconda3\python.exe" -c "import numpy as np; print(np.__version__)" Commented Feb 24, 2019 at 7:59
  • @Justin Ezequiel, ok I copy/pasted your request into CMD, what do we learn from the result? It generated more &different types of errors. Unfortunately the error list is too long for me to paste here, comments don't allow it. Commented Feb 24, 2019 at 20:41

3 Answers 3

1

Your batch file needs to be like the following if you want it to work:

call C:/ProgramData/Anaconda3/Scripts/activate.bat C:/ProgramData/Anaconda3 C:\ProgramData\Anaconda3\python.exe "C:/Users/xxx/Documents/script.py"

Hope this helps...

Sign up to request clarification or add additional context in comments.

2 Comments

Jonathan -- IT WORKS! Thank you so much. I would never have guessed this in a hundred years. Can you please explain WHY it works? (I gave you an "up" vote but it doesnt show up, because I dont have sufficient "reputation")
Welcome @TomH Yes with a bit of trial and error. I believe that there are some path related issues and therefore you would need to specify all the executable paths specifically.
0

I'm pretty sure (from the information given in the import error), that the only thing you have to do is import Numpy before you import pandas. You can do this by switching lines 22 and 21.

6 Comments

The script runs OK from inside the Spyder IDE, why would it "break" when invoked from windows command line?
This shouldn't make a difference. The import order really does not matter.
@Tomothy32 It does matter because, in the ImportError, it shows that Pandas needs the dependency of Numpy, so you can get that dependency by switching the lines.
@TomH Maybe Spyder IDE already has the dependency of Numpy so Pandas is able to run properly
@ArnavPoddar I think you have fundamentally misunderstood imports. Imports are for the current file. Open a fresh file/interactive window, type in import pandas as pd, and it will work if you have all dependencies installed. The dependencies don't have to be imported.
|
0

Have you at least tried conda install numpy as it seems your Anaconda installation does not include numpy. (Can somebody turn this into a comment? Thanks.)

1 Comment

"numpy", "numpy-base", "numpydoc" are already installed.Sorry, this reply isn't formatting correctly and is difficult to read: (base) C:\Users\th>conda list # packages in environment at C:\ProgramData\Anaconda3: # # Name Version Build Channel [snipped] numpy 1.15.4 py37h19fb1c0_0 numpy-base 1.15.4 py37hc3f5095_0 numpydoc 0.8.0 py37_0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.