I'm defining a bash function in my .bash_profile so that I can run a certain Python script from any directory. My function is something like this:

     python -i ~/Scripts/script.py

It works perfectly from my home directory, but it cant find the directory ./Scripts/script.py if run from anywhere else.

The error message is:

/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'Projects/sym.py': [Errno 2] No such file or directory

Shouldn't the ~ initially direct to the home directory anyways?

  • 3
    It is "something like this" or it looks exactly like that? Can you paste your actual code? And can you paste in the exact error message you receive? – larsks Aug 24 '16 at 3:08
  • Code is exactly like that semantically. The error message /usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'Projects/sym.py': [Errno 2] No such file or directory – orange_juice Aug 24 '16 at 3:37

From the error message:

/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'Projects/sym.py': [Errno 2] No such file or directory

you can see that your ~/Scripts/script.py tries to reference another file using a relative path Projects/sym.py.

Most likely you are calling the func function when your current directory is set to one that does not contain Projects/sym.py.

If your intention is to include a file from ~/Projects/sym.py you need to use an absolute path in the Python code or change the current directory to ~ inside the func prior to calling python.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.