1

For a Python3 project of my own, I'm trying to follow the file system structure described by Jean-Paul Calderone (credits for the original document) :

MyProject/
|-- bin/
|   |-- myproject
|
|-- myproject/
|   |-- test/
|   |   |-- __init__.py
|   |   |-- test_main.py
|   |   
|   |-- __init__.py
|   |-- main.py
|
|-- setup.py
|-- README

My project isn't mature but the tests are ok : a call to nosetests run the different parts of the code as expected.

So now, I'm trying to produce an executable, an "external entry point" to my code. I'm struggling with the content of the executable (bin/myproject); I read in Jean-Paul Calderone blog :

Don't give them a .py extension, even if they are Python source files. Don't put any code in them except an import of and call to a main function defined somewhere else in your projects.

So, I tried to fill the executable with the following lines...

from ..myproject.main import main
main()

... but being in the Project directory and calling my program with bin/myproject raises an import error :

SystemError: Parent module '' not loaded, cannot perform relative import

I can't go further : any idea to help me ?

Community
  • 1
  • 1
suizokukan
  • 1,303
  • 4
  • 18
  • 33

1 Answers1

2

Well, I got it :

import sys, os
sys.path.append( os.path.join("..", "myproject") )

from myproject.main import main
main()

: adding the right path to sys.path does the trick.

By the way, creating such an executable script isn't required by Pypi which can create an entry point of its own; see the following lines in setup.py :

entry_points={
    # let's create an executable script named 'myproject' :
    'console_scripts': ['myproject=myproject.main:main',],
},

... But such an executable script is a very convenient way to try the project without installing it using pypi; such a script makes easy to launch the project "on the fly".

suizokukan
  • 1,303
  • 4
  • 18
  • 33