Page 85 -
P. 85

sharing your code


          Welcome to the PyPI community


           Congratulations! You are now a full-fledged, card-carrying member of the
           PyPI community. Your distribution has joined the over 10,000 other uploads
           on PyPI. Feel free to surf on over to the PyPI website to confirm the upload.
           Programmers from all over the globe are now able to download, unpack, and
           install your module into their local copy of Python, which is pretty cool when
           you think about it.
                                                                                   You’ve now written and

                                                                                   how cool is that?
           Sit back, put your feet up, and wait for the plaudits to                published your code…
           begin…




           Q:   Which is best: plain imports or   Q:  Is it really necessary for me to   a result of your Java technologies compiling
                                                                            your code.) The Python interpreter is smart
           specific imports?                install my modules into my local copy of   enough to skip the translation phase the next
                                                                            time your module is used, because it can
                                            Python? Can’t I just put them in any old   determine when you’ve made changes to
          A: Neither, really. Most programmers   folder and import them from there?  the original module code file. If your module
           mix and match based on their own personal                        code hasn’t changed, no translation occurs
           preference and taste (although there are   A: Yes, it is possible. Just bear in mind   and the “compiled” code is executed. If your
           plenty of programmers willing to argue that   that Python looks for modules in a very   code has changed, the translation occurs
           their preferred way is the “one true way”).    specific list of places (recall the import   (creating a new pyc file) as needed. The
                                           sys; sys.path trick from earlier   upshot of all this is that when Python sees a
           Note that the from module import  in this chapter). If you put your modules   pyc file, it tries to use it because doing so
          function form pollutes your current   in a folder not listed in Python’s path list,   makes everything go much faster.
           namespace: names already defined in your   chances are the interpreter won’t find
           current namespace are overwritten by the   them, resulting in ImportErrors. Using the   Q:  Cool. So I can just provide my
           imported names.                 distribution utilities to build and install your   users with the pyc file?
           Q:   And when I press F5 in IDLE’s edit   module into your local copy of Python avoids
                                            these types of errors.
           imported with an import statement, right? Q:  I noticed the distribution utiliites   A: No, don’t do that, because the use of
           window, it’s as if the module’s code is
                                                                            the pyc file (if found) is primarily a runtime
                                           created a file called nester.pyc.   optimization performed by the interpreter.
          A: Yes, that is essentially what happens.   What’s up with that?  Q:
                                                                                 So, can I delete the pyc file if I don’t
           The code in your edit window is compiled                         need it?
           and executed by Python, and any names  A: That’s a very good question. When
           in the edit window are imported into the   the interpreter executes your module code
           namespace being used by IDLE’s shell. This   for the first time, it reads in the code and   A: Sure, if you really want to. Just be
           is handy, because it makes it easy to test   translates it into an internal bytecode format   aware that you lose any potential runtime
           functionality with IDLE. But bear in mind that   which is ultimately executed. (This idea is   optimization.
           outside of IDLE, you still need to import your   very similar to the way the Java JVM works:
           module before you can use its functionality.  your Java code is turned into a class file as




                                                                                       you are here 4    49
   80   81   82   83   84   85   86   87   88   89   90