3

I am running a python script named myscript.py on ubuntu machine. I usually use python command to run python scripts as below.

python main.py 

Recently, I downloaded a python script from a Github repository ( if need to look into the repository can find it at https://github.com/gsrivas4/mnist-gan) which asks to run script using './' as below.

./main.py 

Running python scripts second way is new to me. I am confused about when can we use './' to run scripts and is this method to run scripts used for other languages as well. Usually, I would expect name of binary such as python which will start a process before I add the name of script. This script will be fed to the process. Also, I want to understand what is the meaning of './' when we run scripts. I feel this is trivial question, but could not find much help online.

I also tried making one of my python file executable and then ran it. However, running it using ./ gave me errors for any python library import commands.

4

1 Answer 1

1

./ is simply a relative path indicating the current working directory. When executing a file that is not in your PATH it's necessary to prefix it with either the full path or a relative path, ./ is the most simple method of doing this, but it would also work if you used a full path like /path/to/script.py

The reason your python script gets errors when you execute it as:

./script.py 

rather than:

python script.py 

is because you do not have a hashbang(shebang) interpreter line at the top telling it which interpreter to use when executing the script. It's likely trying to execute it with bash or whatever shell you are using to execute the script. (See Which shell interpreter runs a script with no shebang?)


To get your script to execute properly using python add the following to the first line in the script:

#!/usr/bin/env python 

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.