小编典典

从Windows命令行启动Python脚本:脚本启动,然后失败

python

我是高级新手,请多多包涵。

我已经从另一位工程师那里继承了python脚本。为了方便起见,我希望能够从Windows
bat文件启动脚本,但最初尝试通过从Windows命令行运行进行调试。

每当我从CMD启动脚本时,它似乎都可以启动,然后立即因错误而失败。

我的环境:Windows7 Pro和Windows10 Pro(发生相同的错误),Anaconda 3.7,Spyder 3.3.2

当我从Spyder 内部 运行脚本时,脚本运行正常,没有错误。

当我尝试从Windows CMD运行时:

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

我得到这些错误:

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']

以下是脚本的第一部分,带有实际的#号行,其中似乎是由错误引起的。互联网搜索一无所获。

可能是什么问题?
再次注意,脚本可以从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]

已于2019-02-24添加,以回应AJNeufeld的评论:

在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#
]

在Anaconda提示中跑:

(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'
]

阅读 182

收藏
2021-01-20

共1个答案

小编典典

如果要使其工作,则批处理文件必须类似于以下内容:

调用C:/ProgramData/Anaconda3/Scripts/activate.bat C:/ ProgramData / Anaconda3
C:\ ProgramData \ Anaconda3 \ python.exe“ C:/Users/xxx/Documents/script.py”

希望这可以帮助…

2021-01-20