简体   繁体   中英

How to run anaconda2 python on anaconda3 python

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

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.

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:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM