omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    160032 import from same directory still broken

    Pythonista
    9
    14
    8067
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • userista
      userista last edited by userista

      Is it still possible to do an import from the same directory?

      E.g.

      foo
      ...bar.py
      ...init.py
      baz.py

      And in baz.py: from foo import bar

      This used to work, though I'm not sure if it was supposed to.

      Also, @omz should probably start a new thread for every beta. Instead of having multiple per beta.

      1 Reply Last reply Reply Quote 0
      • dgelessus
        dgelessus last edited by

        Yes, that is completely normal Python import behavior. If a folder contains an __init__.py, it is treated as a package and you can import submodules from it. (This of course only works for locations that can be imported from, like the main Script Library or site-packages.)

        1 Reply Last reply Reply Quote 0
        • ywangd
          ywangd last edited by

          It seems that @omz took the dot out from default sys.path. That is why you code does not import. Add following code befor the import and it will work again.

          import sys
          if '.' not in sys.path:
              sys.path.insert(0, '.')
          
          1 Reply Last reply Reply Quote 1
          • omz
            omz last edited by

            Yes, I accidentally removed "." from the default sys.path, will be back in the next build.

            1 Reply Last reply Reply Quote 0
            • Tizzy
              Tizzy last edited by

              I originally posted the below in 1.6 thread. The workaround from ywangd worked for me.

              Build 160025 bug report.

              I have a folder structure /AAAdriver/ inside of which I have DriverInfo.py and TripDataA.py.

              previously, from within DriverInfo.py,

              "import TripDataA"
              

              worked just fine. With the latest beta, I'm getting "ImportError: No module named TripDataA"

              1 Reply Last reply Reply Quote 0
              • Webmaster4o
                Webmaster4o last edited by

                This also seems to break the requests module, fix soon please!

                1 Reply Last reply Reply Quote 0
                • userista
                  userista last edited by

                  In 160032 '.' is still not in sys.path

                  1 Reply Last reply Reply Quote 0
                  • Webmaster4o
                    Webmaster4o last edited by

                    Yeah, that's a shame. Just noticed that myself.

                    1 Reply Last reply Reply Quote 0
                    • polymerchm
                      polymerchm last edited by polymerchm

                      Agreed. module in same folder is not findable. Any temp fix, short of moving it to site-packages?

                      1 Reply Last reply Reply Quote 0
                      • Webmaster4o
                        Webmaster4o last edited by

                        Yeah, I can use

                        import sys
                        if '.' not in sys.path: sys.path.append('.')
                        

                        At the beginning of a script to fix this. This has to be above your import statement

                        1 Reply Last reply Reply Quote 0
                        • JonB
                          JonB last edited by

                          just bumping this... seems to have missed the latest beta

                          1 Reply Last reply Reply Quote 0
                          • omz
                            omz last edited by

                            Hmm, works for me in the latest beta.

                            1 Reply Last reply Reply Quote 0
                            • Phuket2
                              Phuket2 last edited by

                              Mine is also working. I also doubled checked to make sure I didn't have a statement in the pythonista_startup.py that was correcting it

                              1 Reply Last reply Reply Quote 0
                              • JonB
                                JonB last edited by

                                oh i see...

                                >>> '.' in sys.path
                                False
                                

                                but the "played" path does get put on top, both for Play button and editor actions.
                                execfile does not do this, though i guess we were already responsible for setting path using execfile, at least indirectly through chdir.

                                Editor actions are a little surprising, in that they change sys.path globally. I have not tried this yet, but might that cause unintended consequences for running programs? I.e if there is some dynamic import, running a wrench item would cause that import to break.

                                I guess the point is, scripts should add their own import folder to sys.path, if they want to be guaranteed to be able to import from that folder later.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post
                                Powered by NodeBB Forums | Contributors