Run python program from terminal - python

I have downloaded a python program from git.
This program is python 3.
On my laptop i have both python 2.7 and python 3.4. Python 2.7 is default version.
when i want run this program in terminal it gives some module errors because of it used the wrong version.
how can i force an name.py file to open in an (non) default version of python.
I have tried so search on google but this without any result because of lack of search tags.
also just trying things like ./name.py python3 but with same result(error)

When you type "python", your path is searched to run this version. But, if you specify the absolute path of the other python, you run it the way you want it.
Here, in my laptop, I have /home/user/python3_4 and /home/user/python2_7. If I type python, the 3.4 version is executed, because this directory is set in my path variable. When I want to test some scripts from the 2.7 version, I type in the command line: /home/user/python2_7/bin/python script.py. (Both directory were chosen by me. It's not the default for python, of course).
I hope it can help you.

Use the shebang #!<path_to_python_version_you_want>
As in:
#!/usr/bin/env python
at the very top of your .py file
Also checkout: Should I put #! (shebang) in Python scripts, and what form should it take?

The Method of #Tom Dalton and #n1c9 work for me!
python3 name.py

Related

using #!/usr/bin/env python3 shebang with Windows

I'm trying to run a Python script from the command line as a command on Windows -- so no usage of "Python" or ".py". If my script is named "testing.py", I am attempting to make this name into a command and call "testing" from the command line.
Going through the docs it seems I need to use this shebang #!/usr/bin/env python as long as I have Python in my PATH.
https://docs.python.org/3/using/windows.html#shebang-lines
I also have the script folder in my PATH, so something like
"testing.py" is currently working from the command line.
According to the docs and this tutorial,
https://dbader.org/blog/how-to-make-command-line-commands-with-python
I should be able to evoke my Python script just by "testing" if I have the proper paths within PATH and the above shebang. However, I can't seem to get the script running withouth adding ".py".
The accepted answer by #AKX is incorrect for Windows 10 standard Python 3, certainly in the latest Windows 10 (1903) if not earlier.
(Note: I cannot speak to how this may or may not work under WSL.)
I have several versions of Python installed (2.7, 3.6, 3.7, and most recently Python 3.8b1). I've been using the #!/usr/bin/env shebang for years in my scripts for cross-platform compatibility (usually to distinguish Py2 vs Py3 scripts).
I've created a little script in a folder (C:\so_test\awtest.py):
#!/usr/bin/env python3.6
import sys
print(sys.version)
If I run this with awtest.py or just awtest I get 3.6.x reported (showing it's running with Python 3.6). If I change the shebang to refer to 3.7, I get 3.7.x reported. If I change the shebang to just #!/usr/bin/env python3 I get the latest version of Python installed (3.8).
Now, if I add that folder to my path (path=%PATH%;C:\so_test in the command window you're testing in, or in the main env vars (you will need to restart the command window if you do the latter though)), I can change to a different directory and run awtest or awtest.py and they still work and refer to the folder in the path. If I remove the script folder from the path these files are no longer found.
While I wouldn't necessarily expect this to work on Windows prior to 10 or Python 2.7, this functionality appears to be the way of things going forward.
No, Windows does not support shebang lines.
The documentation you've linked relates to the py launcher installed by Python, which can interpret various shebang lines to choose a Python version to run a script with.
setuptools is able to generate wrapper .exes for your Python scripts, but it gets a little involved and already assumes you have a package with a setup.py and so on.
Locally, if you really, really need this, you probably could add .py to the PATHEXT environment variable, so the Windows command line looks up .pys like it looks up .exes (and various others; the current modern default is .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC). However, this will naturally not scale for distributing apps, as all of your users would need to set that too.
My recommendation is to stick with just that boring old python testing.py, really.
You can use shebang in windows by setting the path of your interpreter as the first line in the file(you will see a marker on VSCode that says 'set as interpreter ' on that line).
Using windows 10,Python version 3.9 see example:
#!C:/Users/waithira/AppData/Local/Programs/Python/Python39/python.exe
print('hello world')
if you're not going to do this often. You can still add a batch file to your path testing.bat containing the command that you need to execute your code.
#echo off
python testing.py
It's a borring workaround but it works without needing to mention the extention since windows interpret batch files the same way it interpret executables.

running a python script through powershell

this may seem basic, but could somebody run me through how to run a python file (one that's already created), through powershell? I know absolutely nothing about powershell despite hours of looking online to learn
Thanks all
It is happily very similar, if not the same, as running a python script from the normal command line.
First you're going to need to have python installed and in your path.
To test this try python --version in powershell. You should get output like: python 2.7.
If that worked fine then you run your script by typing python followed by the script name i.e. python test.py (if its in another directory you will need to go to that dir or add the dir to the filename).
If that didn't work you probably need to install python: https://www.python.org/
Provide the path where you have installed the Python, followed by the path of the Python script:
'path of python.exe' 'Path of the python script'
Example:
C:\\Python27\python.exe 'D:\\Project\script.py'

How do I confirm my shebang line worked for python?

How do I confirm my shebang line worked for python? I'm on a windows machine.
My default python is python3.3 but I would like to use python2.7 for my current script.
How do I know if the shebang worked? This is my quick code to attempt to confirm
#!/usr/bin/python2.7.6
import sys
print (sys.version)
However, I am being told that I'm still on python3.3. I know this is simple but I haven't found anything that can quickly answer my question.
Shebang lines are a Unix-ism, not a Windows thing. On Windows you will need to launch your script with the correct interpreter. One idea would be to change the file extension to .py2 and then associate that with the Python 2 interpreter.
You can also look at PEP 397, which describes a new launcher in Python 3.3 which can locate other Python interpreter versions according to your shebang line. This is a Python 3.3 feature, not a Windows one, just to be clear.

How can I make the "python" command in terminal, run python3 instead of python2?

I'm just starting to learn Python and did search around a little, so forgive me if this has been asked and answered.
When running scripts through the command line/terminal, I have to type "python3" to run the latest version of Python. With Python 2.X I just use "python".
Is there a way to run Python 3 just using "python"?
It may seem a little lazy, but I'm mostly just curious if it is possible or if it will break anything unnecessarily if I could in fact do it.
If you are using Linux, add the following into into ~/.bashrc
alias python=python3
Restart the shell and type python and python3 should start instead of python2.
If you're using Windows then you can use the Python Launcher For Windows.
This will allow you to use the py command to select different python installations such as:
py -2.7 # Runs Python 2.7
py -3.3 # Runs Python 3.3
py -2 # Runs the latest version of Python 2.x (so if you have 2.6 and 2.7 it will run 2.7)
Similarly you can set a shebang in your python files as demonstrated below:
#! python3
print('Hello World!')
If you now run that file (let's call it test.py) with py test.py it will automatically run with Python 3. It gets the Python installation to use from the shebang at the beginning of the line.
What you probably want is to customise the default python version though. This will allow you to set the default actions if you just call py on it's own.
Once you installed python 3 in your Mac, "python3" command will be registered into the environment variable automatically. So if you need to run your python 3 file just do that:
python3 your_file_name.py
I hope this help you.
Sounds like you have python 2 and 3 installed and your pythonpath is pointed at python 2, so unless specified it uses that version. If you are using python I would suggest setting up a virtual environment (virtualenv) for each project, which means you could run whatever version you'd like in that project and keep all dependencies contained.
According to PEP-394,
"for the time being, all distributions should ensure that python refers to the same target as python2".
On *nix systems, there are three links to executables of python command line interpreter named
python, python2 and python3 in directory /usr/bin. The python link points to python2 according to the PEP, but you can change it to point to python3 by creating a new link to python3 and renaming it to python. Also, you have to delete the old python link.
on raspbian linux in the terminal i just run it by typing python3 file.py or just python file.py for python 2

Python 3.2 doesn't work with python 2.7

I already have python 2.7 (installed using activepython). I'm trying to use python 3.2 just to learn more about it so i downloaded it from python.org
python 2.7 still works perfectly, but python 3.2 gives me this error when i try to open the ide.
and then I see the send error window, if i install python 3.2 using activepython i see the same error.
I'm using windows xp pro sp3 32 bit and i had the same error on sp2... How do I fix it?
EDIT #Zuljin
This is the first time that i use dependancy walker so could you give me a hand please
this is what i see
what does that mean? I already have these files...
I saw a lot of answers here. But, I think the file that you are trying to run is not correct
C:\Python32\Pythonw.exe
is not the one you use to open idle.
Open
C:\Python32\Lib\idlelib\idle.pyw
Python 2.x and 3.x can cohabitate perfectly in win xp and win 7, either 32 or 64 bits.
If you first installed the ActiveState Python distribution, be careful when installing python-3.2.2.msi.
When you arrive to the customize python 3.2.2 screen, (un)check 'register extensions' to make the feature unavailable (that is: do not register extensions).
I have installed this way both distributions in different computers without any problem so far.
Note: Check your environment variables. Only python 2.7 should be in the path (if you installed before the two distros you could have them both in the path. Remove python 3.2 path)
Edit:
From Ankit post I realized that in fact you were trying to open IDLE, maybe.
If you follow my installation instructions when you call idle.bat from the py3.2 idlelib folder you actually get idle for python 2.7 as this is what the call find in the windows path. To be able to open idle for py3.2 in the presence of python 2.7 as the registered python, I use a .bat file modified from that in the distribution:
idle_stay.bat
#echo off
rem Working IDLE bat for Windows - Custom bat for py3k as secundary python
C:\Python32\pythonw C:\Python32\Lib\idlelib\idle.pyw %1 %2 %3 %4 %5 %6 %7 %8 %9
The name idle_stay.bat is because I put this file in the folder where the official idle.bat is (C:\Python32\Lib\idlelib\idle_stay.bat). In this way it does not get overwritten each time I unisntall and reinstall a new version of python 3
It seems to me you have associated a *.py or *.pyw file extension type to some version of python.exe and/or pythonw.exe you aren't clear about. This is often, but not exclusively done from within Microsoft's "explore.exe" File Manager (I believe in Options).
I disagree that this is a reliable method:
C:\Python32\Lib\idlelib\idle.pyw
as this method will run whichever program is assigned to the "pyw" extension. That could be:
C:\Python32\pythonw.exe
or
C:\Python26\python.exe
or whatever might have happened to your file extension association as per OS or bad PATH environment variable setting. It is better to do something like this:
C:\Python32\pythonw.exe C:\Python32\Lib\idlelib\idle.pyw
where you are explicidly telling the OS to run the python executable from a specific path and selecting a specific python script in the file/folder hiearchy.
Another thing you can try to gather info is just:
python.exe -V
which will tell you the version number.
Another thing you can do:
Open a DOS command prompt and type:
set PATH=C:\Windows;C:\Windows\system32;C:\Python32
python.exe and note the version and whether is runs.
exit()
exit
Once working debug your PATH. You can use this Python script to gather intel.
If you turned off your WinXP findfile stuff (like I have) you can use this script:
inpath.py can help shed some light.
Run this python script like this:
Drop to a DOS prompt
cd to your folder with a working python.exe version.
and type python.exe inpath.py python*
where inpath is in that same folder for this test/debug.
inpath.py will search your path for all files associated as "runable" in your OS and that is in your "PATH" with the python*.* pattern.

Categories

Resources