When I have a package test, I'm used to import another file script from it as:
import test.script
and libraries as:
import library
This works as long as I don't need any library that's name corresponds with any of my scripts. When it happens, the script is loaded, instead of the library. Is there a way to force local imports to use the full path to file as I'm using it? Or is there another way to prevent the name clash, without renaming the script?
When you name a "script" with the same name as a dependency, you have intercepted the lookup for that name.
You could modify your PYTHONPATH i.e.
sys.path, putting your dependencies in front of your "scripts" but then you've intercepted your scripts - this seems like a very bad idea.Don't modify the lookup order in your
sys.path.I would not name any script with the same name as a dependency, with the sole exception that I was intending to intercept the name with something that is semantically the same thing.
You need your own namespace. Create a directory, for example
a_namespace, put your scripts in that directory, and work from the parent directory. For example:And you can still get to your dependencies: