繁体   English   中英

在Python中执行时检查并安装wget软件包

[英]Check and Install wget package at time of execution in Python

我正在尝试使用以下代码安装wget软件包。 目的是检查软件包是否已安装。 如果未安装,请在执行代码时自动下载并安装。 以下代码适用于pandas软件包,但不适用于wget。

import pip

# Define function to install missing modules/packages

def install(package):
    pip.main(['install', package])

# Call function and install missing module/package

if __name__ == '__main__':
    install('pandas')
    install('wget')

# Below is the exception message received for wget.

[31mException:
Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "C:\Python36\lib\site-packages\pip\commands\install.py", line 324, in run
    requirement_set.prepare_files(finder)
  File "C:\Python36\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
    ignore_dependencies=self.ignore_dependencies))
  File "C:\Python36\lib\site-packages\pip\req\req_set.py", line 634, in _prepare_file
    abstract_dist.prep_for_dist()
  File "C:\Python36\lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
    self.req_to_install.run_egg_info()
  File "C:\Python36\lib\site-packages\pip\req\req_install.py", line 439, in run_egg_info
    command_desc='python setup.py egg_info')
  File "C:\Python36\lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
    line = console_to_str(proc.stdout.readline())
  File "C:\Python36\lib\site-packages\pip\compat\__init__.py", line 73, in console_to_str
    return s.decode(sys.__stdout__.encoding)
AttributeError: 'NoneType' object has no attribute 'encoding'[0m

知道可能是什么问题吗? 我在Windows 7 x64位操作系统上使用Python 3.6.4。

我想从外部脚本安装它能正常工作吗? 我尝试了您的代码,它正在工作。 我试图找出发生了什么,有两种可能性:

  1. 您安装了错误的python版本(也许是x64 <> x86)? 尝试重新安装
  2. 您的python安装已中断,因此无论如何都要尝试重新安装。 这确实是一个奇怪的问题,我也看不出明确的原因,无论如何,您应该考虑将pip3用于python3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM