问题
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'
]
回答1:
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.
回答2:
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.)
回答3:
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...
来源:https://stackoverflow.com/questions/54847871/launching-python-script-from-windows-command-line-script-starts-then-fails