4

my python folder structure is as follows

repository
  -libraries
     -image
        -imagefuncs.py
     -videos
        -videofuncs.py
     -text
        -textfuncs.py
  -docs
  -tests
     -test_imagefuncs.py

For the life of me i cannot figure out how I am supposed to test my functions defined in imagefuncs.py in test_imagefuncs.py

I cannot import the libraries folder in test_imagefuncs.py so none of those functions are visible to my testing code.

I'm using python3 and all i want to achieve is do py.test from my root repository folder and have all my tests execute without throwing import errors.

Is modifying the python path the only way to go about achieving this?

I want to achieve this without modifying the system path or python path

Thalish Sajeed
  • 1,351
  • 11
  • 25
  • Write a `setup.py` and *install* the modules for testing. When setting up a new project I generally follow https://jeffknupp.com/blog/2013/08/16/open-sourcing-a-python-project-the-right-way/, or you could look into the cookiecutter project. – jonrsharpe Aug 19 '18 at 07:56
  • 2
    Possible duplicate of [PATH issue with pytest 'ImportError: No module named YadaYadaYada'](https://stackoverflow.com/questions/10253826/path-issue-with-pytest-importerror-no-module-named-yadayadayada) – hoefling Aug 19 '18 at 08:59
  • 1
    Add an empty `conftest.py` into the `repository` dir, this should already suffice to adjust `sys.path`. Check out the answer in the linked question for more details. – hoefling Aug 19 '18 at 09:04

1 Answers1

1

Is modifying the python path the only way to go about achieving this?

Yes. One way or other, only paths in sys.path will be used.

modify PYTHONPATH is one way to just append paths to sys.path, any other way will do the same thing as adding paths to sys.path. The specific variation really depends on project.

e.g.

export PYTHONPATH='/opt/mybuild'

[tmp]$ python3.6 -m site
sys.path = [
'/tmp',
'/opt/mybuild', 
'/usr/lib64/python36.zip',
'/usr/lib64/python3.6',
'/usr/lib64/python3.6/lib-dynload',
'/home/joe/.local/lib/python3.6/site-packages',
'/usr/lib64/python3.6/site-packages',
'/usr/lib/python3.6/site-packages',
]
USER_BASE: '/home/joe/.local' (exists)
USER_SITE: '/home/joe/.local/lib/python3.6/site-packages' (exists)
ENABLE_USER_SITE: True
Gang
  • 2,658
  • 3
  • 17
  • 38