Run python script without calling the interpreter [duplicate] - python

Question: In command line, how do I call a python script without having to type python in front of the script's name? Is this even possible?
Info:
I wrote a handy script for accessing sqlite databases from command line, but I kind of don't like having to type "python SQLsap args" and would rather just type "SQLsap args". I don't know if this is even possible, but it would be good to know if it is. For more than just this program.

You can prepend a shebang on the first line of the script:
#!/usr/bin/env python
This will tell your current shell which command to feed the script into.

You want a shebang. #!/path/to/python. Put that on the first line of your python script. The #! is actually a magic number that tells the operating system to interpret the file as a script for the program named. You can supply /usr/bin/python or, to be more portable, /usr/bin/env python which calls the /usr/bin/env program and tells it you want the system's installed Python interpreter.
You'll also have to put your script in your path, unless you're okay with typing ./SQLsap args.

Assuming this is on a unix system, you can add a "shebang" on the top of the file like this:
#!/usr/bin/env python
And then set the executable flag like this:
chmod +x SQLsap

Another way to do this would be to package your python script.
Then all you would have to do is run $ pip install package-name and simply running $ package-name from any directory would execute your python script.
I would recommend this method since it enables you to quickly share/install/use/remove your python script.
I wrote a simple script QuickPackage that can instantly create and upload python package for your awesome script by just entering the path of your python script.
Example:
$ pip install quickpackage
Usage :
Usage
Simply run quickpackage and enter the path of your python script.
Example:
❯ quickpackage
Enter name: quickpackage
Enter version: 1.1
Enter description: Instantly create and upload python package for your script
Enter github url: https://github.com/yask123/quick-package
enter author: Yask Srivastava
Enter email: yask123#gmail.com
Path of python script file: run.py
running register
....
running upload
Submitting dist/quickpackage-1.1.tar.gz to https://pypi.python.org/pypi
Server response (200): OK
Now simply execute your script by running$ quickpackage (In this case)

On Windows or DOS you can come close by putting your python code into a .BAT file like this.
rem = ''' this line interpreted by BAT and PYTHON
#REM BAT-ONLY code begins now inside PYTHON comment
#echo off
python "%~dpnx0" %*
goto :eof
#REM CMD-ONLY code ends
'''
import sys
print sys.path
Unfortunately, I can't get rid of the first line message on the output, but you could always change the text after ''' to be something like the application's name and people wouldn't notice.
Ideally, you wouldn't put lots of Python code in the .BAT file but would import a .py file and then run its .__main__ method.

In unix, you use a shebang line at the start of your script:
#!/usr/bin/env python
make the file executable:
chmod +x arbitraryname
and put it in a directory on your PATH (can be a symlink):
cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname
In windows, files like *.py has been set to be open with python.exe by default. (If not, you can set manually.) So you can run *.py files in console directly.
Note that:
In windows, text new line code is "\r\n", but in unix, it is "\n". If your python script is windows format, then executing in unix will report "No such file or directory" error.
To repair this problem, just replace all "\r\n" with "\n" in unix will be ok.

Related

Why do we first need the cd command when opening the .py file in the terminal by python?

Why can't we open the .py file with its path by python?
noob asking. Apologies if I am stupid.
You don't.
If you have the file /home/user/scripts/tests/main.py, with the following content:
print("Hello World")
Running python3 /home/user/scripts/tests/main.py will run the python script as expected.
There may be an issue with your PATH or with how you're entering the path to the file
You probably use similar construction:
myscript.py
It means, that terminal consider your input as a command, and tries to find in in $PATH.
Firstly you need to use absolute or relative addressation:
./myscript.py
# or
/path/to/file/myscript.py
Secondly, you need to make sure, your script has an executable bit:
chmod +x myscript.py
Thirdly, make sure you use shebang in the first line of your scriptb:
#!/usr/bin/python
Shebang explains to your shell, which app should process the file.
Otherwise you'll need to run it via python directly:
python myscript.py

Is there a way to execute .py scripts from path in windows?

I would like to use a python script anywhere within command prompt. This is possible in unix, but I couldn't find anything for windows. Do I have to convert it to .exe?
Edit: not sure why this is being downvoted, maybe it's a silly question but I can't find any similar threads here and I can't be the first person to want to execute .py scripts from their path...
Edit 2: I think my wording was unclear. I would like to know a method to execute python scripts in Windows without needing to specify python path/to/script.py every time. Here is a solution on Linux where the shebang statement invokes the python interpreter, and the script in question can be easily placed in bin: How do I install a script to run anywhere from the command line? . Does there exist a solution like this for Windows?
Here's a solution for running myScript.py:
add to the myScript.py file a first line #!python (or #!python3 if you want to use Python 3)
For instance:
#!python
import sys
sys.stdout.write("hello from Python %s\n" % (sys.version,))
change the "opens with" property of myScript.py to py.exe (to find where it is use where py-- I have it in C:\Windows\py.exe)
put the script myScript.py somewhere in your Windows path
and now you should be able to type myScript.py anywhere in a command prompt and run the Python script with your chosen Python version.
See: https://docs.python.org/3.6/using/windows.html#from-the-command-line

