Python Forum
May my 'particular' Py2 coexists with Py3 in Windows OS?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
May my 'particular' Py2 coexists with Py3 in Windows OS?
#1
My abrupt answer in _no_, because pip2 installation interfers (as far as I know) with pip of Py3, but I'm confident in forum's drifts to overcome the problem.

The main Python installation in my pc, Win-10 equipped, is as follows:
Output:
C:\Users\Alfabeta>python -V Python 3.7.2 C:\Users\Alfabeta>pip -V pip 19.0.3 from c:\users\alfabeta\appdata\local\programs\python\python37\lib\site-packages\pip (python 3.7) C:\Users\Alfabeta>pip list Package Version -------------- -------- 2to3 1.0 <---- converter py2/py3, I won't use it! altgraph 0.16.1 astropy 3.1.2 de421 2008.1 future 0.17.1 jplephem 2.9 julian 0.14 macholib 1.11 mpmath 1.1.0 numpy 1.16.2 pefile 2018.8.8 pip 19.0.3 PyInstaller 3.4 pywin32-ctypes 0.2.0 setuptools 40.6.2 sympy 1.3
I reach the Py2 version by means of this batch file:
Output:
@echo off cls cd\0_BackUP-29-Feb-2016\Temp\Python26 python2 -V python2 --help python2 my-newton.py pause
So, the python release name is shown below and runs properly next script
# --------- my-newton.py ------------------ 
# --  (derivative computed through finite interval dx=h) ----
def derivative(f, x, h):
      return (f(x+h) - f(x-h)) / (2.0*h)  # via finite interval h

def funzione(x):
    return x**3 - x -1     # Given function 

def solve(f, x0, h):
    lastX = x0
    nextX = lastX + 10* h                   # different than lastX so loop starts OK
    while (abs(lastX - nextX) > h):         
        newY = f(nextX)                     
        print "f(", nextX, ") = ", newY     # printout progress
        lastX = nextX
        nextX = lastX - newY / derivative(f, lastX, h)  # update estimate using Newton method
    return nextX

xFound = solve(funzione, 2, 10**(-7))   # call the solver given starting value, tolerance
print "solution: x = ", xFound          # print the result
# ------------------------------------------------------
Output:
Python 2.6.5 usage: python2 [option] ... [-c cmd | -m mod | file | -] [arg] ... Options and arguments (and corresponding environment variables): -B : don't write .py[co] files on import; also PYTHONDONTWRITEBYTECODE=x -c cmd : program passed in as string (terminates option list) -d : debug output from parser; also PYTHONDEBUG=x -E : ignore PYTHON* environment variables (such as PYTHONPATH) -h : print this help message and exit (also --help) ........... ........... Running my-newton.py ------------- f( 2.000001 ) = 5.00001100001 f( 1.54545504037 ) = 1.14575812265 f( 1.35961505435 ) = 0.153705563753 f( 1.32580135343 ) = 0.00462495304571 f( 1.32471904943 ) = 4.65779447723e-06 f( 1.32471795725 ) = 4.74331685041e-12 solution: x = 1.32471795724
Unfortunately, such a 'particular' installation allows a strong limitative use of Py2, due to the absence of libraries, commonly coming from (pip).

Thx in advance, and cheers
Reply
#2
When install Python 3.7 it also put py.exe in Path of Windows.
This py is what you use to access other versions.
Example:
# Main version set in Path
E:\div_code
λ python -V
Python 3.7.2

# Main pip set in Path
E:\div_code
λ pip -V
pip 19.0.3 from c:\python37\lib\site-packages\pip (python 3.7)

# Access Python 3.4  
E:\div_code
λ py -3.4 -V
Python 3.4.2

# Access Python 2.7
E:\div_code
λ py -2.7 -V
Python 2.7.9

# Install with pip to 2.7
E:\div_code
λ py -2.7 -m pip install jsonlines
Collecting jsonlines
  Downloading https://files.pythonhosted.org/......any.whl
Requirement already satisfied: six in c:\python27\lib\site-packages (from jsonlines) (1.9.0)
Installing collected packages: jsonlines
Successfully installed jsonlines-1.2.0

