python 尝试导入python中系统可能无法使用的包
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 尝试导入python中系统可能无法使用的包相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
# @Author: codykochmann
# @Date: 2016-09-26 14:29:17
# @Last Modified 2016-09-26
# @Last Modified time: 2016-09-26 16:37:43
# the basic bitch version
#------------------------------------------------------------------------------
def attempt_import(packagename):
exec("global {}".format(packagename))
try: exec("import {}".format(packagename))
except Exception, e:
print("""\nerror: There was a problem with the package: {}""".format(packagename))
print("message: {}\n".format(e))
exit()
# attempt_import("scipy")
# print scipy.__version__
# exit()
# the AI version
#------------------------------------------------------------------------------
from os import popen, system, getpid
import re
from time import sleep
# confirms that pip is installed
#------------------------------------------------------------------------------
pip_version_regex = r'pip\ {1,}[0-9\.]{3,}'
pip_v = lambda: popen('pip --version').read()
pip_installed = lambda: (pip_v()).startswith(re.findall(pip_version_regex,pip_v())[0])
# pip search functions
#------------------------------------------------------------------------------
pkg_regex = r'([a-zA-Z0-9\.\_\-]{1,}\ \([0-9\.]{1,}\)\ {1,})\-'
pkg_name_regex = r'[a-zA-Z0-9\.\_\-]{1,}'
# exports the package name from the string
pkg_name = lambda s: re.findall(pkg_name_regex,s)[0].split(' ')[0]
# confirms the string is a package from pip's search
is_pkg = lambda s: s.startswith(pkg_name(s)) if (' ' in s and '-' in s) else False
# collects the package names from pip's search output
pip_filter_packages = lambda i: [ pkg_name(p) for p in i.split('\n') if is_pkg(p) ]
# returns a list of packages from pip's search
pip_search = lambda packagename: pip_filter_packages("{}".format(popen("pip search {}".format(packagename)).read()))
# checks if a pip package is available
available_in_pip = lambda p: p in pip_search(p)
# pip install function
#------------------------------------------------------------------------------
# installs a package if pip is installed
pip_install = lambda packagename: system("sudo pip install {}".format(packagename))
# restart modules
#------------------------------------------------------------------------------
# gets the full command running this process
get_running_command = lambda: popen('ps -ax | grep {} | head -1'.format(getpid())).read()[27:]
# restarts the running command
restart_script = lambda: system(get_running_command())
#restart_script = lambda: exit("please restart the script now") #system(get_running_command())
def attempt_import(packagename, autoinstall=False):
try: exec("global {};import {}".format(packagename,packagename))
except Exception, e:
print("""\nerror: There was a problem with the package: {}""".format(packagename))
print("message: {}".format(e))
sleep(2)
if autoinstall and 'o module named ' in str(e):
e=str(e)
package_to_install = e.split("o module named ")[1]
print("searching for remote package of: {}".format(package_to_install))
if pip_installed() and available_in_pip(package_to_install):
print("found it.")
sleep(2)
print("attempting install using: sudo pip install {}".format(package_to_install))
sleep(2)
pip_install(package_to_install)
restart_script()
exit()
# Test code below
#------------------------------------------------------------------------------
attempt_import("scipy", autoinstall=True)
print("scipy has been automatically installed at version: {}".format(scipy.__version__))
以上是关于python 尝试导入python中系统可能无法使用的包的主要内容,如果未能解决你的问题,请参考以下文章
python错误:无法从'cupy.core'导入名称'core'
无法从 python 中的 pigutil 导入 outputSchema