Check and Install wget package at time of execution in Python

时间:2018-01-15 18:19:10

标签: python python-3.x pip

I am trying to install the wget package using the below code. The objective is to check if a package has already been installed. If not installed, then download and install it automatically at the time of execution of code. The below code works for the pandas package but it fails for 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

Any idea what could be the problem? I am using Python 3.6.4 with Windows 7 x64 bit OS.

1 个答案:

答案 0 :(得分:0)

我想从外部脚本安装它是有效的吗? 我尝试了你的代码,它正在运行。 我试图找到发生的事情,有两种可能性:

  1. 您安装了错误的python版本(可能是x64<> x86)?尝试重新安装
  2. 你已经破解了python安装,所以无论如何都要尝试重新安装它。 这是一个非常奇怪的问题,我没有看到明确的原因 无论如何,你应该考虑使用pip3 for python3