rexstubbins
Posts: 4
Joined: Thu Dec 13, 2018 4:32 pm

Running Python script from USB

Wed Dec 19, 2018 2:46 pm

This may seem like a dumb question but can a Python script reside on a USB memory stick and then be run from the USB device? I think the answer is probably yes however I don't have a USB device at the moment to try it with and cannot find an answer anywhere on the forums.

User avatar
neilgl
Posts: 764
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Running Python script from USB

Wed Dec 19, 2018 3:18 pm

Yes

n67
Posts: 938
Joined: Mon Oct 30, 2017 4:55 pm

Re: Running Python script from USB

Wed Dec 19, 2018 3:20 pm

I'm not au courant with Python, but I think the answer to your question is "No, by default". But you can fix it.

I hit this recently, when I tried to build a program on a USB device. I unpack the TGZ file to a directory on the flash drive, then tried to do the usual "./configure". But it wouldn't find the script. Note, BTW, that the only thing on the flash drive was the "configure" shell script - the actual executable (/bin/sh) was still on the SD card, so you'd think it would work, But it didn't. I quickly figured out that the problem was that external media gets mounted by default with the "noexec" flag.

However, I was able to get things working by doing (as root):

Code: Select all

# mount -o remount,exec /dev/MyFlashDrive
After that, ./configure worked as expected.

I assume the situation with Python will be similar.
"L'enfer, c'est les autres"

G fytc hsqr rum umpbq rm qyw rm rfc kmbq md rfgq dmpsk:

Epmu Sn!

J lnacjrw njbruh-carppnanm vxm rb mnuncrwp vh yxbcb!

rhubarbdog
Posts: 64
Joined: Mon Dec 11, 2017 9:58 pm

Re: Running Python script from USB

Wed Dec 19, 2018 7:10 pm

Yes your program is a script. The executable python / python3 resides in /usr/bin on the raspberry pi

User avatar
rpdom
Posts: 14985
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Running Python script from USB

Wed Dec 19, 2018 7:17 pm

To clarify the conflicting posts above, (the ones that said "yes" are closest). Running the script by just typing the path name in the terminal will probably not work, but typing "python path-to-script" or "python3 path to script" should work.

"path-to-script" will probably be something like /media/pi/ABCDEFG/something/scriptname.py

User avatar
thagrol
Posts: 1777
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Running Python script from USB

Thu Dec 20, 2018 4:51 pm

It's all in the mount options.

If the drive was mounted with "noexec" which is the default for several filesystem types, even if the script/file is flagged as executable the system won't allow it to be run directly. It doesn't matter whether it's python, bash, compiled C etc.

There are three ways around this:
  • As rpdom suggests, run it indirectly, though this is no use for compiled code:

    Code: Select all

    python /path/to/file
    bash /path/to/file
  • As n67 suggests remount the drive with the exec option:

    Code: Select all

    sudo mount -o exec /path/to/mount_point
  • Change the stored mount options for the device. In /etc/fstab for devices mounted at boot time, not sure where for devices mounted in the desktop or File Manager. (I don't use them)
This space unintentionally left blank.

Return to “Beginners”