I try use the super admin and use #!/usr/bin/python at the top however it also doesn't work
Actually, I want to open the .py document but terminal shows that print: command not found.
Just type python in your terminal to invoke the interactive shell. You gave the command in the bash terminal beginning with #, which inactivated the whole line.
If you want to run a python script, then put your code in a file like this:
script.py
#!/usr/bin/python
print "2"
Now you can run the code typing python script.py.
The first line of the code is the shebang line, which specifies which interpreter to use, so you can run your script typing ./script.py.
Related
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.
I am running the following command on terminal:
python SpellingCorrector.py
No error is thrown by terminal and it simply progresses to the next line with the same prompt showing my current working directory, rather than the Python '>>>' terminal prompt.
I would like to run a function within the program with an argument and I only have the option to attempt that as such:
[my/current/directory/]$ correction('speling')
This then throws the error
bash: syntax error near unexpected token `'speling'`
I'm guessing I need to run it with this prompt in order for it to work:
>>> correction('speling')
The Python version is 2.7.5.
Does anyone know why the prompt is not changing when I run the program or how I can run the function?
you need to execute your script in the interactive mode, like the following :
python -i SpellingCorrector.py
and then execute your function from there :
correction('speling')
You are launching the program instead of launching python interpreter.
To use interpreter, launch it as follows (without arguments):
python
then use import SpellingCorrectorto import your program to interpreter.
Now you can use its functions etc.
Please note, that import statement has no .py extension.
The python script.py command simply executes the script and returns control to the shell, which is not what you want.
You could use ipython's %run command to run a python script from inside the interpeter:
%run ./script.py
This is similar to running at a system prompt python file args, but
with the advantage of giving you IPython’s tracebacks, and of loading
all variables into your interactive namespace for further use.
I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.
In Linux, we usually add a shebang in a script to invoke the respective interpreter. I tried the following example.
I wrote a shell script without a shebang and with executable permission. I was able to execute it using ./. But if I write a similar python program, without shebang, I am not able to execute it.
Why is this so? As far as my understanding, shebang is required to find the interpreter. So how does shell scripts work, but not a python script?
My assumption is that a script without a shebang is executed in the current environment, which at the command line is your default shell, e.g. /bin/bash.
shell scripts will only work if you are in the shell you targeted ... there is not python shell ... as such python will never work without explicity calling python (via shebang or command line)
By default the shell will try to execute the script. The #! notation came later
There’a subtle distinction here. If the target is a binary or begins with a #! shebang line, then the shell calls execv successfully. If the target is a text file without a shebang, then the call to execv will fail, and the shell is free to try launching it under /bin/sh or something else.
http://en.wikipedia.org/wiki/Shebang_(Unix)
Under Unix-like operating systems, when a script with a shebang is run as a program, the program loader parses the rest of the script's initial line as an interpreter directive; the specified interpreter program is run instead, passing to it as an argument the path that was initially used when attempting to run the script.
Now when the #! is not found, every line is interpreted as native shell command. And hence if you write a bash script and run it under bash shell it will work. If you run the same bash script in say a tcsh shell it will not work without the initial #!/usr/bin/tcsh
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.