How to run programs in python2 and python3 - python

I have python 2.6.6 and python 3.1.3 currently installed on my machine (Windows Vista 64 bit)
My path variable includes the directory of both versions. How can I specify which python I want to run a program in. For instance, if I want to run a program in python 3, it works but if I want to run a different program in python2 I get a syntax error. So how can I run a python 2 program in the cmd?
Typing python in my command line, python 3.1.3 is the only one that shows up.

You can specify the version in the executable name python2.6 and python3.

Instead of just typing "python" on the command line, use the full path the python.exe that you want to run: FULL_PATH_TO_PYTHON_2.6.6\python.exe or FULL_PATH_TO_PYTHON_3.1.3\python.exe should distinguish between the two.

The Python Launcher is probably what you need. I used it with 2.7 and 3.2.

You can also use: 'py -main_version script_name.py args'
Example:
py -2 script_name.py args for Python 2.X
py -3 script_name.py args for Python 3.X
To test both are working or not, you can try,
>> py -2
Python 2.7.16 (v2.7.16:413a49145e, Mar 4 2019, 01:37:19) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>> py -3
Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.

The shell will read the PATH from left to right, so you most likely defined Python 3.1.3 before Python 2.6.6. Specify the full path for each to use both versions.

If someone is using jupyter, and you have both python installed, you can also select which kernel to use

Related

How to declare the Python 3.7 in my path directory in Windows 10 after installation of Anaconda3?

I have installed Anaconda3 (2019) which comes with Python 3.7.
However, when I run from cmd python I am informed that I run Python 3.6.5:
(base) C:\WINDOWS\system32>python
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
I have declared Anaconda3 in my Path variable:
The python.exe is contained in a directory that I can not declare in my Path:
The directories under Anaconda3 are the following:
There is no bins subdirectory here.
But there is a python.exe file:
I have declared its path in the Path variable:
For anaconda3, the path to python is:
/anaconda3/bin/python instead of /anaconda3/pkgs/... (as you did). Try the former one.
Also, make sure that you restart your terminal after changing your PATH.
Note: you can always use which python in your terminal to check your python location.

I don´t know what version of Python I have, and problem coding my first website

I have installed the latest version of python (3.7.3)
When I go to cmd and I put py, it says my version is 3.7.3. But the problem comes when i put python or python --version, because it says that my version is 2.7.10. And i don´t know why.
I´m learning how to do my first website, and i´m just following what the tutorial says. I don´t know if the version of python is the cause of the problem, but i tell you what happens to me appart from not knowing what version i have.
I´m using Visual Studio Code and the guy from the tutorial said that we had to have at least a version of python over 3, and to find this out we have to write on CMD python --version. Then we had to know what version of pip we have putting pip --version.
Now I´ll show you the code he did and I copied, I had to install flask from CMD putting pip install flask.
So when I go to CMD to run this as the youtuber did, putting cd Desktop then cd NoFear (NoFear is the name of the folder) and finally python index.py. I got the following output:
C:\Users\Usuario\Desktop\NoFear>python index.py
Traceback (most recent call last):
File "index.py", line 1, in <module>
from flask import Flask
ImportError: No module named flask
C:\Users\Usuario>py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 21:26:53) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
C:\Users\Usuario>python --version
Python 2.7.10
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return 'Hello World'
if __name__=='__main__':
app.run()
Type Py -3 index.py in cmd instead of typing python index.py. I hope this helps
You have two versions of python so uninstall the older version first
You can create a virtual environment that will isolate your Python dependencies. Because you have stated you are using Python 3.7.3, you can create a virtual environment by running the following command:
$ python3 -m venv .venv
Then you can activate it by using:
$ source .venv/bin/activate
Then if you run python in the terminal, you will start Python 3.7.3 because your virtual environment has been created using this version of Python.
Finally, if you want to deactivate the virtual environment just run:
$ deactivate
You can uninstall a your other python versions or if you want to keep them you can rearrange the path. That being said I would recommend looking into creating virtual environments.
1) In the bottom left of windows search for: environment variables
2) Select Environment Variables... on the bottom right.
3) Select the Path variable on the top (or bottom if there is not one on the top).
4) Move python3 above python2.7 and save.
Maybe get atom cause you are working in IDLE and i had the same problem each time you want to use code form IDLE on other software you cannot run it because of the lines on top
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
you have to take them of or use virtual studio or atom or oder IDE, i think.

Python 2.7 Anaconda2 installed in windows

