How to use the values ​​of two variables that are in the second python to use in file 3?

The variable is updated in a cycle (you ask why you cannot push two programs into one - the answer is: python has no intermediate version, and lib for 3 pytons does not work for 2, and for 2 does not work for 3).

If you know how to make friends with can-python and opencv - write!

  • 3
    opencv is for both 3 and 2 python, python-can too. pypi.python.org/pypi/python-can , - Andrio Skur
  • That's the problem, that you will get a bit of mistakes and python-ken in the third python too, but vice versa - OLEERM
  • what type of variables? - MaxU
  • 3
    @OLEERM It would be better to start with trying to deal with these errors - andreymal

1 answer 1

If there is no other option left, try using some kind of Interprocess Communication, such as sockets, as described here: https://docs.python.org/3.5/library/socket.html#example

Here is my version of the example of using sockets (written at 4 am, so don’t blame the code too much, please;)).

test2.py:

import socket f = open('log', 'w') s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) s.bind('/tmp/test_socket') s.listen(0) conn = s.accept()[0] while True: data = conn.recv(1024) if not data: break num = int(data) conn.sendall(str(num) + ':' + str(pow(num, 3))) conn.close() s.close() 

test3.py:

 import socket import subprocess as sp from time import sleep with sp.Popen(['python', 'test2.py']) as proc: sleep(1) with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as s: s.connect('/tmp/test_socket') for i in range(10): s.sendall(bytes(str(i), 'ascii')) res = str(s.recv(1024), 'utf8') print('int: {}, pow3: {}'.format(*res.split(':'))) 

test2.py contains code using Python 2, and test3.py - using the third version, respectively. From test3.py, test2.py is started, in which a socket is opened and a server is started, which in the loop receives one number from the client and returns the original number and its third power.

Here is the output from the test3.py file (which is the client):

 int: 0, pow3: 0 int: 1, pow3: 1 int: 2, pow3: 8 int: 3, pow3: 27 int: 4, pow3: 64 и т.д. 
  • Thanks, today I will try to write the result. - OLEERM
  • the code didn't work, but I managed to put it in 3 python opencv - OLEERM
  • Well, it’s still a better option. And what exactly did not work in the code? Specifically, this run or your version? Which mistakes? Which OS did you run? - vchslv13
  • Every time you started, you had to create a new socket, because there was a mistake in the spirit: this socket is already in use. Yes, I ran this copy-paste. Launched under ubuntoy. - OLEERM
  • Ah, got it. Yes, I saw this error, but did not correct it: I thought that you would see the performance of this construction, and finish the rest yourself :) Then I’ll look later for the reason why the socket is not deleted, or some solution in the form of removing the socket as usual file (I think it will work too) and add to the code "for history". - vchslv13