Import a script in IDLE

Nick Heiner picture Nick Heiner · Mar 3, 2011 · Viewed 15.3k times · Source

This has probably been asked before, and is really basic, but:

I'm using Windows 7. I have Idle for Python 2.4.4 and 3.1. I have some scripts residing in arbitrary locations on my file system. I'd like to import them and play around with their types. How can I do so?

In Ubuntu, on the command line, import scriptname works if the directory I called python from contains scriptname. How would I import a script from somewhere else?

Answer

stderr picture stderr · Mar 3, 2011

In idle you could append a path containing your scriptname.py file.

>>> import pprint
>>> import sys
>>> print pprint.pprint(sys.path)
# you could just move your scriptname.py to a directory in the sys.path list
>>> sys.path.append(r"C:\Users\You\")
>>> import scriptname

You could also customize the PYTHONPATH environment variable in windows to include other directories like "C:\Users\You\lib"