My Python 2 environmental path:
C:\Python27
C:\Python27\Scripts
My Python 3 environmental path:
C:\Python35
C:\Python35\Scripts
I set the environmental path for Anaconda2
C:\Users\User\Anaconda2\Scripts
C:\Users\User\Anaconda2
But when i typed python to enter the shell in cmd (C:\Users\user)
Importing the module of Anaconda like numpy or matplotlib
C:\Users\User>python
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named numpy
C:\Users\User>cd Anaconda2
C:\Users\User\Anaconda2>python
Python 2.7.12 |Anaconda 4.1.1 (64-bit)| (default, Jun 29 2016, 11:07:13) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
>>> import numpy
>>>
So i don't know
1.Why my module can't import while not in Anaconda2
2.It is said that the path of Python2 will overrdie the Python,so how to enter in the Python35 shell?
thanks everybody
Each Python installation has its own libraries. As you will see, you are not running the same Python 2.7 interpreter when you run with Anaconda active as you are without (I assume that's either the system Python or one you installed yourself).
Libraries installed in one interpreter aren't available to others. You should consider learning about conda environments to allow you to manage multiple projects easily.
The command deactivate should terminate the Anaconda environment, and if the Python 3 interpreter is first on your PATH you should then be able to run it. Another way would be to explicitly use the command
C:\Python35\python
which should work even with Anaconda active.
Caution: it's a long time since I used Windows, and I don't have current information on getting multiple Pythons to live happily together on Windows.

Why would a Python import succeed when invoking *.py file from python but not when invoked by file name alone

I'm in the early stages of integrating some Python to invoke some computations in Matlab and roll up all the figures into some html generation I'll be doing in ll.xist. I had initially installed Python 2.7.5 32bit, but with a 64bit Matlab R2015a installation I could not install the matlab engine for that version of python. I then downloaded a 64bit version of Python 2.7.9 and tried to do a pip into ll-xist, which was apparently not well maintained and failed. I then downloaded Python 3.4.3 64bit, downloaded the ll.xist installer ll-xist-5.13.win-amd64-py3.4.exe and thought I was good to go. I had to do a pip to pull in cssutils, which succeeded.
Now I come to the point where I invoke my python file from the console. If I invoke it preceded with python everything works fine, if I don't the import isn't recognized. What could account for that discrepancy? Looks like some installation snafu between the various versions I've installed, somehow the python version on the path isn't being called appropriately and I'm guessing the 2.7.9 version of python is being invoked somehow because that install never had ll.xist installed, though my 2.7.5 install did.
C:\Temp>python MyScript.py a.txt b.txt
file1: a.txt
file2: b.txt
C:\Temp>MyScript.py a.txt b.txt
Traceback (most recent call last):
File "C:\Temp\MyScript.py", line 20, in <module>
from ll.xist import xsc
ImportError: No module named ll.xist
Here's a sanity check too...
C:\Temp>python
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Temp>which python
/cygdrive/c/Python34/python
Check the file association for .py. While the proper installation might be in your path, the file association might still be pointing to a different version.
You can check this by importing the sys module in a test script and call print(sys.version). Then run the test script with python test.py and just test.py to see which versions are printed.
Turns out the governing issue here is what is known as the Python Launcher for Windows.
3.4. Python Launcher for Windows New in version 3.3.
The Python launcher for Windows is a utility which aids in the
location and execution of different Python versions. It allows scripts
(or the command-line) to indicate a preference for a specific Python
version, and will locate and execute that version.
From my console, I can see the "default" version via the py command...
C:\Users\me>py
Python 2.7.9 (default, Dec 10 2014, 12:28:03) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
C:\Users\me>py -3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
Here's the fundamental fix...
The key benefit of this is that a single launcher can support multiple
Python versions at the same time depending on the contents of the
first line.
The first line I had to add in my case to my script was
#! python3
Great commentary linked here for details and motivations behind the Launcher.

Python gives "SyntaxError: invalid syntax" under Cygwin but not in CMD, after hitting "return"?

I have used Cygwin for years and recently started using Python. I decided not to use the Cygwin provided Python (for various other issues) and installed a native Windows version. However, when running this version in the Cygwin shell it behaves a bit different than when running on the Windows Command line (CMD). The problem is that hitting "return" when in Cygwin, results in a "SyntaxError", whereas doing the same in CMD, does not. Here's what happens:
In Cygwin:
$ python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
File "<stdin>", line 1
^
SyntaxError: invalid syntax
>>> ^Z
In (Win) CMD:
Microsoft Windows [Version 6.0.6002]
Copyright (c) 2006 Microsoft Corporation. All rights reserved.
C:\Windows\System32>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> ^Z
I cannot comprehend what's going on. Any ideas?
PS. I've had python on my machine many times before (including the Cygwin native version), without any problem, but have since removed all those. I've also looked at this post, suggesting that it could have something to do with the character set or end-of-line character...
Ha! I just found an old latent environment variable: PYTHONUNBUFFERED=1 and disabled it, as suggested elsewhere. Now it works. Apparently this is a known Python bug... Another way is to run your python interpreter with: python -E, which disables all python related environment variables.

Categories