Hello everyone,

I search for this answer, and yeah, it looks like many asked
this question before, but my answer was not on those posts.
now i would liek to double ask the same question again...
how can i invoke a python script from command pronpt?
this is the way i'am doing it:

C:\Python26\python.exe MyScript.py

Now, i understand that python need the whole path for the script
in order to process the python script, am i right? please some one
i some advices with this, thanks in advance.

Dani AI

Generated

Short practical summary: the explicit call that used works, but it’s easiest in the long run to make the interpreter callable by name and/or provide a small launcher so full paths aren’t needed. ’s “put a bin on PATH” idea is the most portable Windows approach; ’s note about .pyw/pythonw for GUI apps is also correct (use when a console window is unwanted). Below are concise, safe ways to automate launching on both Windows and Ubuntu without retyping full paths.

Make Python callable from any CMD

  • Add the Python install folder and its Scripts folder to the system PATH (or check “Add Python to PATH” when installing). To make .py files runnable by name, associate the extension to the interpreter:
assoc .py=Python.File
ftype Python.File="C:\Path\To\python.exe" "%1" %*
  • Alternatively use the Windows “py” launcher (if present) to select versions: py -3 myscript.py.

Small Windows launcher (place next to the script; double‑click runs it)

REM run-myscript.bat
SETLOCAL
SET PY=python
"%PY%" "%~dp0\myscript.py" %*
ENDLOCAL

Ubuntu (no terminal typing)

  • Put a shebang that targets the desired interpreter, make the file executable, and then run it directly:
    #!/usr/bin/env python3
    # (script body)

    then chmod +x myscript.py and ./myscript.py. To run from the desktop without opening files, create a simple .desktop launcher that executes the script inside a terminal (or configure it to run invisibly for GUI apps).

Extra options and cautions

  • For distribution on Windows, packaging tools (PyInstaller/py2exe) produce a standalone .exe.
  • Avoid running GUI apps with sudo on Ubuntu; prefer correct ownership/permissions or a launcher that requests elevation only if required.
  • Keep PATH changes limited to your user if multiple Python versions are installed; use virtualenvs for per‑project isolation.

Recommended Answers

All 8 Replies

There is many variations, I have usually python not in path, because I use both Python 2.7.2 and 3.2.2 installed. Actually I have also PyPy 1.8 also installed. So for more rigorous testing I usually use ConText editor, for which I have defined launching various versions of Python to function keys of .py file type.

From command line I use TAB key a lot to catch proper Python version by TAB completion, and then type my script name. That is I first CD to by scripts directory.

Additionallly I have one version defined associated with .py files and also .PY added to my PATHEXT environment variable, so I can call my_script.py as my_script also.

It might be time to use the editor environment that comes normally with your Python distribution. Since you are using Python26 try this batch file ...
C:\Python26\pythonw.exe -u C:\Python26\Lib\idlelib\idle.pyw

Otherwise you have to give the full file path in your commandline. That does get tiresome.

Hello guys, thanks for your help,
but this one worked for me:

cd c:\ enter
C:\>C:\Python26\python.exe C:\MyScript.py

and vegaseat whats up with those two "w" on each end?
i tried to fallow your suggestion, then i noticed the
two w on each end of the line ...Huh? what the heck?
doing it that way nothing was happening, then decide
to cut and type it in a diferent way and it did work.
Thank you

pyw file is py for which you do not want to bring up the terminal screen when launching.

Oh i see Tony, thats something good to know, thanks.
i did two tests, one on Ubuntu and on Windows, but the
idea was to be made on Windows only, so the .pyw is like
when you execute a batch silently right? oh okay, i see now.
Thank you guys

Hey Tony, let me ask you a question, i would like to write a pythong batch,
something like a batch in windows with something on it, this for example:

@ECHO OFF
TITLE C0ding
COLOR 17

I have my python script that i would like to run without going to Ubuntu
menu and open Terminal and do everything manually, how can i start this out?
Thanks Tony

link the script to one executable directory on your path, common practice is to have one directory called bin in your user directory.

You can put there one script without extension and start the script with shebang line, here is example of script from wxpython installation for launching the editra editor in Linux environment from my python27/Scripts directory:

#!/usr/bin/env python

from wx.tools.Editra.launcher import main
main()

Then you can create a launcher for the script on the desktop.

Thank you Tony, now let me explain what i have...
I have the Ubuntu CD which i've been using to run
in a laptop with Windows 7, then once in Ubuntu...
i drag and drop my python script to Ubunto desktop,
then i open terminal and start typing from there...
this is not something i do every day, but i was just
wondering how i can make my life easier with a batch.
now if this is my case, how can i accomplish the script?

This is how mine look like:
sudo python /home/ubuntu/Desktop/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.