Cannot get imports to work in web.py project

后端 未结 2 1754
囚心锁ツ
囚心锁ツ 2021-01-15 12:07

I\'m trying to create a basic blogging application in Python using Web.Py. I have started without a direcotry structure, but soon I needed one. So I created this structure:<

相关标签:
2条回答
  • 2021-01-15 12:40

    App.py contains the statement

    from Blog.Views import Home
    

    So Blog needs to be among the list of directories Python searches for modules (sys.path). That can be arranged in various ways.

    1. Since you are starting the app with python start.py, the directory containing start.py is automatically added to the search path. So you could change

      from Blog.Views import Home
      

      to

      from Views import Home
      
    2. Another option would be to move start.py up one level, out of the Blog directory. Then when you call python start.py, the directory containing start.py will also be the directory containing Blog. So Python would find Blog when executing from Blog.Views ...

    3. Finally, you could add the Blog directory to your PYTHONPATH environment variable.

    0 讨论(0)
  • 2021-01-15 12:57

    You can only import the module Blog if its parent directory (not Blog itself) is on python's path.

    If you run your program from the Blog directory like you do, you can only imort Views directly, like you do with Application.App:

    from Views import Home
    

    instead of

    from Blog.Views import Home
    

    in your Application/App.py.

    0 讨论(0)
提交回复
热议问题