The structure of my project:

project ┐ β”œβ”€ src ┐ β”‚ β”œβ”€ __init__.py β”‚ β”œβ”€ __main__.py β”‚ └─ ... β”œβ”€ requirements.txt └─ setup.py 

When testing through the setup.py interpreter, yes, and pylint think that I am in the project folder and expect that in the src files I will add to the imports from src .

When launching __init__.py directly, the opposite is true: from src causes an exception, since I am already in the src folder.

In os looking for how to change the current directory, I did not find it. I would be grateful if you throw an article on structuring projects and the relationship setup.py , __init__.py , __main__.py , etc.

  • No src needed, the directory with the application module should have the name of the module, that is, in your case, this is probably project - andreymal
  • one
    And __init__.py doesn’t need to be started at all, and since you start it for some reason, it means you are doing something wrong - andreymal
  • @andreymal I do not think that the porridge of a dozen files in the root directory of the project is normal. And __init__.py runs main() in handler.py or asks for extra information in exceptional situations. - lisovskey
  • No need to root directory, you just need to rename src to project - you get project/project/__init__.py . The main() call should be in the __main__.py file, and this file should not be launched directly, but with the python -m project command python -m project or something like that. Then imports of the import project (now you have import src , you need to rename the directory) will work fine always and everywhere - andreymal
  • All this draws on the answer, but there are people in ruSO who understand this better than me, so I’ll wait. :) - andreymal

1 answer 1

The presence of src/__init__.py is an error if src not the name of a Python package. If you want to use your code as an import package (usually project==package ), then src should be renamed to package ( project/package/__init__.py ). This is described, for example, in the introductory manual (there are Russian translations) .

package/__main__.py you should not run directly. The command to start is: python -m package from the project root folder or from any folder if the package is already installed ( pip install -e . ). See __main__ description in official documentation .

When you run the Python script, its directory is automatically added to pythonpath ( sys.path ), so the python setup.py test command that runs py.test or similar tools will find your package .

For more information on package management, see https://packaging.python.org/

In order not to create a project skeleton every time from scratch, you can generate folders / files from a template using a cookiecutter you can use your own template or choose one of the most appropriate templates for your task:

 $ pip install -U cookiecutter $ cookiecutter gh:audreyr/cookiecutter-pypackage 
  • And what if you need to debug a separate file? - lisovskey
  • @lisovskey: what does it mean in Russian? (they wrote the test, started the tests, corrected the code so that the tests would pass, repeat - what step does the question cause?) - jfs
  • all is very clear, thanks - lisovskey