Reputation: 716
Lets assume I have Python module in a directory and in the same directory I have a Python script that uses this module somehow.
wdir
├── module
│ └── module_stuff
└── script.py
If I execute this file in a python environment where the module is not installed everything works fine as Python imports the module from it's source code.
The question is: if I install the module in this Python environment and continue executing the script the same way as before, will Python import the module from what was installed or from the source code? What is the hyerarchy Python follows to search for each import?
Why does it matter: If I have a script repeatedly calling other Python scripts and modify the source code of the module during this execution, then there will be results that used the old version of the module and the new version as well, or even worse, executions may be missed if a bug is inserted into the module code. But this would be solved if created a development env separated from the testing env.
Upvotes: 0
Views: 624
Reputation: 1574
You can actually try this yourself: Lets build a module and install it, then modify it to see who takes preference:
mymod -- | setup.py
| mymod --| myfun.py
| __init__.py
setup.py contains:
from setuptools import setup, find_packages
setup(
name='mymod',
version='1.0',
packages=find_packages(),
)
and myfun.py contains:
def myfun():
print('original Mod')
Now we make a virtualenv and install it:
virtualenv venv -p python3
source venv/bin/activate
cd mymod
python setup.py install
now we go to python and import
from mymod.mymod.myfun import myfun
myfun()
# Returns original Mod
Now we modify myfun.py wITHOUT installing it:
def myfun():
print('Modified Mod')
and we go back to python:
from mymod.mymod.myfun import myfun
myfun()
# returns Modified Mod
So looks like directories take precedence over modules, but give it a try!
(Note that if we change to a directory where mymod.myfun is not directly in our path it goes back to printing original mod)
Upvotes: 1
Reputation:
It seems to depend on sys.path
Quoting https://docs.python.org/3/tutorial/modules.html#the-module-search-path:
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations: [edit: presumably in this order]
The directory containing the input script (or the current directory when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
The installation-dependent default.
which goes on to say
After initialization, Python programs can modify sys.path.
which suggests to me that anything could in theory happen :-/
I guess module_stuff
doesn't have the same name as a builtin so that rules out the first of these.
As you probably know, Python won't import the same module twice in a single run. Perhaps you can test it by making a small change to module/module_stuff
and running again.
Upvotes: 1