How can I run a Python 3 script?

2

2

I want to run a Python 3 script.

I have looked in the official doc and FAQ, and they are the typical loss. I was looking for answers, I ended up with more questions.

I have Windows and Linux. On Windows, I don't have Python. On Linux, I have Python, but it is Python 2.6.8. I have tried to run my script with this Python, but this gives an error.

I want something simple : a Python 3 that I would put somewhere in my disk and that I would use to run my script. Where can I find this ?

I am looking for a solution for Windows or Linux.

Thank you.

Nicolas Barbulesco

Posted 2015-01-06T10:58:18.387

Reputation: 371

1Are you sure you do not have python3 installed on your linux box ? Several distros ship it but it is not the default python used to run scripts. – NaeiKinDus – 2015-01-06T11:02:25.820

@NaeiKinDus - No, I am not sure. I have tried the command python3 but the command is not found. – Nicolas Barbulesco – 2015-01-06T11:10:11.657

1

you could try the following project for linux: https://code.google.com/p/pts-mini-gpl/wiki/StaticPython You can directly download the Python binary and run it without having to install it on your system.

– NaeiKinDus – 2015-01-06T12:58:59.473

@NaeiKinDus - I have already managed to use the Portable Python suggested by Karel. But StaticPython is good to know. – Nicolas Barbulesco – 2015-01-06T13:09:44.530

Answers

2

You have registered for Ask Ubuntu Q&A, so I'll provide the command for Ubuntu. In Ubuntu open the terminal and type:

sudo apt install python3 idle3  

python3 is already installed by default in Ubuntu, I have added python3 to the command for the sake of generality with other Linux distributions. IDLE 3 is an Integrated Development Environment for Python 3. Open IDLE 3 and then open your Python script from the menu in IDLE 3 -> File -> Open.

In Windows IDLE 3 is bundled with the Python 3 installation file, and the instructions for opening a Python 3 script in IDLE 3 are the same.

You can also install Python 3 in Windows as a portable app. Portable Python is pre-configured to run directly on the Windows OS from any USB storage device, enabling you to have, at any time, a portable programming environment. Portable Python is available for both Python 2 and Python 3. The download options for Portable Python offer you a choice of downloading optional additional Python packages for scientific computing, etc. The installed size of Portable Python (Python 3), based on the selected packages, is between 63MB and 260MB.

karel

Posted 2015-01-06T10:58:18.387

Reputation: 11 374

On Windows, I did not succeed with the Python 3 installer. It asks me for admin auth, even when I choose to install "just for me". – Nicolas Barbulesco – 2015-01-06T11:27:32.250

On Linux, I don't want to run this kind of command. I have an integration server, and I don't want to install anything in the system. I want to have and use Python 3 in a folder. – Nicolas Barbulesco – 2015-01-06T11:31:09.423

Portable Python is pre-configured to run directly on the Windows OS from any USB storage device, enabling you to have, at any time, a portable programming environment. There is a way to run Python scripts as a regular user without administrative privileges in both Windows and Linux by running the scripts from the Python virtual environment creator (virtualenv). Using virtualenv will improve security when running untrusted Python scripts. You can also install Python 3 itself inside a virtualenv Python virtual environment. However you still need administrative privileges to install virtualenv. – karel – 2015-01-06T11:34:02.377

2Portable Python for Python 3 is currently at version 3.2.5.1. – karel – 2015-01-06T11:37:44.310

Karel, I understand that Portable Python and virtualenv are two different things. I am interested in Portable Python ! – Nicolas Barbulesco – 2015-01-06T11:39:24.607

The Portable Python works fine ! Not enough user-friendly to my taste, but I managed to make it run my script. – Nicolas Barbulesco – 2015-01-06T12:59:44.177

1

On many systems, "python" defaults to "python2", but you can run a script under python 3 simply by doing

python3 myscript.py

Issuing

which python3

will tell you if that's going to work.

Also, if the script includes an appropriate shebang,

./myscript.py

is supposed to work...

Nemo

Posted 2015-01-06T10:58:18.387

Reputation: 1 050

On Linux, the command python3 is not found. Neither on Windows. – Nicolas Barbulesco – 2015-01-06T11:06:21.513

Regarding launching the script with "./" , it fails : /usr/bin/env: python3: No file or folder of that type. – Nicolas Barbulesco – 2015-01-06T11:09:18.450

Then you definitely have to install python 3. I see you're discussing that in the comments to the other answers. – Nemo – 2015-01-06T14:50:42.407

1

For Windows:

  1. You need to install Python 3 from here
  2. Follow the setup instructions, making sure to check the box that says whether to add it to the system PATH variable
  3. Go into cmd, cd to the location of the script, and type python script.py, replacing script with your filename or open the script with Python Launcher which will have been installed via the installer

To make the script, you can use Python IDLE which was also installed.

ᔕᖺᘎᕊ

Posted 2015-01-06T10:58:18.387

Reputation: 5 559

This link does not give Python 3, it gives an installer.msi. Coming from the Mac, I am not fond of installing programs. Anyway, I have (re-)tried the installer, to no avail. Even when I choose to install "just for me", the installer asks me for admin auth, and it fails. Why can't those people just provide a Zip file ? – Nicolas Barbulesco – 2015-01-06T11:41:50.057

@NicolasBarbulesco So you want to run python without installing? Like a portable version?? – ᔕᖺᘎᕊ – 2015-01-06T11:43:09.333

Yes ! I am looking into @karel's solution. – Nicolas Barbulesco – 2015-01-06T11:45:12.320

1OK, I didn't see @karel's comments - I agree about using Portable Python, it works fine on a USB for me :) – ᔕᖺᘎᕊ – 2015-01-06T11:46:28.173

-1

There is a difference from Linux to Windows. On Linux you would type like this:

python3 script.py

While on Windows (once you have Python installed) chances are you would type:

py -3 script.py

Comparing to py -2 script.py that would run Python 2.


Note: On Windows python or python.exe would call the latest version of Python you have installed.

prosti

Posted 2015-01-06T10:58:18.387

Reputation: 99

1Huh, aren't the Windows binaries called python.exe? Where did you get py from? – slhck – 2019-01-11T12:58:24.660

I don't care, I just use it with py when I need to specify the version, but python or python.exe still works. – prosti – 2019-01-11T15:48:07.673

So it's not really required. That said, this seems to be a new feature with version 3.6: https://docs.python.org/3/using/windows.html

– slhck – 2019-01-11T21:21:50.410