-2

It's a big project, some libraries are written in Python2 and some libraries are written in Python3. The project is big.

Is it possible to use both Python 2 and Python 3 in the same project?

marlon
  • 6,029
  • 8
  • 42
  • 76
  • 1
    Yes. Same as you can use any two or more languages in one project. – MisterMiyagi Aug 06 '20 at 18:32
  • 1
    @MisterMiyagi, but in one virtual environment, there is can only one version of Python. Right? – marlon Aug 06 '20 at 18:34
  • If you're using say Anaconda to run your virtualenvs , you may run into some issues since I don't think Anaconda natively supports multiple environment instances simultaeously. Check out https://stackoverflow.com/questions/50953575/can-you-activate-multiple-python-virtual-environments-at-once for other solutions to work around this – DerekG Aug 06 '20 at 18:35
  • there is no rule like that, indeed in your IDE you will have option to choose between the interpreter u want to use – Delrius Euphoria Aug 06 '20 at 18:36
  • @DerekG that Q talks abt simultaneous running, but in this Q OP only mentions about at the same system, nothing abt running both together – Delrius Euphoria Aug 06 '20 at 18:37
  • and about running the code, if your code contains mixed syntax's from python3 and 2 then it depends upon, which version runs the code and you can also use `try` and `except` to battle most of those error due to version change – Delrius Euphoria Aug 06 '20 at 18:41
  • Does this answer your question? [Can I install Python 3.x and 2.x on the same Windows computer?](https://stackoverflow.com/questions/341184/can-i-install-python-3-x-and-2-x-on-the-same-windows-computer) – Trenton McKinney Aug 08 '20 at 20:50
  • Maybe [Sharing variables between different instances of different classes](https://stackoverflow.com/questions/51992554) – Trenton McKinney Aug 08 '20 at 20:59
  • It is very much possible to seemingly run both Py2 and Py3 code in one application — e.g. [cpy2py](https://github.com/maxfischer2781/cpy2py) (disclaimer: I maintained this package) uses a mechanism similar to multiprocessing but with different interpreters, and there are other means to do similar things. However, not only will this produce artifacts (e.g. copies instead of references) — Py2 and Py3 are subtly but fundamentally different and cannot be translated 1:1 unambiguously. In the general case, you still have to solve all the problems of porting the code from one version to the other. – MisterMiyagi Aug 09 '20 at 07:25

2 Answers2

2

Depending on the structure of your project, it MAY be possible to do so. If the entire project must run from a single Python interpreter, using multiple versions of python will not be possible. However, it would be possible to run multiple python interpreters simultaneously, using sockets or some other inter-process communication tool to communicate between them. Without more information on your project, it's hard to tell to what extent this might be feasible.

DerekG
  • 3,555
  • 1
  • 11
  • 21
  • For example, what other inter-process communication tool? Is there sockets example for this purpose? – marlon Aug 06 '20 at 19:28
  • I'd guess you'd have to use a socket-based protocol for this, or else write to shared file locations. For instance, MQTT provides lightweight UDP and TCP implementations for cross-process (cross interpreter) interfacing, which should work across python versions – DerekG Aug 06 '20 at 19:36
0

you can get two different window for the two python interpreters. For example, if you download python 2.7 and what ever python 3 version, you can open the windows side-by-side.