Importing correctly with pytest

前端 未结 4 1871
野趣味
野趣味 2020-12-13 23:25

I just got set up to use pytest with Python 2.6. It has worked well so far with the exception of handling \"import\" statements: I can\'t seem to get pytest to respond to im

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 23:55

    import looks in the following directories to find a module:

    1. The home directory of the program. This is the directory of your root script. When you are running pytest your home directory is where it is installed (/usr/local/bin probably). No matter that you are running it from your src directory because the location of your pytest determines your home directory. That is the reason why it doesn't find the modules.
    2. PYTHONPATH. This is an environment variable. You can set it from the command line of your operating system. In Linux/Unix systems you can do this by executing: 'export PYTHONPATH=/your/custom/path' If you wanted Python to find your modules from the test directory you should include the src path in this variable.
    3. The standard libraries directory. This is the directory where all your libraries are installed.
    4. There is a less common option using a pth file.

    sys.path is the result of combining the home directory, PYTHONPATH and the standard libraries directory. What you are doing, modifying sys.path is correct. It is something I do regularly. You could try using PYTHONPATH if you don't like messing with sys.path

提交回复
热议问题