Search code examples
pythonpackaging

python tox - No such file or directory error


I have a project, let's say called 'my_project' with the following structure:

my_project
|
 tox.ini
 setup.py
 src
 |
  client.py
  server.py
  config.json
  # other files
  tests.py

The classes defined in files client.py and server.py use information contained in config.json and tests.py implements the py.test tests for each file in folder src. Inside the client/server files I read the config file as follows:

with open('./config.json') as infile:
     self.parameters = json.load(infile)

Next, I created a tox.ini file as follows:

[tox]
envlist = py27,cov

[testenv]
#commands = py.test --cov=src/client.py src/tests.py -v
commands = py.test -sv --doctest-modules src/__init__.py src/tests.py


[testenv:py27]
commands = coverage erase
           coverage run --source=src -m pytest
           coverage report -m
deps = pytest

But when I run the tox command I get an error: "No such file or directory: 'config.json'". How can I setup up the correct path to make sure that tox can find all necessary files?


Solution

  • Trying to open './config.json' will always make Python look in the process's current directory; that's just how filepaths work. If you want to open the config.json file in the same directory as the .py file that's doing the opening, you need to include the path of the directory containing the .py file:

    with open(os.path.join(os.path.dirname(__file__), 'config.json')) as infile: