I'm trying to import a module I wrote in python that just prints out a list containing numbers. The issue I'm having is that I want to be able to import it from a separate directory but the answers I have read so far don't seem to be working for my situation.
For example, given I want to import printnumbers.py from a directory in my documents folder I am supposed to implement the following:
When the file is
printnumbers.py, the module is called
printnumbers (without the
.py). Therefore use
import sys sys.path.append('/home/jake/Documents')
'/home/jake/Documents' to the end of
sys.path. The directories listed in
sys.path are searched (in the order listed) whenever an import statement causes Python to search for a module. (Already imported modules are cached in
sys.modules, so Python does not always need to search
sys.path directories to import a module...)
So if you have a file
import printnumbers will cause Python to import it provided there is no other file named
printnumbers.py in a directory listed in
sys.path ahead of
Note that injecting directories into
sys.path is not the usual way to set up Python to search for modules. Usually it is preferable to add
/home/jake/Documents to your PYTHONPATH environment variable.
sys.path will automatically include the directories listed in the PYTHONPATH environment variable.