7

I have cloned a python library xyz into my computer. The file structure is as follows:

>> project (folder)
    * main.py
    >> xyz_git (folder)
          >> xyz (folder)

In the main.py

import sys
sys.path.insert(0, './xyz_git')
from xyz import Xyz

instance = Xyz()
print(instance.some_function())

The problem is I also have pip-installed module xyz to default python. Even if I remove the local folder, the from xyz import Xyz will still work due to the default pip installation. How can I ensure if the xyz import is not from local directory, it will give an error?

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
KubiK888
  • 4,377
  • 14
  • 61
  • 115

3 Answers3

4

If you know the path to the local directory, insert it into the array sys.path's first position before trying to import it.

e.g.

import sys
mypath = "ABS_PATH_TO_PROJECT_ROOT_DIR"
sys.path.insert(0, mypath)
from module import var

If you are using git, you might want to consider adding it as a git submodule also.

skullgoblet1089
  • 554
  • 4
  • 12
  • This doesn't work, exactly the same behaviour as my code above. – KubiK888 Apr 04 '19 at 14:58
  • If you can I strongly recommend that you use a virtualenv to organize the dependencies for your project. This way you won't have to plan around the system python's site packages. https://virtualenv.pypa.io/en/latest/ – skullgoblet1089 Apr 04 '19 at 18:57
0

This isn't clean, but the only way I can think of doing it, is to check if there is some attribute in your local xyz that you know is not in the installed xyz. See the following as an example:

main.py

import sys
sys.path.insert(0, './xyz_git')
import xyz
try:
    xyz.foo
except AttributeError as e:
    raise AttributeError('Local module xyz not installed!')
0

The best way to do this would be to install the package in development mode as the accepted answer to this question mentions.

so in the context of this question:

$ cd ~/xyz_git/
$ pip install -e .

will reload the package in pip so that pip is referencing the local package. To enforce that you could check the path in pip:

import xyz
correctLocation = "ABS_PATH_TO_PROJECT_ROOT_DIR/xyz_git/"
assert correctLocation == os.path.abspath(xyz.__file__))

Of course "correct location" will need to match absolute import path but if you have corrrectl installed in dev mode that should be no problem.

As mentioned above I would also strongly recommend using virtual environments to manage installed packages.

$ virtualenv dev_env
$ source dev_env/bin/activate

before installing in dev mode.

Xander
  • 104
  • 1
  • 4