VCS and Python structure: how to configure PYTHONPATH automatically?

There are many suggestions on the Internet about what the structure of a Python project can / should be, for example. What is the best project structure for a Python application? .

"proj-dir" +- doc +- apidoc +- scripts +- "src-dir" +- tests 

It seems that many people in the Python world prefer "src-dir" and "proj-dir" to be equal (or very similar). However, scripts and tests will certainly need to import some modules from src-dir, so either I have to

  • run all scripts and tests when proj-dir as the current current directory, or
  • PYTHONPATH should contain proj-dir, right?

Or is there any other opportunity that I am missing now?

Suppose I have such a structure and correctly configured PYTHONPATH. Now I submit the project to VCS, and another developer checks the project. On his machine, PYTHONPATH will not be installed correctly, and import in scripts and tests will not work.

  • Is there any way to make part of the PYTHONPATH definition part of the project so that it can be part of version control?
  • Is there any other project structure that would allow me to check and start using the project without changing PYTHONPATH?
  • The proposed solution is proposed: Python - how is PYTHONPATH with a complex directory structure? (i.e. include a short sys.path snippet for modification in each script and test) correct?
  • Can any help use the site module http://docs.python.org/2/library/site.html ?
  • What is your workflow when developing a project with a similar structure?
+4
source share
1 answer

Even within the framework of the project structure is complex, and test tables use src in different packages, you can add PYTHONPATH to Environmental Variables on Windows

My computer โ†’ Properties โ†’ System properties โ†’ Advanced tab โ†’ Environmental variables. Add a new system variable PYTHONPATH and add the dir to this variable with the delimiter ';'

I suggest you keep readme.txt in your project, which clearly talks about how to add PYTHONPATH to run scripts. This is similar to adding PATH android-sdk to make the adb shell work by adding a Java path to make Java work on the command line.

Thus, readme.txt helps to run the script when loading on another machine with only one change (adding the variable PYTHONPATH with value = path to the project)

No need to add all src modules to make test files work using src

Here is an example proj structure:

 SAMPLEPROJECT src com sample example utils a.py b.py testcases test1.py test2.py res docs 

If the test1.py test case uses a.py and and b.py: Do not add the utils module to PYTHONPATH, since you have already added the path to the python path, the import statement looks something like this: test1.py

 from src.com.sample.example.utils import a.py from src.com.sample.example.utils import b.py 
+1
source

Source: https://habr.com/ru/post/1442861/


All Articles