When calling a Python script, what is the difference between "./script.py" and "python script.py"

One difference is that "./ script.py" only works if script.py is executable (as in file permissions), but "python script.py" works independently. However, I strongly suspect that there are more differences, and I want to know what they are.

I have a Django site, and "python manage.py syncdb" works fine, but "./manage.py syncdb" for some reason creates a broken database, which remains a mystery to me. Perhaps this is due to the fact that syncdb requests the username and password of the superuser from the command line, and perhaps the use of "./manage.py syncdb" changes the way it interacts with the command line, thereby changing the password. May be? I'm just puzzled by this mistake. "python manage.py syncdb" completely fixes it, so this is just curiosity.

Thanks.

Edit: Right, I forgot about the need for the shebang line #! / Usr / bin / python. But I just checked: "python manage.py syncdb" and "./manage.py syncdb" use the same Python interpreter (2.7.2, the only one installed on Linux Mint 12). But previous works and the latter do not.

Can the environment variables seen by Python code be different? My code requires $ LD_LOADER_PATH and $ PYTHON_PATH, which will be set specifically for each shell.

+6
source share
4 answers

The ./script.py call uses the "shebang" line in the script to determine which interpreter to use to run the script. Such a line might look like

 #!/usr/bin/env python 

or

 #!/usr/bin/python2.7 

or any other way to the python interpreter. If it allows the same Python interpreter, which is simply called

 python 

from the shell command line, there is no difference between ./script.py and python script.py , but the two versions may end up using different Python interpreters.

+6
source

./script.py = "Attempting to execute a file called script.py in the current shell

python script.py = "Send script.py as an argument to the first python executable in the current $PATH "

The first only works if the file has a run bit set for the user trying to execute the file and has the so-called shebang , which tells the shell how to run it.

+1
source

./script.py starts the interpreter defined in #! at the beginning of the file. For example, the first line could be #! /usr/bin/env python #! /usr/bin/env python or #! /usr/bin/python #! /usr/bin/python or something similar. If you look at which interpreter is being called, you can fix this problem.

0
source

On Linux, using the terminal, you can execute any file if the user has permission to execute by entering ./fileName . When the OS sees a valid title, for example #! /usr/bin/python #! /usr/bin/python (or perl #! /usr/bin/python ), it will call the python or perl interpreter (corresponding) to execute the program. You can use the python script.py directly because python is an executable program located in /usr/bin (or somewhere else) that is in the $ PATH environment variable, which corresponds to the executable directory.

0
source

Source: https://habr.com/ru/post/911429/


All Articles