2

I want to make a file executable, but I don't know how to do that and have the idle prompt appear afterward. Is the best way to just open idle and then import the file? I'm using python3 on raspbian jessie. Can you add flags like -i when you make the file executable? Please help.

Update I found that adding the -i flag in the beginning of the file works.

jath03
  • 436
  • 1
  • 4
  • 13

2 Answers2

1
  1. Keep all your executable files in a folder, I usually keep it in /home/pi/. Your folder can be called for example exec

    mkdir exec
    
  2. move your Python3 script into this folder:

    mv myScript.py ~/exec/
    
  3. In your myScript.py add the shebang line on the top-most part of script

    $ nano myScript.py
    
    ## add this line on top of file
    !/usr/bin/python3
    

CTRL+O then press Enter and in the end CTRL+X

  1. give executable rights to your file:

    $ chmod +x myScript.py

Additional : you can also remove the .py from the file and keep it just myScript

    mv myScript.py myScript

You have your script as an executable you can trigger it by doing a simple

   ./myScript

Pausing Scripts

I think this is inherent to the script you have written, you can use time.sleep in your script to pause the working and do something in the meantime for e.g. print something or do nothing.

Adding Arguments

You can use configParser module in Python to give arguments to your script

Your executable will now run as:

./myScript -arg1 -arg2

Links

Python3 ConfigParser

time.sleep() for Pausing Python scripts

Edit:

Parse Flags to a Python Executable

Shan-Desai
  • 1,541
  • 2
  • 13
  • 28
0

I am running python 3.7.3 on a Raspberry Pi 3. I found that I needed to change the line at the top of the file from

!/usr/bin/python3

to

#!/usr/bin/python3

in order to get it to work.

Mats Karlsson
  • 979
  • 5
  • 11