1

I have to run a few python scripts and was trying to create a bash script to do it for me. The bash script is as follows:

#!/bin/bash    

FILE=$(ls | grep .\.py)
for f in $FILE
do
    python2.6 $f
done

And the following error pops up for each of python calls:

SyntaxError: invalid syntax
Traceback (most recent call last):
  File "script.py", line 2, in ?
    import requests
  File "/[some path]/python/local/lib/python2.6/site-packages/requests-2.0.0-py2.6.egg/requests/__init__.py", line 53
    from .packages.urllib3.contrib import pyopenssl

Any ideas would help a lot. Thanks for your time.

EDIT:

Output from

import sys
print sys.version
...

2.6.6 (r266:84292, Nov 21 2013, 10:50:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)]
4
  • The problem seems to be in the Python scripts. BTW, what's the problem with for f in *.py? Commented Mar 24, 2014 at 6:34
  • The python scripts work fine if I run them individually from the console. Python scripts that don't have line import requests work fine. $FILE is just there for when I change the directory. Commented Mar 24, 2014 at 6:37
  • 2
    Is it probable that your python scripts start with #!/usr/bin/python and pick a different python interpreter ? Commented Mar 24, 2014 at 6:43
  • @sateesh No, none of them have a shebang. Commented Mar 24, 2014 at 6:51

1 Answer 1

1

Can you go into the interpreter and do import requests and it works?

Can you test which version of python you are calling by doing an import sys, print sys.version before the import requests? Also possibly check your pythonpath variables to make sure you aren't improperly mixing different versions of python.

Also, you seem to be using a rather old version of requests. It may make sense to upgrade to a newer version (or at least check through the issues to see if anyone else has encountered this).

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

2 Comments

import requests works inside the interpreter. I put the output from print sys.version in the main post in an edit. I'm using my school's machines so I'm stuck with this version of requests.
@MrDiggles - Is the sys.path significantly different inside the interpreter (where import requests works) than as called from the bash script (where import requests doesn't work?) And again does import sys and print sys.version, print sys.path give the same results in a python script run from a bash file as it does when called from the interpretter? If paths are wrong somehow, you can add missing values to sys.path manually, or set a bash environmental variable PYTHONPATH or create *.pth files. As a last resort look into virtualenv and install requests locally.

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.