Imagine I have a script, let's say my_tools.py
that I import as a module. But my_tools.py
is saved twice: at C:\Python27\Lib
and at the same directory from where the script is run that does the import.
Can I change the order where python looks for my_tools.py
first? That is, to check first if it exists at C:\Python27\Lib
and if so, do the import?
You can manipulate sys.path
as much as you want... If you wanted to move the current directory to be scanned last, then just do sys.path[1:] + sys.path[:1]
. Otherwise, if you want to get into the nitty gritty then the imp module can be used to customise until your hearts content - there's an example on that page, and one at http://blog.dowski.com/2008/07/31/customizing-the-python-import-system/
You can modify sys.path
, which will determine the order and locations that Python searches for imports. (Note that you must do this before the import statement.)
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