0

I am trying to set virtual environment with a different python version.

The default global version is Python 3.11.0

I have installed virtualenv using pip install virtualenv

Command to create virtualenv with different python version is virtualenv -p "C:/Python310/python.exe" .venv

Error when I run the above command is

RuntimeError: failed to query C:\Python310\python.exe with code 1 err: 
Traceback (most recent call last):
  File "C:\Python311\Lib\site-packages\virtualenv\discovery\py_info.py", line 8, in <module>
    import json
  File "C:\Python311\lib\json\__init__.py", line 106, in <module>
    from .decoder import JSONDecoder, JSONDecodeError
  File "C:\Python311\lib\json\decoder.py", line 3, in <module>
    import re
  File "C:\Python311\lib\re\__init__.py", line 125, in <module>
    from . import _compiler, _parser
  File "C:\Python311\lib\re\_compiler.py", line 18, in <module>
    assert _sre.MAGIC == MAGIC, "SRE module mismatch"
AssertionError: SRE module mismatch

Not sure what I am missing here. Any guidance is appreciated.

sinoroc
  • 18,409
  • 2
  • 39
  • 70
sub
  • 527
  • 1
  • 7
  • 24
  • Maybe file a bug ticket: https://github.com/pypa/virtualenv/issues -- You have Python 3.10 installed, right? Just making sure, that you do not expect that `virtualenv` will install Python 3.10 for you. It has to be installed. Even if you use Python 3.11 to create the virtual environment, 3.10 still has to be there. – sinoroc Mar 23 '23 at 09:47
  • If 3.10 is installed, then if I were you I would double check that it works perfectly well. That there is nothing suspicious about `PYTHONHOME` and/or `PYTHONPATH` environment variables. -- See these other questions: https://stackoverflow.com/q/63825609 and https://stackoverflow.com/q/53600426 – sinoroc Mar 23 '23 at 09:53
  • Hi, @sinoroc Yes, I have both python 3.11 and 3.10 installed. I also had only python 3.11( both Python and Python/Scripts folder) added to the path environmental variables. – sub Mar 23 '23 at 10:28
  • According to the other similar questions (`SRE module mismatch`), this happens when there is an issue with the Python setup. If I were you, I think I would re-install the Python 3.11 and 3.10, then go through all possible sources of misconfiguration. I do not know how to narrow it down any further... my guess is that it is something specific to your setup. -- It could of course be a `virtualenv` bug, but I doubt it. -- As a test, maybe I would also try to install Python 3.9 (if not installed yet), and create a virtual environment for 3.9 from 3.11. Maybe I would also try the other way around. – sinoroc Mar 23 '23 at 10:38
  • ok I will try to reinstall python and check the config. I used ```choco install python``` to install python – sub Mar 23 '23 at 10:42

1 Answers1

0

If possible in your environment, i like to use conda for venvs + multiple python versions like:

# Download conda from https://www.anaconda.com/products/distribution

# Create conda env - python 3.7
conda create -n python-3.7 python=3.7

# activate and initialize virtual env
conda activate python-3.7

# make another!
conda create -n python-3.8 python=3.8

# activate and initialize virtual env
conda activate python-3.8

NOTE - I use a Mac, I think its just as smooth on Windows but im not 100% sure :D

Brandon Sollins
  • 293
  • 1
  • 3
  • 8
  • ok, I will try this. But I am also curious to know why the above error occurs. – sub Mar 22 '23 at 22:01
  • If you see in the trace back it looks to be referencing Python 3.11 code instead of 3.10. This stuff can be a deep rabbit hole which is why I usually go for conda, it helps manage the headache – Brandon Sollins Mar 22 '23 at 23:34
  • "*in the trace back it looks to be referencing Python 3.11 code instead of 3.10*", this seems normal to me. It is Python 3.11 that is running (as expected), and as far as I can tell, it is trying to query some information from Python 3.10 (via JSON). – sinoroc Mar 23 '23 at 10:42