Run python script like a Linux command

I want to run my python function in console like this:
my_function_name
at any directory, I tried to follow arajek's answer in this question:
run a python script in terminal without the python command
but I still need to call my_function_name.py to make it work. If I call only my_function_name, the console will inform me command not found . I also tried to add a symbolic link with this answer: Running a Python Script Like a Built-in Shell Command but it failed
sudo ln -s my_function_name.py /home/thovo/test/my_function_name
ln: failed to create symbolic link ‘/home/thovo/test/my_function_name/my_function_name.py’: File exists
Change the name of the script to no longer have the .py extension.
Add this shebang to the top of your file: #!/usr/bin/env python and remove the file extension.

Anjuta IDE - Simple Python Question

I'm new to Linux, Python and the Anjuta IDE.
I have created a new file called hello.py. This is the contents of that file:
#!/usr/bin/env python
print "Hello World!"
All I want to do is run this in the terminal. I go to Run > Execute but I get the following error message:
Program 'home/joe/Programming/Python//hello.py' does not have execution permission
How do I get this really simple program to run?
Thanks,
open a shell, cd to the folder where the file is located and execute chmod +x hello.py.
ZeissS' solution will work and is generally preferred to this, but for the sake of completeness, you could also open a shell, cd to the appropriate directory, and type:
python hello.py

Calling a python script from command line without typing "python" first

Question: In command line, how do I call a python script without having to type python in front of the script's name? Is this even possible?
Info:
I wrote a handy script for accessing sqlite databases from command line, but I kind of don't like having to type "python SQLsap args" and would rather just type "SQLsap args". I don't know if this is even possible, but it would be good to know if it is. For more than just this program.
You can prepend a shebang on the first line of the script:
#!/usr/bin/env python
This will tell your current shell which command to feed the script into.
You want a shebang. #!/path/to/python. Put that on the first line of your python script. The #! is actually a magic number that tells the operating system to interpret the file as a script for the program named. You can supply /usr/bin/python or, to be more portable, /usr/bin/env python which calls the /usr/bin/env program and tells it you want the system's installed Python interpreter.
You'll also have to put your script in your path, unless you're okay with typing ./SQLsap args.
Assuming this is on a unix system, you can add a "shebang" on the top of the file like this:
#!/usr/bin/env python
And then set the executable flag like this:
chmod +x SQLsap
Another way to do this would be to package your python script.
Then all you would have to do is run $ pip install package-name and simply running $ package-name from any directory would execute your python script.
I would recommend this method since it enables you to quickly share/install/use/remove your python script.
I wrote a simple script QuickPackage that can instantly create and upload python package for your awesome script by just entering the path of your python script.
Example:
$ pip install quickpackage
Usage :
Usage
Simply run quickpackage and enter the path of your python script.
Example:
❯ quickpackage
Enter name: quickpackage
Enter version: 1.1
Enter description: Instantly create and upload python package for your script
Enter github url: https://github.com/yask123/quick-package
enter author: Yask Srivastava
Enter email: yask123#gmail.com
Path of python script file: run.py
running register
....
running upload
Submitting dist/quickpackage-1.1.tar.gz to https://pypi.python.org/pypi
Server response (200): OK
Now simply execute your script by running$ quickpackage (In this case)
On Windows or DOS you can come close by putting your python code into a .BAT file like this.
rem = ''' this line interpreted by BAT and PYTHON
#REM BAT-ONLY code begins now inside PYTHON comment
#echo off
python "%~dpnx0" %*
goto :eof
#REM CMD-ONLY code ends
'''
import sys
print sys.path
Unfortunately, I can't get rid of the first line message on the output, but you could always change the text after ''' to be something like the application's name and people wouldn't notice.
Ideally, you wouldn't put lots of Python code in the .BAT file but would import a .py file and then run its .__main__ method.
In unix, you use a shebang line at the start of your script:
#!/usr/bin/env python
make the file executable:
chmod +x arbitraryname
and put it in a directory on your PATH (can be a symlink):
cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname
In windows, files like *.py has been set to be open with python.exe by default. (If not, you can set manually.) So you can run *.py files in console directly.
Note that:
In windows, text new line code is "\r\n", but in unix, it is "\n". If your python script is windows format, then executing in unix will report "No such file or directory" error.
To repair this problem, just replace all "\r\n" with "\n" in unix will be ok.

Categories

Resources