This question already has answers here:
Why do people write #!/usr/bin/env python on the first line of a Python script?
(22 answers)
Closed 7 years ago.
Okay I am new to Python, but my code does not run if the line
#!/usr/bin/python
is not present at the beginning of the file. Why is that ? What does it mean ? I thought it was used for to denote a python version if there were multiple versions installed.
#!/usr/bin/python
def main():
a = [1,2,3]
print a
if __name__ == "__main__":
main()
Omitting the #!/usr/bin/python gives the following error only if I execute it using
./test.py on Ubuntu
if however I use the python command to run then it runs fine without the /usr/bin line.
The first line of the script beginning with #! is a shebang (sometimes called a hash-bang).
The following executable path denotes which interpreter should be used to process the following code (in your case, /usr/bin/python).
If you run the script from the shell with python test.py, you don't need a shebang - the executable is python and the script is passed to it as an argument.
In Unix you can tell a file how it should be opened if it contains a script language (in your case Python).
This line is known as Shebang.
./filename is used to run executable files, to execute it you need to specify the application required.
Whereas, in using python filename.py you already specify the application to use that is python in this case.
Related
This question already has answers here:
Multiple Python versions on the same machine?
(11 answers)
Closed 3 years ago.
while there are multiple versions of Python installed on a Linux machine, is there a way to mention in the script to be open with a specific version, say, 3.8 even if we issue the #python script.py as opposed to python3.8 script.py ?
I don't want to use Linux alias command. I wanna know if that is possible to be accomplished within the script
Use shebang. #!/usr/bin/python
In the first line of the code with the serial number of Python you want at the end.
You'll need to then run your script like ./my_script.py
You can select the interpreter by adding
#!/usr/bin/env pythonXX
as the first line in your script, provided the version is in the path.
You can also invoke the interpreter directly with the script as the argument.
pythonXX script.py
Depending on your situation
pythonXX -E script.py
might be better. That way the interpreter ignores paths set by environmental variables.
For more details view
https://docs.python.org/3/using/cmdline.html
I am in the process of converting my python code from 2.7 to 3 using 2to3. It seems to convert as expected, except that my code always starts with the line #!/usr/bin/python which I expected to change to #!/usr/bin/python3 but it doesn't. Have I missed something? Is there a way to get that to happen?
#!/usr/bin/python is not a python version-dependent statement, or even python at all. It essentially instructs a shell to execute the script (file) using the python executable (program) located at /usr/bin.
The intent behind 2to3 it get you along the path of converting your code to python 3, frequently doing all the work for you. It doesn't address issues outside the python code.
It's entirely possible for /usr/bin/python to be python 3. The #! line exists let a shell execute a script using what's typically a systems default python.
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
This question already has answers here:
Running a python script from the command line in Windows
(3 answers)
Closed 6 years ago.
I have pycharm installed and do my class assignment in it and things work well. The teacher wants on to now run some of the programs in command prompt. Given that my programs and the path is here: "C:\Python27\python.exe C:/Users/sz5062/PycharmProjects/untitled1/main.py", how can I run a program that is saved the above path in command line? So, example, how would I run the main.py in command line, not shell prompt?
I have searched and searched for answers here, but I can't fine one. Thanks very much for your help.
This is the full instruction: Install Python and a programming text editor and write a program that prints one line other than 'hello world', then take two screen shots and upload them below. You should use the command line to execute the Python program you wrote in the text editor. Please do not use the IDLE Python Shell, the Python Interpreter (>>>), or a shortcut in your text editor to run the code.
When i run "python main.py", I get an error message "python is not recognized as inernal or external command".
On you Commandline enter:
C:\Python27\python.exe C:\Users\sz5062\PycharmProjects\untitled1\main.py
The above command should work from every directory.
First part is the program you start (python.exe). Second part is the argument you pass to the program(C:\Users\sz5062\PycharmProjects\untitled1\main.py). This is the path to your python code so that python.exe knows what you want to execute.
If you add C:\Python27\ to your PATH variable, you can navigate to your directory C:\Users\sz5062\PycharmProjects\untitled1\ and start your programm with:
python main.py
I'd recommend making sure that python is on your PATH (if you type python does it open a shell?) and then navigate to the C:/Users/sz5062/PycharmProjects/untitled1 directory. Once there you can do:
python main.py
and that should work.
I think I made this hard. since python was in installed on C:\python27, all I needed to run the command line was simply to write the code in a text editor, save it in the same directory and call it from there. So, essentially, c:\python27\code.py
This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 7 years ago.
I'm reading an online book on Python.It mentioned command line arguments,but I don't know what they are? Can anybody explain them for me with an example?
I don't know what your level of programming experience is, but command line arguments are a pretty common thing. In the olden days, every program you created was done in a text editor, and then run on a command line.
The command line is, in simple terms, a program built into the operating system which allows you to run programs by calling them by name. In Windows, this command line is called the Command Prompt. On other operating systems, it is usually called Terminal.
Though you may be familiar with running programs through an IDE, you can also run them from the command line. To run a python program, you could type:
python the_program.py
to run a program, assuming you have python installed and your terminal knows where it is. So command line arguments are a sort of variable/argument you send to a program that is run this way. If you say:
python the_program.py 100 hello 3.35
You can access these values from within your program, by adding
import sys
to the top, and in the body of your code by accessing the array of arguments, named
sys.argv
Command line argument are all addition information passed to the script after the name of the script.
Is you run a python script like:
python myscript.py abc 123
Then abc 123 are the command line arguments. They can be accessed in Python as a list as sys.argv.