0

I have two static python versions 3.7 and 2.7 The 1st static locations is: ~/anaconda3/bin/python The 2nd static locations is: ~/anaconda2/bin/python

I want to run a method that only works with python2 inside of another script in python 3.

Also, I want to get a returned value from that python2 script.

What would be the appropriate solution?

Thank you

2 Answers 2

2

I think the appropriate solution is to just use one installation of conda.

Then within that installation, you can create one environment for python 2 and another environment for python3.

conda create --name py2 python=2.7
conda create --name py3 python=3.5

Source: https://docs.anaconda.com/anaconda/user-guide/tasks/switch-environment/

After that, you can use the py3 environment to run the python3 part and the py2 environment to run python2 part.

You might have to install the "same" package to both the py2 and the py3 environment. But that is fine. For example, package X might have an old version that works for python2 and a new version that works for python3 but not python 2. In this case, you install the old version on py2 and the new version on py 3

Passing value between the python 2 and python 3 scripts: save the value to a file with the python 2 script. Then let the python3 script read the file.

Sign up to request clarification or add additional context in comments.

4 Comments

This coda will be run by different users, Then the result file in your case will be overwritten, at least giving a dynamic name would ba solution which I can't know which one exactly that I should open in python3
use python3 to create a shell that runs the python2 script. python2 script print to stdout and python3 grab the result from stdout can work. What do you want to pass between python2 and python3? How much of the code is in python2?
It is another AI model that generates some results and return them back to the python3 script.
Use python3 to create a temporary directory, create a shell that runs python2 script, and pass the path of the temp. directory as a command-line argument to the python2 script. Python2 save to temporary directory. Python3 retrieve the result from the temporary directory. If you create temporary directory in /tmp, there might be security problem. Probably you should ask another question for passing value between python3 and python2 script.
0

The solution was to create a file that contains the parameter data if it is very long. Then I pass the file name as an argument {You have to let the other script to parse that file} Example:

python2 = "~/anaconda2/bin/python"
script = "\'import Python2Function as pf ; pf.main(\"{0}\",\"{1}\")\'".format(shortargument, file2name)
output = os.popen(python2 + ' -c ' + script).read()

To create a random file name: You can use

import uuid
str(uuid.uuid4()) + '.tsv'

Thank you and Good Luck:)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.