(学习记录)Python3通过pip安装扩展时出现:PermissionError 的解决办法

本人在学习python3,flask框架,使用flask-sqlalchemy进行数据库操作,在下载安装flask-sqlalchemy时出现问题

在win10终端执行命令 pip3 install -i https://pypi.douban.com/simple flask-sqlalchemy

但是安装过程一直出现 PermissionError 错误。

具体错误如下:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "d:\python\virtualenv\flask\lib\site-packages\pip\basecommand.py", line 215, in main
    status = self.run(options, args)
  File "d:\python\virtualenv\flask\lib\site-packages\pip\commands\install.py", line 385, in run
    requirement_set.cleanup_files()
  File "d:\python\virtualenv\flask\lib\site-packages\pip\utils\build.py", line 38, in __exit__
    self.cleanup()
  File "d:\python\virtualenv\flask\lib\site-packages\pip\utils\build.py", line 42, in cleanup
    rmtree(self.name)
  File "d:\python\virtualenv\flask\lib\site-packages\pip\_vendor\retrying.py", line 49, in wrapped_f
    return Retrying(*dargs, **dkw).call(f, *args, **kw)
  File "d:\python\virtualenv\flask\lib\site-packages\pip\_vendor\retrying.py", line 212, in call
    raise attempt.get()
  File "d:\python\virtualenv\flask\lib\site-packages\pip\_vendor\retrying.py", line 247, in get
    six.reraise(self.value[0], self.value[1], self.value[2])
  File "d:\python\virtualenv\flask\lib\site-packages\pip\_vendor\six.py", line 686, in reraise
    raise value
  File "d:\python\virtualenv\flask\lib\site-packages\pip\_vendor\retrying.py", line 200, in call
    attempt = Attempt(fn(*args, **kwargs), attempt_number, False)
  File "d:\python\virtualenv\flask\lib\site-packages\pip\utils\__init__.py", line 102, in rmtree
    οnerrοr=rmtree_errorhandler)
  File "d:\python\virtualenv\flask\lib\shutil.py", line 494, in rmtree
    return _rmtree_unsafe(path, onerror)
  File "d:\python\virtualenv\flask\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "d:\python\virtualenv\flask\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "d:\python\virtualenv\flask\lib\shutil.py", line 384, in _rmtree_unsafe
    _rmtree_unsafe(fullname, onerror)
  File "d:\python\virtualenv\flask\lib\shutil.py", line 393, in _rmtree_unsafe
    onerror(os.rmdir, path, sys.exc_info())
  File "d:\python\virtualenv\flask\lib\site-packages\pip\utils\__init__.py", line 114, in rmtree_errorhandler
    func(path)
PermissionError: [WinError 32] 另一个程序正在使用此文件,进程无法访问。: 'C:\\Users\\cjs\\AppData\\Local\\Temp\\pip-build-mm86bqui\\SQLAlchemy\\build\\bdist.win32\\wheel'

之前也出现过这个错误,但是重新执行命令的又可以了(刚学,不知道为什么)

但这次一直执行都不行,于是就网上搜解决办法;

通过下面这篇文章中https://blog.csdn.net/qq_33530388/article/details/68933201,找到了解决办法;

原来是编码问题。

解决方案:

打开D:\Python\virtualenv\flask\Lib\site-packages\pip\compat\__init__.py文件(具体路径根据自己的环境安装而定);

找到

if sys.version_info >= (3,):
    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            return s.decode('utf-8')

修改为:

if sys.version_info >= (3,):
    def console_to_str(s):
        try:
            return s.decode(sys.__stdout__.encoding)
        except UnicodeDecodeError:
            return s.decode('cp936')

即 把 utf-8 改为 cp936。

原因: 

编码问题,虽然py3统一用utf-8了。但win下的终端显示用的还是gbk编码。

以上解决方法是参考https://blog.csdn.net/qq_33530388/article/details/68933201这篇文章,感谢这位大哥。我在此只是为了记录学习过程中出现的问题。

你可能感兴趣的:(Python学习-flask)