ubuntu16.04 安装python虚拟环境

ubuntu16.04 安装python虚拟环境


使用Python中,不同项目使用的包所对应的版本可能各不相同,因此有必要对项目进行虚拟环境的搭建。并且当我们需要导出该项目的依赖时,如果只有一个环境,那么可能会导出很多该项目所不需要的依赖包。因此有必要学习几安装Python的虚拟环境。

ubuntu16.04 的虚拟环境的安装步骤可参考如下链接,不再赘述。
Ubuntu16.04中python虚拟环境安装配置

2021-08-17:本次新装的为:ubuntu16.04.7。
本次新建过程中主要遇到以下2个问题:
1、在运行

source ~/.bashrc

报错如下:

koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ source ~/.bashrc
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/stevedore/_cache.py", line 28, in 
    import importlib.metadata as importlib_metadata
ImportError: No module named 'importlib.metadata'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/dist-packages/virtualenvwrapper/hook_loader.py", line 16, in 
    from stevedore import ExtensionManager
  File "/usr/local/lib/python3.5/dist-packages/stevedore/__init__.py", line 11, in 
    from .extension import ExtensionManager
  File "/usr/local/lib/python3.5/dist-packages/stevedore/extension.py", line 19, in 
    from . import _cache
  File "/usr/local/lib/python3.5/dist-packages/stevedore/_cache.py", line 31, in 
    import importlib_metadata
  File "/usr/local/lib/python3.5/dist-packages/importlib_metadata/__init__.py", line 56
    return f"No package metadata was found for {self.name}"
                                                          ^
SyntaxError: invalid syntax
virtualenvwrapper.sh: There was a problem running the initialization hooks.

If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3 and that PATH is
set properly.

代码报错显示,No module named ‘importlib.metadata’。由于我使用的时python3.5,参照网上建议安装了

importlib-metadata==1.7.0

之后source ~/.bashrc执行成功。

2、在创建虚拟环境时报错

koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ mkvirtualenv -p /usr/bin/python3.5 py35
Traceback (most recent call last):
  File "/usr/local/bin/virtualenv", line 7, in 
    from virtualenv.__main__ import run_with_catch
  File "/usr/local/lib/python3.5/dist-packages/virtualenv/__init__.py", line 3, in 
    from .run import cli_run, session_via_cli
  File "/usr/local/lib/python3.5/dist-packages/virtualenv/run/__init__.py", line 7, in 
    from ..app_data import make_app_data
  File "/usr/local/lib/python3.5/dist-packages/virtualenv/app_data/__init__.py", line 9, in 
    from platformdirs import user_data_dir
  File "/usr/local/lib/python3.5/dist-packages/platformdirs/__init__.py", line 27
    result: Type[PlatformDirsABC] = getattr(importlib.import_module(module), name)
          ^
SyntaxError: invalid syntax

该问题在网上搜索了很久未搜索到,看到该报错是在virtualenv中运行时导致的报错,因此考虑是不是virtualenv的版本过新所导致的。因此在卸载了virtualenv之后,重新安装了一个低版本的virtualenv,之后便可以成功创建虚拟环境
过程如下:

koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ sudo pip3 install virtualenv==
The directory '/home/koping/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/koping/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv==
  Could not find a version that satisfies the requirement virtualenv== (from versions: 0.8, 0.8.1, 0.8.2, 0.8.3, 0.8.4, 0.9, 0.9.1, 0.9.2, 1.0, 1.1, 1.2, 1.3, 1.3.1, 1.3.2, 1.3.3, 1.3.4, 1.4rc1, 1.4, 1.4.1, 1.4.2, 1.4.3, 1.4.4, 1.4.5, 1.4.6, 1.4.7, 1.4.8, 1.4.9, 1.5, 1.5.1, 1.5.2, 1.6, 1.6.1, 1.6.2, 1.6.3, 1.6.4, 1.7, 1.7.1, 1.7.1.1, 1.7.1.2, 1.7.2, 1.8, 1.8.1, 1.8.2, 1.8.3, 1.8.4, 1.9, 1.9.1, 1.10, 1.10.1, 1.11, 1.11.1, 1.11.2, 1.11.3, 1.11.4, 1.11.5, 1.11.6, 12.0, 12.0.1, 12.0.2, 12.0.4, 12.0.5, 12.0.6, 12.0.7, 12.1.0, 12.1.1, 13.0.0, 13.0.1, 13.0.2, 13.0.3, 13.1.0, 13.1.1, 13.1.2, 14.0.0, 14.0.1, 14.0.2, 14.0.3, 14.0.4, 14.0.5, 14.0.6, 15.0.0, 15.0.1, 15.0.2, 15.0.3, 15.1.0, 15.2.0, 16.0.0, 16.1.0, 16.2.0, 16.3.0, 16.3.1.dev0, 16.4.0, 16.4.1, 16.4.3, 16.4.4.dev0, 16.5.0, 16.6.0, 16.6.1, 16.6.2, 16.7.0, 16.7.1, 16.7.2, 16.7.3, 16.7.4, 16.7.5, 16.7.6, 16.7.7, 16.7.8, 16.7.9, 16.7.10, 16.7.11, 16.7.12, 20.0.0b1, 20.0.0b2, 20.0.0, 20.0.1, 20.0.2, 20.0.3, 20.0.4, 20.0.5, 20.0.6, 20.0.7, 20.0.8, 20.0.9, 20.0.10, 20.0.11, 20.0.12, 20.0.13, 20.0.14, 20.0.15, 20.0.16, 20.0.17, 20.0.18, 20.0.19, 20.0.20, 20.0.21, 20.0.22, 20.0.23, 20.0.24, 20.0.25, 20.0.26, 20.0.27, 20.0.28, 20.0.29, 20.0.30, 20.0.31, 20.0.32, 20.0.33, 20.0.34, 20.0.35, 20.1.0, 20.2.0, 20.2.1, 20.2.2, 20.3.0, 20.3.1, 20.4.0, 20.4.1, 20.4.2, 20.4.3, 20.4.4, 20.4.5, 20.4.6, 20.4.7, 20.5.0, 20.6.0, 20.7.0, 20.7.1, 20.7.2)
No matching distribution found for virtualenv==
You are using pip version 8.1.1, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ sudo pip3 install virtualenv==15.0.0
The directory '/home/koping/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/koping/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting virtualenv==15.0.0
  Downloading https://files.pythonhosted.org/packages/5c/80/dcbdaf8e6b64b7e7ae5b51949fb8ebcb36081e7a4499e63d2e6b7347a2e5/virtualenv-15.0.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 432kB/s 
Installing collected packages: virtualenv
Successfully installed virtualenv-15.0.0
You are using pip version 8.1.1, however version 21.2.4 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ mkvirtualenv -p /usr/bin/python3.5 py35
Running virtualenv with interpreter /usr/bin/python3.5
Using base prefix '/usr'
New python executable in /home/koping/.virtualenvs/py35/bin/python3.5
Also creating executable in /home/koping/.virtualenvs/py35/bin/python
Installing setuptools, pip, wheel...done.
virtualenvwrapper.user_scripts creating /home/koping/.virtualenvs/py35/bin/predeactivate
virtualenvwrapper.user_scripts creating /home/koping/.virtualenvs/py35/bin/postdeactivate
virtualenvwrapper.user_scripts creating /home/koping/.virtualenvs/py35/bin/preactivate
virtualenvwrapper.user_scripts creating /home/koping/.virtualenvs/py35/bin/postactivate
virtualenvwrapper.user_scripts creating /home/koping/.virtualenvs/py35/bin/get_env_details

(py35) koping@koping-HP:/usr/local/lib/python3.5/dist-packages$ 

3、使用python中的常见问题汇总
在服务器上(比如linux)使用python,初学者会有一些常见的疑问:
1)安装多版本的python时,运行python,pip到底调用的是哪一个python版本的命令?
2)怎么把pip源更换为国内源?
3)我安装的那些python的依赖包到底在服务器的什么路径下,怎么找到这些包?
4)如何知道我安装的python在哪里?如何知道我到底安装了多少个版本的python?
5)明明已经安装好了包,总是提示找不到命令怎么办?
以上问题可以参考下一篇博客:Python常见问题汇总:python、pip的调用版本说明、更换pip源方法、python包的安装路径说明、python安装路径查找等

你可能感兴趣的:(python基础,python,linux,ubuntu)