0

Okay, I am tearing my hair out trying to do this. I've read through dozens of webpages and they've all given me contradictory information, and none of what they've told me to do has worked.

I have a folder full of scripts I downloaded that will only work if they're part of the pythonpath. I want to either move the folder itself into the default path or temporarily (not permanently) add /desktop/search to the path.

What is the default path, and how would I do the latter?

3
  • What version of Python do you use? How did you install it? Commented Oct 5, 2012 at 16:47
  • 2.7, it was already installed on my computer (as I believe is normal for OS X). Commented Oct 5, 2012 at 16:50
  • Once you've allowed time for through answers, you should accept the most helpful answer. Commented Oct 5, 2012 at 20:52

2 Answers 2

1

You can add this to the beginning of your python file.

import sys    
sys.path.append("/Users/<username>/Desktop/search")
Sign up to request clarification or add additional context in comments.

7 Comments

Didn't fix it... am I correct in thinking that if a file is in the path, I will be able to type "python filename.py" into terminal without specifying a location?
For that you need to change your system PATH as opposed to your PYTHONPATH. Open up terminal and type export PATH=$PATH:/Users/<username>/Desktop/searchObviously make sure your replace <username> with your username.
Will that change the path, or just add another path? And will it be permanent or temporary?
It will change the path by adding another path. It will be temporary. It will only exist in your current shell.
Ohh nevermind. I don't believe that's possible. PATH is used for finding the executable. You should type python $HOME/Desktop/search/file.py
|
1

The canonical UNIX approach would be to ensure each of the scripts has a proper shebang line, perhaps:

#!/usr/bin/env python2.7

and then installed with the proper permissions (i.e. including execute permission) in a directory on your shell search PATH, perhaps /usr/local/bin. Then you can invoke a script just with its name:

scriptname

The pythonic approach would be to install the scripts using Distutils/easy_install/pip into one of the standard site-packages locations for your Python instance and then be able to invoke a script with something like:

python2.7 -m scriptname

But that may require some work to get everything set up. The first approach is probably easier.

The bottom-line is that you can't really do exactly what you asked for, that is, be able to type python scriptname when the scriptflle can be in an arbitrary directory.

Comments

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.