# Running 2.7 code would be
E:\div_code
λ py -2.7 my_script.py
Reply
#3
Good drift, snippsat.
Will perform back to home and then refer here. Thanks a lot
Reply
#4
Oh, yes! Now all is clear.

This command said me how to query my system:
Output:
C:\>py --help Python Launcher for Windows Version 3.7.2150.1013 usage: py [launcher-args] [python-args] script [script-args] Launcher arguments: -2 : Launch the latest Python 2.x version -3 : Launch the latest Python 3.x version -X.Y : Launch the specified Python version The above all default to 64 bit if a matching 64 bit python is present. -X.Y-32: Launch the specified 32bit Python version -X-32 : Launch the latest 32bit Python X version -X.Y-64: Launch the specified 64bit Python version -X-64 : Launch the latest 64bit Python X version -0 --list : List the available pythons -0p --list-paths : List with paths The following help text is from Python: usage: C:\Users\Alfabeta\AppData\Local\Programs\Python\Python37\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... .......... .......... and so on My trials --------- C:\>py -2 Python 2 not found! Installed Pythons found by py Launcher for Windows -3.7-64 * Requested Python version (2) not installed, use -0 for available pythons C:\>py -0 Installed Pythons found by py Launcher for Windows -3.7-64 * My comments: the answer is correct, because my 'particular' Py2 has been unlinked ----------- from the environment variables.
Now my next steps are easy to carry out, as detailed in this screenshot http://imgbox.com/ewyV3XM2

Thank you very much, wonderful forum!
Reply
#5
Some helps more, please! Py2 version been installed
Output:
C:\Training>py -2 -V Python 2.7.15
alongside astropy library, which properly performs the script through the command (py -2 marte.py)
# --------- marte.py ----------  (mars coords) --------
from astropy.time import Time
from astropy.coordinates import solar_system_ephemeris, EarthLocation
from astropy.coordinates import get_body_barycentric, get_body, get_moon
t = Time("2017-08-21 17:28:11.284")
loc = EarthLocation.of_site('greenwich') 
marte = get_body('mars', t, loc) 
#
print(marte)  
print(solar_system_ephemeris.bodies)
# ---------- OUTPUT -----------
# C:\Training>py -2 marte.py
# <SkyCoord (GCRS: obstime=2017-08-21 17:28:11.284, obsgeoloc=(-2439719.30740335, -3138944.00436915, 4970891.10618061
# ) m, obsgeovel=(228.88127335, -178.52172321, -0.39499058) m / s): (ra, dec, distance) in (deg, deg, AU)
#     (143.19625021, 15.77320554, 2.64972634)>
# (u'earth', u'sun', u'moon', u'mercury', u'venus', u'earth-moon-barycenter', u'mars', u'jupiter', u'saturn', u'uranu
# s', u'neptune')
# -------------------
My problem is: how to catch the list of libraries in Py2? Some errors arose, Py3 pip was called.
Error:
C:\Users\Alfabeta>py pip -2 freeze C:\Users\Alfabeta\AppData\Local\Programs\Python\Python37\python.exe: can't find '__main__' module in 'pip' C:\Users\Alfabeta>py pip -2 list C:\Users\Alfabeta\AppData\Local\Programs\Python\Python37\python.exe: can't find '__main__' module in 'pip' C:\Training>py -2 pip freeze C:\Python27\python.exe: can't open file 'pip': [Errno 2] No such file or directory C:\Training>py pip -2 list C:\Users\Alfabeta\AppData\Local\Programs\Python\Python37\python.exe: can't open file 'pip': [Errno 2] No such file or directory
Thx in advance
Reply
#6
You need -m in command.
py -2.7 -m pip freeze
py -2.7 -m pip list
Reply
#7
Oh yes! Problem solved, thank you.
Output:
C:\Users\Alfabeta>py -2.7 -m pip freeze DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. astropy==2.0.12 atomicwrites==1.3.0 attrs==19.1.0 colorama==0.4.1 funcsigs==1.0.2 more-itertools==5.0.0 numpy==1.16.2 pluggy==0.7.1 py==1.8.0 pytest==3.6.4 six==1.12.0
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020