Tuesday, January 4, 2022

[SOLVED] how to make virtualenv see the default python version on Ubuntu

Issue

I encounter an error when creating virtual environment on Ubuntu-16.04. In particular, virtualenv -p python myenv returns

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

meaning incorrect syntax in Python 3.5. However, I have set my default python version on Ubuntu to be Python 3.8.9 i.e., python -V returns Python 3.8.9. So why does virtualenv refer to Python 3.5 even though Python 3.8.9 is default?

Additional info:
My virtualenv version is 15.0.1
The first line in the traceback refers to /usr/local/bin/virtualenv, which looks like this

#!/usr/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from virtualenv.__main__ import run_with_catch

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(run_with_catch())

Update:
Running python3 -m pip install virtualenv returns

Traceback (most recent call last):
  File "/usr/lib/python3.8/runpy.py", line 185, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/usr/lib/python3.8/runpy.py", line 144, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/usr/lib/python3.8/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 14, in <module>
    from pip.utils import get_installed_distributions, get_prog
  File "/usr/lib/python3/dist-packages/pip/utils/__init__.py", line 23, in <module>
    from pip.locations import (
  File "/usr/lib/python3/dist-packages/pip/locations.py", line 9, in <module>
    from distutils import sysconfig
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.8/distutils/__init__.py)

Solution

You can use the below command for ensuring that virtualenv refers to python3.8 by default

python3.8 -m virtualenv myenv


Answered By - Arun Sammit Pandey