问题描述
我是新手,请多多包涵.
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 启动脚本时,它似乎启动正常,然后立即失败并出现错误.
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:Windowssystem32> "%programdata%Anaconda3python.exe" "B:IcCharDataB1505_Process_Data_20190214.py"
我收到以下错误:
Traceback (most recent call last):
File "B:IcCharDataB1505_Process_Data_20190214.py", line 21, in <module>
import pandas as pd # Dataframe library
File "C:ProgramDataAnaconda3libsite-packagespandas\__init__.py", line 19, in <module>
"Missing required dependencies {0}".format(missing_dependencies))
ImportError: Missing required dependencies ['numpy']
下面是脚本的第一部分,带有实际的 #s 行,错误似乎源于此.互联网搜索无果.
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 Prompt 中运行:
(base) C:Users h>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:ProgramDataAnaconda3python.exe "C:/Users/xxx/Documents/script.py"
call C:/ProgramData/Anaconda3/Scripts/activate.bat C:/ProgramData/Anaconda3C:ProgramDataAnaconda3python.exe "C:/Users/xxx/Documents/script.py"
希望这会有所帮助...
Hope this helps...
这篇关于从 Windows 命令行启动 Python 脚本:脚本启动,然后失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!