Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:52:37+00:00 2026-06-01T12:52:37+00:00

Is there a portable way to run a python script from a shell without

  • 0

Is there a portable way to run a python script from a shell without writing its full path?

For example in Linux, I would like while in my home directory

cd ~

to be able to run a python script called run.py that is in say, ~/long/path/to/run.py, but I want to run it by simply typing

python run.py

instead of

python ~/long/path/to/run.py

I would hope for some kind of search path list that contains several directories just like the PATH variable, so that python run.py runs the first run.py it encounters in one of the directories.

I have considered turning run.py into an executable and adding its directory the system PATH variable, but could not find a portable way of making a python script executable.

EDIT

One year later after asking it, I am a bit less noob, and I see that my question was not very clear and did not make much sense, so after a question upvote I’ll clarify some things.

1) Portable.

When I asked this I said portable. However what portable means is not clear in this case, and I did not give much emphasis to it.

  • the platforms: should work on POSIX (Linux, MacOS, etc.) and Windows

  • this still does not make much sense since windows uses cmd.exe, and POSIX uses sh, so each one could run the commands with a different syntax. So let’s say that the most portable thing possible would be to feed the same input to both sh and cmd.exe, running the python script in both cases. In this case, you could run the same command from an ANSI C system function, which uses sh on POSIX and cmd on Windows. ANSI C being one of the few things that is common to Windows and POSIX, the question makes some sense in that case.

2) Executable

Next, the phrase turning run.py into an executable, is not very clear. By that I was talking about the Linux strategy of chmod +x run.py, add a shebang #!/usr/bin/env python, and adding its directory the system add ~/long/path/to/ the PATH enviroment variable. But then this won’t work for windows because windows does not support an executable file metadata property like Linux and because /usr/bin/env does not necessarily exist in Windows.

3) Extension

Finally, in my head I was hoping for a solution that does not specify what kind of file run is, so that if someday we decide to make it, say, a perl file, no interfaces would change.

Therefore, writing run.py would be bad because it would specify the filetype; it would be better to be able to write just run

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:52:38+00:00Added an answer on June 1, 2026 at 12:52 pm

    If the directory containing run.py is on the module search path (for example, PYTHONPATH environment variable), you should be able to run it like this:

    python -m run
    

    Here is the documentation on the -m command line option:

    -m module-name
    Searches sys.path for the named module and runs the corresponding .py file as a script.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Python, is there a portable and simple way to test if an executable
Possible Duplicate: Is there a portable way to get the current username in Python?
Is there any portable way of doing branch prediction hints? Consider the following example:
Is there a portable way of getting thread and/or process identifier (string, int, ...)
Is there a (portable) way to rotate text in a HTML table cell by
Is there a portable way to convert a UTF-8 string in C to upper
Is there a portable, not patent-restricted way to play compressed sound files in C#
I'm running portable python and portable gvim. When I run gVimPortable, I want it
Suppose you run the application 'app' by typing 'app', rather than its absolute path.
I'm writing (under linux or windows+cygwin) a java program that needs to run a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.