I'm going to start with the obvious: Python 2.7 has been dead for 4 years now ([Python]: Sunsetting Python 2). Make sure that you clearly mention the reason for sticking to it, otherwise many users might see it as an XY Problem. I saw in a comment from another question that you are working with [Gwyddion]: Gwyddion which indeed doesn't support Python 3.
Now, since Python 2 is no longer supported, packages, tools, ... don't have to support it (some still might, but most will not - as time goes by).
VirtualEnv that is shipped by PyCharm doesn't. Check [SO]: pycharm does not connect to console with python3.8 (@CristiFati's answer) for a similar situation.
Alternatives:
I'll be explaining the theoretical process with practical examples. There will be scattered snippets from a Cmd console (starting below):
[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q079004210]> sopr.bat ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ### [prompt]> :: === Start === [prompt]> dir /b code00.py [prompt]>
Prerequisites:
There are multiple variants here, so I'll focus on the one I use most. You'll have to have:
A Python 2.7 instance that will be used as a base for creating the VEnv. I'll be referring to it as (like it was a Nix environment variable) ${PY2}
A Python instance with VirtualEnv ([PyPA.VirtualEnv]: Installation - also check [SO]: Python3.7 venv does not Create Virtual Environment Directory (@CristiFati's answer)) installed. This can be the same as the previous one, but I recommend using a n up to date Python 3 version (${PY3}), as there are some prerequisites as well ([PyPI]: pip, and modern Python versions come with it inside). Might be helpful:
Create the VEnv
Step(s) detailed in [SO]: Create Windows Python virtualenv with a specific version of Python (@CristiFati's answer).
Typically: "${PY3}" -m virtualenv -p "${PY2}" ${VENV2}
where ${VENV2} is the location where you want your VEnv to be created at. Activate it afterwards:
[prompt]> :: === Create VEnv === [prompt]> "c:\Install\pc064\Python\Python\03.10\python.exe" -m virtualenv -p "c:\Install\pc064\Python\Python\02.07\python.exe" ".\py_0207_pc064_venv" created virtual environment CPython2.7.18.final.0-64 in 8051ms creator CPython2Windows(dest=E:\Work\Dev\StackExchange\StackOverflow\q079004210\py_0207_pc064_venv, clear=False, no_vcs_ignore=False, global=False) seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=C:\Users\cfati\AppData\Local\pypa\virtualenv) added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.37.1 activators BashActivator,BatchActivator,FishActivator,NushellActivator,PowerShellActivator,PythonActivator [prompt]> (py_0207_pc064_venv) [prompt]> dir /b code00.py py_0207_pc064_venv [prompt]> [prompt]> ".\py_0207_pc064_venv\Scripts\python.exe" Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z [prompt]> :: Activate VEnv [prompt]> ".\py_0207_pc064_venv\Scripts\activate.bat" (py_0207_pc064_venv) [prompt]> (py_0207_pc064_venv) [prompt]> :: Check for PIP (py_0207_pc064_venv) [prompt]> python.exe -m pip > /nul e:\Work\Dev\StackExchange\StackOverflow\q079004210\py_0207_pc064_venv\Scripts\python.exe: No module named pip (py_0207_pc064_venv) [prompt]> (py_0207_pc064_venv) [prompt]> :: @TODO - cfati: Check previous command (error message if PIP not installed, OK otherwise) (py_0207_pc064_venv) [prompt]> (py_0207_pc064_venv) [prompt]> echo %ERRORLEVEL% (py_0207_pc064_venv) [prompt]> 1
Install PIP in the VEnv
This is an optional step that has to be executed only if PIP is not already there (check the @TODO at the end of the last snippet). Usually it's not required, but exemplifying it anyway (you need PIP to be able to install packages in your VEnv).
Check [AskUbuntu]: How can I find an older version of pip that works with Python 2.7?:
(py_0207_pc064_venv) [prompt]> :: === Get (v2.7 compatible) PIP (if not already there) === (py_0207_pc064_venv) [prompt]> curl https://bootstrap.pypa.io/pip/2.7/get-pip.py --output ".\get-pip.py" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 1863k 100 1863k 0 0 3912k 0 --:--:-- --:--:-- --:--:-- 3956k (py_0207_pc064_venv) [prompt]> (py_0207_pc064_venv) [prompt]> dir /b code00.py get-pip.py py_0207_pc064_venv (py_0207_pc064_venv) [prompt]> (py_0207_pc064_venv) [prompt]> :: Check for PIP (py_0207_pc064_venv) [prompt]> python.exe -m pip > /nul (py_0207_pc064_venv) [prompt]> echo %ERRORLEVEL% 0
Now, you should have an usable Python 2.7 VEnv.
Import the new VEnv in PyCharm
Steps (PyCharm related ones) are identical to the ones from [SO]: How to install Python using the "embeddable zip file" (@CristiFati's answer)
After the above steps and PyCharm finishes indexing the files, your new VEnv is ready to use:
Test the new VEnv from Pycharm
code00.py:
#!/usr/bin/env python
import sys
def main(*argv):
print "Py2.7 specifics:", range(7)
if __name__ == "__main__":
print(
"Python {:s} {:03d}bit on {:s}\n".format(
" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32,
sys.platform,
)
)
rc = main(*sys.argv[1:])
print("\nDone.\n")
sys.exit(rc)
Output (PyCharm console):
E:\Work\Dev\StackExchange\StackOverflow\q079004210\py_0207_pc064_venv\Scripts\python.exe E:\Work\Dev\StackExchange\StackOverflow\q079004210\code00.py Python 2.7.18 (v2.7.18:8d21aa21f2, Apr 20 2020, 13:25:05) [MSC v.1500 64 bit (AMD64)] 064bit on win32 Py2.7 specifics: [0, 1, 2, 3, 4, 5, 6] Done. Process finished with exit code 0
Also attaching a screenshot:
[JetBrains]: Other Versions contains a list of them. For example, v2019.3 should be OK, but you'll have to see it for yourself.
Needless to say that it won't have the features an upper-to-date version has.
Only mentioning this as a purely theoretical variant, I don't even need to start enumerating the reasons why not to do it. I did it once though: [SO]: Run / Debug a Django application's UnitTests from the mouse right click context menu in PyCharm Community Edition? (@CristiFati's answer).