1

I want to run tests/modules in my Python project without installing them. I have this on top of every file in my project:

import sys
from os.path import expanduser
sys.path.append(expanduser("~") + "/myproject")

I tried putting this in 'setup.py' or 'init.py' of the folder containing the file, but that doesn't work. So I want to nice way of adding project root to path so it's there before running anything in the project. I should be able to import any module/file in the project like this:

from dir1.file1. import MyClass

As mentioned in the Title, I don't want to 'pip install' my project and the solution should work if I want to install it later.

codebee
  • 824
  • 1
  • 9
  • 22
  • This is a good example of the fundamental futility of manipulating `sys.path` in a module—it’s too late if you’re loading it. Even in a script it’s poor practice: you already had to configure starting the script, so you might as well configure the path outside as well. – Davis Herring Apr 12 '19 at 02:12
  • Possible duplicate of (part of) [Best practises for imports in Python 3 packages and scripts](https://stackoverflow.com/questions/50185897/best-practises-for-imports-in-python-3-packages-and-scripts) – Davis Herring Apr 12 '19 at 02:13

0 Answers0