1

I have a python script fetchcreative.py which typically fetches some data from db . The script has execute permission . When i run the script using

./fetchcreative.py rtb:3778:4019617065 

it fails with ImportError: No module named psycopg2. But when i run using

python fetchcreative.py rtb:3778:4019617065

it works fine. Just want to understand , what is going on here ? Please point to corresponding reference where i can learn more about it .

4
  • What is the first line in fetchcreative.py? Commented Sep 14, 2017 at 18:50
  • 1
    are you using windows or Linux? Commented Sep 14, 2017 at 18:51
  • With a command line of ./fetchcreative.py (forward slash) I have assumed a unix derivative. Commented Sep 14, 2017 at 18:53
  • yeah, ok ./script.py doesn't work on windows. Commented Sep 14, 2017 at 18:54

1 Answer 1

1

./fetchcreative.py will use the shebang (#!) magic at the beginning of the file to determine which python to run.

Usually it is best to have:

#!/usr/bin/env python

at the beginning of a script.py to get python from the environment (i.e. from $PATH) which should match (though aliases and other actions could make it different):

$ python script.py

However, if the #! is hard-coded to a specific python it may not match python on the command line.

Sign up to request clarification or add additional context in comments.

2 Comments

I had #!/usr/bin/python at the beginning of the file and it was not working , changing it to #!/usr/bin/env python worked , but i can python inside /usr/bin . What is the explanation for this ?
You haven't installed psycopg2 in the system python, if you type which python it will show which python you have install psycopg2 into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.