0

I have version 3.10.2 of Python installed on my Windows machine via the Anaconda distribution. There is an API that I'd like to use that supports only Python version 2.7.

In order to use this API, do I need to also install version 2.7 (which I would rather not do!)?

Or, can I use version 2.7 of Python virtually through some means? And, if, "yes", what is the best way to do so virtually?

Thanks!

wjandrea
  • 28,235
  • 9
  • 60
  • 81
equanimity
  • 2,371
  • 3
  • 29
  • 53
  • You can, but not in the same *process*. If you have code that only runs under Python 2.7, you'll need to install Python 2.7 in order to run it. A virtual environment is just a name for a specific installation and some shell configuration to ease its use. – chepner Jul 18 '22 at 16:28
  • 1
    Python 2 was officially sunset in January 2020. As a consequence, it hasn't received any patches or security updates since then. It's generally *not* a good idea to write new code that depends on Python 2. All else equal, I would recommend seeking alternatives for the libraries you're seeking to leverage, while also acknowledging there are *some* scenarios where this type of dependency is unavoidable. – esqew Jul 18 '22 at 16:29
  • https://stackoverflow.com/questions/27863832/calling-python-2-script-from-python-3 – Tom McLean Jul 18 '22 at 16:29
  • @esqew For reference: [Sunsetting Python 2](https://www.python.org/doc/sunset-python-2/) – wjandrea Jul 18 '22 at 16:30
  • 1
    APIs generally don't care what you call them with. Do you mean you have a Python library for this API but the _library_ is for Python 2? Try translating it with `2to3` but don't necessarily expect it to work right out of the box. In particular, Python 3 distinguishes between Unicode strings and `bytes` sequences where Python 2 obliviously let you mix the two; perhaps see also http://nedbatchelder.com/text/unipain.html – tripleee Jul 18 '22 at 16:44

1 Answers1

0

Create a new virtual env with python 2.7, with conda create --name new_env, and run your code with the python.exe inside this env

conda

Devyl
  • 565
  • 3
  • 8
  • Shouldn't that be `conda create --name new_env python=2.7`? – wjandrea Jul 18 '22 at 17:21
  • yes i mean you can install whatever you want in a virtualenv – Devyl Jul 18 '22 at 17:26
  • Oh wait, maybe I'm misunderstanding. How does this use Python 2.7 virtually, like OP's asking? If you don't specify the Python version, Conda uses the **system** version, no? So that'd be 3.10 for OP. Or if you are in fact suggesting that OP install Python 2.7 in the virtual env, how does that help? OP's asking how to **avoid installing it**. (Personally, I think I understand how Conda works, I'm just not very experienced with it.) – wjandrea Jul 18 '22 at 17:37
  • with an env with python=2.7, just do `conda activate name_of_env` and then localize where is python with `echo $CONDA_PREFIX`, after in console execute your python script with this python.exe, it will run in a different process – Devyl Jul 18 '22 at 18:50