Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When invoking 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 regardless. However, I strongly suspect there are more differences, and I want to know what they are.

I have a Django website, and "python manage.py syncdb" works just fine, but "./manage.py syncdb" creates a broken database for some reason that remains a mystery to me. Maybe it has to do with the fact that syncdb prompts for a superuser name and password from the command line, and maybe using "./manage.py syncdb" changes the way it interacts with the command line, thus mangling the password. Maybe? I am just baffled by this bug. "python manage.py syncdb" totally fixes it, so this is just curiosity.

Thanks.

Edit: Right, right, I forgot about the necessity of the shebang line #!/usr/bin/python. But I just checked, "python manage.py syncdb" and "./manage.py syncdb" are using the same Python interpreter (2.7.2, the only one installed, on Linux Mint 12). Yet the former works and the latter does not.

Could the environment variables seen by the Python code be different? My code does require $LD_LOADER_PATH and $PYTHON_PATH to be set special for each shell.

like image 979
SerMetAla Avatar asked Mar 22 '12 16:03

SerMetAla


People also ask

What is script py in Python?

A script is a . py file that contains multiple Python commands that are usually run sequentially. Scripts are usually used to conduct a specific task, or solve a specific problem. Scripts often use functions and other programming tools that are contained in modules.

What is the difference between py and Python in CMD?

The python command is the standard installed alias for python2 or python3. the py command is a non-standard command line tool which seem to invoke Python3 by default and gives command line access to python constructs . The py command uses it's own method for identifying which python version to use.

Is a py file a script?

Python is an interpreted object-oriented programming language, and PY files are program files or scripts created in Python. Text editors can be used to create and modify it, but a Python interpreter is required to execute it.

Why does Python work but not py?

py is itself located in C:\Windows (which is always part of the PATH ), which is why you find it. When you installed Python, you didn't check the box to add it to your PATH , which is why it isn't there. In general, it's best to use the Windows Python Launcher, py.exe anyway, so this is no big deal.


1 Answers

Calling ./script.py 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 whatever path to the python interpreter is used. If it resolves to the same Python interpreter that is called by just

python

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

like image 78
Sven Marnach Avatar answered Sep 28 '22 09:09

Sven Marnach