问题描述
我是高级新手,请多多包涵.
I'm a rank novice, please bear with me.
我已经从另一位工程师那里继承了python脚本.为了方便起见,我希望能够从Windows bat文件启动脚本,但最初尝试通过从Windows命令行运行来进行调试.
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.
每当我从CMD启动脚本时,它似乎都启动OK,然后立即因错误而失败.
Whenever I start the script from CMD, it seems to start OK and then immediately fails with errors.
我的环境: Windows7 Pro和Windows10 Pro(发生相同的错误), Anaconda 3.7,Spyder 3.3.2
My environment: Windows7 Pro and Windows10 Pro (same errors occur), Anaconda 3.7 , Spyder 3.3.2
当我从 内部 Spyder运行脚本时,脚本运行正常,没有错误.
When I run the script from inside Spyder, script runs fine, no errors.
当我尝试从Windows CMD运行时:
When I try running from 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']
下面是脚本的第一部分,带有实际的#号行,其中似乎是由错误引起的.互联网搜索一无所获.
Below is the first part of the script, with actual line #s, where errors seem to be originating.Internet searches have been fruitless.
可能是什么问题?
再次注意,脚本可以从Spyder内部正常运行
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]
已于2019-02-24添加,以回应AJNeufeld的评论:
ADDED on 2019-02-24, in response to AJNeufeld's comment:
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'
]
推荐答案
您的批处理文件必须如下所示:
Your batch file needs to be like the following if you want it to work:
调用C:/ProgramData/Anaconda3/Scripts/activate.bat C:/ProgramData/Anaconda3C:\ ProgramData \ Anaconda3 \ python.exe"C:/Users/xxx/Documents/script.py"
call C:/ProgramData/Anaconda3/Scripts/activate.bat C:/ProgramData/Anaconda3C:\ProgramData\Anaconda3\python.exe "C:/Users/xxx/Documents/script.py"
希望这对您有帮助...
Hope this helps...
这篇关于从Windows命令行启动Python脚本:脚本启动,然后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!