gpt4 book ai didi

python - 从 Windows 命令行 : script starts, 启动 Python 脚本失败

转载 作者:太空宇宙 更新时间:2023-11-03 21:14:03 24 4
gpt4 key购买 nike

我是新手,请多多包涵。

我从另一位工程师那里继承了一个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 年 2 月 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'
]

最佳答案

如果您希望它工作,您的批处理文件需要如下所示:

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

希望这有帮助...

关于python - 从 Windows 命令行 : script starts, 启动 Python 脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54847871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com