Find module

Find module#

import sys
sys.path.append("/tmp")

Experiment#

This experiment examines whether the Python interpreter will load a module first from the current folder or the environment.

Module files#

Let’s create the module files. It will be a package that will just print something when you import it.

%%writefile environment_vs_files_files/pyproject.toml
[project]
version = "0.0.0"
name = "echo_project"
Overwriting environment_vs_files_files/pyproject.toml
%%writefile environment_vs_files_files/src/echo_project/__init__.py
print("Hello world")
Overwriting environment_vs_files_files/src/echo_project/__init__.py

Excuting file#

Here is a file that imports the module created in the previous section. Note that it adds the module’s folder to it’s path, so even if it’s not installed in the folder it will access the module from.

%%writefile environment_vs_files_files/loader.py
import sys
import os
from pathlib import Path
sys.path.append(
    str(Path(os.getcwd())/"src")
)
import echo_project
Overwriting environment_vs_files_files/loader.py

Test#

Creating the environment so we’re sure we’re working in a clear environment.

!python3 -m venv venv

Now let’s try to execute the file we prepared.

%%bash
source venv/bin/activate
cd environment_vs_files_files
python3 loader.py
Hello world

And so we got the expected behaviour.

Install package - so it’s now added to the environment.

%%bash
source venv/bin/activate
pip3 install ./environment_vs_files_files
Processing ./environment_vs_files_files
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Building wheels for collected packages: echo_project
  Building wheel for echo_project (pyproject.toml): started
  Building wheel for echo_project (pyproject.toml): finished with status 'done'
  Created wheel for echo_project: filename=echo_project-0.0.0-py3-none-any.whl size=1183 sha256=4fbfdaf9bd624cd574606e2fcbe6091cf50bbc1eeef41c4efb6b9d45e12c8194
  Stored in directory: /tmp/pip-ephem-wheel-cache-ptthpksn/wheels/33/24/f8/050002a9108b77a63da6dcfc566179f4a21a20d56b8b708e45
Successfully built echo_project
Installing collected packages: echo_project
Successfully installed echo_project-0.0.0

Now let’s change the behaviour of the package.

%%writefile environment_vs_files_files/src/echo_project/__init__.py
print("Hello world")
print("I was edited")
Overwriting environment_vs_files_files/src/echo_project/__init__.py

But since the changes are not installed in the virtual environment, the behaviour remains the same as before.

%%bash
source venv/bin/activate
cd environment_vs_files_files
python3 loader.py
Hello world

But if we run the same code without enabling the virtual environment, we’ll get different behaviour.

%%bash
cd environment_vs_files_files
python3 loader.py
Hello world
I was edited

Don’t forget to remove the environment we just used, to avoid cluttering up the repository with temporary files.

!rm -r venv