On 05/05/2015 10:59 AM, Oscar Benjamin wrote:
<SNIP>
My projects are structured like:
Project > develop > Project > Project > __main__.py
                               tests   > __main__.py
I want to be able to execute my Project from a cwd of:
Project/develop/Project
as: Python3 -m Project
That currently works.

That will only work if there is also a file __init__.py under the
Project/develop/Project/Project directory (alongside the __main__.py
file). Adding a __main__.py file allows the directory to be treated as
a Python script e.g.:
But actually there is no __init__.py in Project/develop/Project/Project/
There is only __main__.py and Project.py files. The __main__.py file imports Project.py.
$ python3 Project/develop/Project/Project
$ python3 Project  # Assuming cwd is Project/develop/Project
I tested the above on the terminal and it does not work. I get:
wolfrage@wolfrage-Lemur-UltraThin ~/HomePlusWolfrage/Projects/Project/develop/Project $ python3 Project
Traceback (most recent call last):
  File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "Project/__main__.py", line 4, in <module>
    from . import Project
SystemError: Parent module '' not loaded, cannot perform relative import

The -m switch searches for a script as if it were a module using the
module search path. In this case the directory containing the package
Project must be on sys.path and one way to achieve this is to change
directory to the Project/develop/Project directory. Then the Project
folder in this directory is on sys.path.
Did you perhaps mean the Project/develop/Project/Project/ directory?

However a directory is not considered a package unless it contains a
file __init__.py. So if the directory is on sys.path (e.g. in the cwd)
AND their is an __init__.py file then you can do

$ python3 -m Project

and the code in __main__.py will run.

Assuming you have the same setup in the
Project/develop/Project/Project/tests directory (both __init__.py and
__main__.py and Project is on sys.path) then you can run
tests/__main__.py using the module search path as
I think I may have confused you. The layout is like this:
Project/develop/Project/Project
Project/develop/Project/tests
Thus tests is not contained in module's directory.
My reasoning for this is that the tests would be shipped with my production code. I am currently undecided as to whether this is a good or bad thing. But the more that I read about testing the more it seems to be a good thing. So perhaps the best solution is to move the tests into the module's directory. Then the below information would probably work.

$ python3 -m Project.tests

Note that this is the same name that you would use to import from the
tests package in interpreter e.g.

from Project.tests import test_stuff

imports from the __init__.py in the tests folder.

<SNIP>
Oscar


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to