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?
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"
To import a script from IDLE, you can do:
>>> import os
>>> os.chdir('C:\\Users\\You\\Some\\Arbitrary\\Path')
>>> import scriptname
Keep in mind that you will need to call constructors with scriptname.
prepended, like scriptname.myClass(...)
If you change something in the script, you will need to reload it like this:
>>> import imp
>>> imp.reload(scriptname)
(There is a simpler way if you just want to play around with types from one script, and if the script only contains function and class definitions (no running code). Then you can simply open the script in IDLE and go to Run>Run Module
. When you use this method, it is not necessary to put scriptname.
before constructors.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With