0

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:

func() { 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?

2
  • 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
    CommentedAug 24, 2016 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 directoryCommentedAug 24, 2016 at 3:37

1 Answer 1

2

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.

    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.