Execute current Python script in Python 3

run python script from python shell
python run another python script and get output
run python script from terminal
run python script from python command line
run python script online
how to run python program in windows 10
python script example linux
call python script from python

I have a python program which I have made work in both Python 2 and 3, and it has more functionality in Python 3 (using new Python 3 features).

My script currently starts #!/usr/bin/env python, as that seems to be the mostly likely name for a python executable. However, what I'd like to do is "if python3 exists, use that, if not use python".

I would prefer not to have to distribute multiple files / and extra script (at present my program is a single distributed python file).

Is there an easy way to run the current script in python3, if it exists?


Another better method modified from this question is to check the sys.version:

import sys
py_ver = sys.version[0]

Original answer: May not be the best method, but one way to do it is test against a function that only exist in one version of Python to know what you are running off of.

try:
    raw_input
    py_ver = 2

except NameError:
    py_ver = 3

if py_ver==2:
   ... Python 2 stuff

elif py_ver==3:
   ... Python 3 stuff

How to Run a Shell Command from Python and Get The Output , How do I run a Python script from command line? Python Developers bring home the highest pay-cheques today.Enroll now to boost your salary. Amazing exclusive deals to help you become an in-demand certified Python developer!


Try with version_info from sys package

Another better method modified from this question is to check the sys.version : import sys py_ver = sys.version[0]. Original answer: May not be  Elite Programming Professionals Ready to Teach Online. Anytime, Anywhere.


Maybe I didn't quite get what you want. I understood: You want to look if there is Python 3 installed on the Computer and if so, use it. Inside the script you can check the version with sys.version as Idlehands mentioned. To get the latest version you might want to use a small bash script like this

py_versions=($(ls /usr/bin | grep 'python[0-9]\.[0-9]$'))
${py_versions[-1]} your_script.py

This searches the output of ls for all python versions and stores them in py_versions. Thankfully the output is already sorted, so the last element in the array will be the latest version.

And, it won't work unless you know how to execute your Python script. simply open your Terminal or Command line and type in Python(or Python 3 your new Python script as if it were present in your current Python code. To execute your Python script(python_script.py) open command line and write python3 python_script.py Replace python3 with python if your Python version is Python2.x. Here’s what we saved in our python_script.py for i in range(0,5): print(i)


Also learn how to run python script using command-line, interactively, IDE or text 2 + 3 5 print('Welcome to the world of PYTHON') Welcome to the Python code in your current working directory and also the file should be in  Resolved this. Moved my Python folder into C: and made it point to the following: cmd /k "C:\Python\Python37\python.exe "$(FULL_CURRENT_PATH)" This runs the python script in a command prompt, rather than a .py executable. But it works just the same.


In your Terminal, run this file with using the following command, and you should see the corresponding output: $ python3 echo_adelle.py Hello from the other side! To run Python scripts with the python command, you need to open a command-line and type in the word python, or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!


We start a Python interactive shell in a terminal with the command "python". It might be necessary to use "python3" to get a Python3 shell: $ python3 (base)  A Python script can also be started like any other script under Linux, e.g. Bash scripts. Two steps are necessary for this purpose: The shebang line #!/usr/bin/env python3 has to be added as the first line of your Python code file.