Search code examples
pythonpython-2.7centosvirtualenv

Can't create virtual environment with python 2.7


I' trying to create a virtual environment with a custom python 2.7.15 on a centOS server,I followed the steps from this website (https://help.dreamhost.com/hc/en-us/articles/215489338-Installing-and-using-virtualenv-with-Python-2),but when I use virtualenv to create a python virtual environment something happens.Here is the code.

$ virtualenv -p /public/home/myname/opt/python-2.7.15/bin/python venv
RuntimeError: failed to query /public/home/myname/opt/python-2.7.15/bin/python with code 1 err: '  File "/public/home/myname/.local/lib/python3.8/site-packages/virtualenv/discovery/py_info.py", line 152\n    os.path.join(base_dir, exe) for exe in (f"python{major}", f"python{major}.{minor}")\n                                                           ^\nSyntaxError: invalid syntax\n'

I've searched the net for answer but I failed to reach one,can anyone explain what happened? Is the anaconda that prevents me from creating my virtual environment?

I searched the Error but it seems no one came across with the same error I met.


Solution

  • You must use virtualenv installed for Python 2.7. Install/upgrade tools:

    $ /public/home/myname/opt/python-2.7.15/bin/python -m pip install --upgrade "pip<21.0"
    $ /public/home/myname/opt/python-2.7.15/bin/python -m pip install --upgrade virtualenv
    

    Now create the venv:

    $ /public/home/myname/opt/python-2.7.15/bin/python -m virtualenv venv