[TriPython] Problems with import after attempting to create pip installable package

Steve Gambino stevegambino at gmail.com
Thu Nov 2 01:07:49 EDT 2017


Hi David,
Thank you for your questions.  My answers are inline below.  I've made
the code available here so that you can look at the setup.py
See https://github.com/sgambino/project-random

I was a little confused reading your description, mainly because I wasn't
   sure when you said "package" whether you meant a distributable unit of
   Python code with a setup.py file, or whether you meant "an importable
   directory on sys.path containing an __init__.py file".


> I see what you mean how my use of terms was confusing. I should have said
a
> distributable unit of Python code with a setup.py file



   Is there an importable package directory named "get_random" containing an
   __init__.py file? Or is there instead a get_random.py module?

> Yes, there was an importable package directory named "get_random".  Note
that
> since the original post, I renamed the distributable unit to
"sample_random" and
> the importable package directory to "try_random"

   If you have an importable package directory named get_random, then the
   behavior you saw is expected. When you import a package, the only names
   you see in that package are the ones defined (or imported into) the
   __init__.py file itself. To see anything in any other module inside that
   package you have to import that explicity, in your main program file or
in
   the __init__.py file.



   Which form does your get_random program take? There are at least two ways
   to distribute Python scripts, one that uses setuptools and console entry
   points, and one doesn't.

> The form of the program uses setuptools and console entry points. This is
> the topic where I think my understanding is the weakest and need help with

   Yes, if you shared your full setup.py file with us, that would probably
   answer those questions and more, and would be helpful in diagnosing your
   problem. If you would share the full layout of your distributable package
   files, that would help too.

> Yes, of course. See https://github.com/sgambino/project-random
> And note that as mentioned earlier, I renamed the distributable unit from
>  to "sample_random" and the python package to "try_random"
>
> I reported earlier that after doing a pip install, I could run the
program from
> command line but if I tried to import the package into a script or into
the
> python REPL itself that I could not see the complete list of the package
> so as to be able call its functions.   It appears my issue was with the
> way I was importing it into the namespace.  I was declaring "import
sample_random"
> However, in the IRC channel, James (jwhisnant) was helping me, and he
declared the
> import in this form: "from sample_random import try_random"    This
appears
> to have resolved the issue.  I would like to better understand why so that
> I do not repeat this behavior in the future.  I suspect it may be with the
> way I declared the "entry_points" in the setup.py   I think need a better
understanding
> of entry points in general and also a clearer understanding of the
semantic elements of the
> form of how they are specified in the setup.py   I used the example
setup.py as referenced
> in the tutorials at https://packaging.python.org/   There seems to be
ample documentation
> and commentary in the setup.py concerning entry points but it is still
taking awhile for
> it to become clear enough in my understanding so that I can confidently
say, I truly
> understand. :-)
>
> Thanks,
> Steve Gambino

On Wed, Nov 1, 2017 at 10:17 PM David Handy <david at handysoftware.com> wrote:

>    Hi Steve -
>
>
>
>    I was a little confused reading your description, mainly because I
> wasn't
>    sure when you said "package" whether you meant a distributable unit of
>    Python code with a setup.py file, or whether you meant "an importable
>    directory on sys.path containing an __init__.py file".
>
>
>
>    Is there an importable package directory named "get_random" containing
> an
>    __init__.py file? Or is there instead a get_random.py module?
>
>
>
>    If you have an importable package directory named get_random, then the
>    behavior you saw is expected. When you import a package, the only names
>    you see in that package are the ones defined (or imported into) the
>    __init__.py file itself. To see anything in any other module inside that
>    package you have to import that explicity, in your main program file or
> in
>    the __init__.py file.
>
>
>
>    Which form does your get_random program take? There are at least two
> ways
>    to distribute Python scripts, one that uses setuptools and console entry
>    points, and one doesn't.
>
>
>
>    Yes, if you shared your full setup.py file with us, that would probably
>    answer those questions and more, and would be helpful in diagnosing your
>    problem. If you would share the full layout of your distributable
> package
>    files, that would help too.
>
>
>
>    Thanks,
>
>    David H
>
>
>
>
>
>    On Wednesday, November 1, 2017 3:29pm, "Steve Gambino"
>    <stevegambino at gmail.com> said:
>
>    > _______________________________________________
>    > TriZPUG mailing list
>    > TriZPUG at python.org
>    > https://mail.python.org/mailman/listinfo/trizpug
>    > http://tripython.org is the Triangle Python Users Group
>    > Hello friends,
>    >
>    > I'm attempting to learn how to create a pip installable package. So
> far
>    the
>    > attempt has been relatively successful but for one nagging issue. I'm
>    > hoping if I describe the issue here clearly enough that someone can
>    offer
>    > pointers to help me get to a resolution.
>    >
>    > I've created the package (named get_random) successfully with
> setuptools
>    > and have been able to do a pip install of it in a virtualenv.
>    >
>    > The package "get_random" contains a trivial program also named
>    > "get_random". The program, "get_random", imports one module, 'random',
>    and
>    > it has the functions, main(), get_random() and print_random(). After
> the
>    > pip install of the get_random package, I can call the get_random
> program
>    > from the command line in the virtualenv and get the expected results.
> In
>    > otherwords, if I run the following in a terminal I get a resulting
>    random
>    > number, like so:
>    >
>    > sgambino-m1:~/vnv_for_pip > get_random
>    > 0.0259274305036
>    >
>    > However, if I try to import the get_random package into a python REPL
> I
>    do
>    > not see
>    > a complete list of what is in the package. I only see the following:
>    >
>    > >>> dir(get_random)
>    > >>> ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
>    > '__path__']
>    >
>    > but I was expecting to see:
>    >
>    > >>> dir(get_random)
>    > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
>    > 'get_random', 'main', 'print_random', 'random']
>    >
>    > If I try to import the get_random package into a script and run the
>    script,
>    > I get the following error:
>    >
>    > ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
>    > Traceback (most recent call last):
>    > File "./try_get_random.py", line 5, in <module>
>    > get_random.print_random()
>    > AttributeError: 'module' object has no attribute 'print_random'
>    >
>    > Here is some more detail from the REPL that may give a clue. I'm
>    wondering
>    > if there is something in the setup.py that I haven't
>    > defined correctly or not defined at all. I could share that if someone
>    > thinks it is worth a look. The setup.py that I started with
>    > was the one that is provided from
> https://github.com/pypa/sampleproject
>    >
>    > Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>    > [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
>    > Type "help", "copyright", "credits" or "license" for more information.
>    > >>> import get_random
>    > >>> dir(get_random)
>    > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
>    > '__path__']
>    > >>> print(get_random.__file__)
>    >
>
>  /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
>    > >>> print(get_random.__package__)
>    > None
>    > >>> print(get_random.__path__)
>    > ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
>    >
>    > If you have bothered to read this far, I hope this has made some sense
>    and
>    > I thank you for any ideas you may be willing to share!
>    > Steve Gambino
>    > --
>    > Best,
>    > Steve
>    > Hello friends,
>    > I'm attempting to learn how to create a pip installable package. So
> far
>    > the attempt has been relatively successful but for one nagging issue.
>    I'm
>    > hoping if I describe the issue here clearly enough that someone can
>    offer
>    > pointers to help me get to a resolution.
>    > I've created the package (named get_random) successfully with
> setuptools
>    > and have been able to do a pip install of it in a virtualenv.
>    > The package "get_random" contains a trivial program also named
>    > "get_random". The program, "get_random", imports one module, 'random',
>    and
>    > it has the functions, main(), get_random() and print_random(). After
> the
>    > pip install of the get_random package, I can call the get_random
> program
>    > from the command line in the virtualenv and get the expected results.
> In
>    > otherwords, if I run the following in a terminal I get a resulting
>    random
>    > number, like so:
>    > sgambino-m1:~/vnv_for_pip > get_random
>    > 0.0259274305036
>    > However, if I try to import the get_random package into a python REPL
> I
>    do
>    > not see
>    > a complete list of what is in the package. I only see the following:
>    > >>> dir(get_random)
>    > >>> ['__builtins__', '__doc__', '__file__', '__name__',
>    > '__package__',
>    > '__path__']
>    >
>    > but I was expecting to see:
>    >
>    > >>> dir(get_random)
>    > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
>    > 'get_random', 'main', 'print_random', 'random']
>    >
>    > If I try to import the get_random package into a script and run the
>    > script, I get the following error:
>    >
>    > ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
>    > Traceback (most recent call last):
>    > ** File "./try_get_random.py", line 5, in <module>
>    > ****** get_random.print_random()
>    > AttributeError: 'module' object has no attribute 'print_random'
>    >
>    > Here is some more detail from the REPL that may give a clue.** I'm
>    > wondering if there is something in the setup.py that I haven't
>    > defined correctly or not defined at all. I could share that if someone
>    > thinks it is worth a look. The setup.py that I started with
>    > was the one that is provided from
>    [1]https://github.com/pypa/sampleproject
>    > Python 2.7.10 (default, Oct 23 2015, 19:19:21)
>    > [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
>    > Type "help", "copyright", "credits" or "license" for more information.
>    > >>> import get_random
>    > >>> dir(get_random)
>    > ['__builtins__', '__doc__', '__file__', '__name__', '__package__',
>    > '__path__']
>    > >>> print(get_random.__file__)
>    >
>
>  /Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
>    > >>> print(get_random.__package__)
>    > None
>    > >>> print(get_random.__path__)
>    > ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
>    > If you have bothered to read this far, I hope this has made some sense
>    and
>    > I thank you for any ideas you may be willing to share!
>    > Steve Gambino
>    > --
>    > Best,**
>    > Steve
>    >
>    > References
>    >
>    > Visible links
>    > 1. https://github.com/pypa/sampleproject
>    >
> _______________________________________________
> TriZPUG mailing list
> TriZPUG at python.org
> https://mail.python.org/mailman/listinfo/trizpug
> http://tripython.org is the Triangle Python Users Group
>
-- 
Best,
Steve
-------------- next part --------------
   Hi David,
   Thank you for your questions.** My answers are inline below.** I've made
   the code available here so that you can look at the setup.py
   See [1]https://github.com/sgambino/project-random
   I was a little confused reading your description, mainly because I wasn't
   ** **sure when you said "package" whether you meant a distributable unit
   of
   ** **Python code with a setup.py file, or whether you meant "an importable
   ** **directory on sys.path containing an __init__.py file".
   > I see what you mean how my use of terms was confusing. I should have
   said a
   > distributable unit of Python code with a setup.py file

   ** **Is there an importable package directory named "get_random"
   containing an
   ** **__init__.py file? Or is there instead a get_random.py module?

   > Yes, there was an importable package directory named "get_random".**
   Note that
   > since the original post, I renamed the distributable unit to
   "sample_random" and
   > the importable package directory to "try_random"
   ** **If you have an importable package directory named get_random, then
   the
   ** **behavior you saw is expected. When you import a package, the only
   names
   ** **you see in that package are the ones defined (or imported into) the
   ** **__init__.py file itself. To see anything in any other module inside
   that
   ** **package you have to import that explicity, in your main program file
   or in
   ** **the __init__.py file.

   ** **Which form does your get_random program take? There are at least two
   ways
   ** **to distribute Python scripts, one that uses setuptools and console
   entry
   ** **points, and one doesn't.

   > The form of the program uses setuptools and console entry points. This
   is
   > the topic where I think my understanding is the weakest and need help
   with
   ** **Yes, if you shared your full setup.py file with us, that would
   probably
   ** **answer those questions and more, and would be helpful in diagnosing
   your
   ** **problem. If you would share the full layout of your distributable
   package
   ** **files, that would help too.
   > Yes, of course. See [2]https://github.com/sgambino/project-random
   > And note that as mentioned earlier, I renamed the distributable unit
   from
   >** to "sample_random" and the python package to "try_random"
   >
   > I reported earlier that after doing a pip install, I could run the
   program from
   > command line but if I tried to import the package into a script or into
   the
   > python REPL itself that I could not see the complete list of the package
   > so as to be able call its functions.**** It appears my issue was with
   the
   > way I was importing it into the namespace.** I was declaring "import
   sample_random"
   > However, in the IRC channel, James (jwhisnant) was helping me, and he
   declared the
   > import in this form: "from sample_random import try_random"****** This
   appears
   > to have resolved the issue.** I would like to better understand why so
   that
   > I do not repeat this behavior in the future.** I suspect it may be with
   the
   > way I declared the "entry_points" in the setup.py**** I think need a
   better understanding
   > of entry points in general and also a clearer understanding of the
   semantic elements of the
   > form of how they are specified in the setup.py**** I used the example
   setup.py as referenced
   > in the tutorials at [3]https://packaging.python.org/**** There seems to
   be ample documentation
   > and commentary in the setup.py concerning entry points but it is still
   taking awhile for
   > it to become clear enough in my understanding so that I can confidently
   say, I truly
   > understand. :-)
   >
   > Thanks,
   > Steve Gambino
   On Wed, Nov 1, 2017 at 10:17 PM David Handy <[4]david at handysoftware.com>
   wrote:

     ** **Hi Steve -

     ** **I was a little confused reading your description, mainly because I
     wasn't
     ** **sure when you said "package" whether you meant a distributable unit
     of
     ** **Python code with a setup.py file, or whether you meant "an
     importable
     ** **directory on sys.path containing an __init__.py file".

     ** **Is there an importable package directory named "get_random"
     containing an
     ** **__init__.py file? Or is there instead a get_random.py module?

     ** **If you have an importable package directory named get_random, then
     the
     ** **behavior you saw is expected. When you import a package, the only
     names
     ** **you see in that package are the ones defined (or imported into) the
     ** **__init__.py file itself. To see anything in any other module inside
     that
     ** **package you have to import that explicity, in your main program
     file or in
     ** **the __init__.py file.

     ** **Which form does your get_random program take? There are at least
     two ways
     ** **to distribute Python scripts, one that uses setuptools and console
     entry
     ** **points, and one doesn't.

     ** **Yes, if you shared your full setup.py file with us, that would
     probably
     ** **answer those questions and more, and would be helpful in diagnosing
     your
     ** **problem. If you would share the full layout of your distributable
     package
     ** **files, that would help too.

     ** **Thanks,

     ** **David H

     ** **On Wednesday, November 1, 2017 3:29pm, "Steve Gambino"
     ** **<[5]stevegambino at gmail.com> said:

     ** **> _______________________________________________
     ** **> TriZPUG mailing list
     ** **> [6]TriZPUG at python.org
     ** **> [7]https://mail.python.org/mailman/listinfo/trizpug
     ** **> [8]http://tripython.org is the Triangle Python Users Group
     ** **> Hello friends,
     ** **>
     ** **> I'm attempting to learn how to create a pip installable package.
     So far
     ** **the
     ** **> attempt has been relatively successful but for one nagging issue.
     I'm
     ** **> hoping if I describe the issue here clearly enough that someone
     can
     ** **offer
     ** **> pointers to help me get to a resolution.
     ** **>
     ** **> I've created the package (named get_random) successfully with
     setuptools
     ** **> and have been able to do a pip install of it in a virtualenv.
     ** **>
     ** **> The package "get_random" contains a trivial program also named
     ** **> "get_random". The program, "get_random", imports one module,
     'random',
     ** **and
     ** **> it has the functions, main(), get_random() and print_random().
     After the
     ** **> pip install of the get_random package, I can call the get_random
     program
     ** **> from the command line in the virtualenv and get the expected
     results. In
     ** **> otherwords, if I run the following in a terminal I get a
     resulting
     ** **random
     ** **> number, like so:
     ** **>
     ** **> sgambino-m1:~/vnv_for_pip > get_random
     ** **> 0.0259274305036
     ** **>
     ** **> However, if I try to import the get_random package into a python
     REPL I
     ** **do
     ** **> not see
     ** **> a complete list of what is in the package. I only see the
     following:
     ** **>
     ** **> >>> dir(get_random)
     ** **> >>> ['__builtins__', '__doc__', '__file__', '__name__',
     '__package__',
     ** **> '__path__']
     ** **>
     ** **> but I was expecting to see:
     ** **>
     ** **> >>> dir(get_random)
     ** **> ['__builtins__', '__doc__', '__file__', '__name__',
     '__package__',
     ** **> 'get_random', 'main', 'print_random', 'random']
     ** **>
     ** **> If I try to import the get_random package into a script and run
     the
     ** **script,
     ** **> I get the following error:
     ** **>
     ** **> ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
     ** **> Traceback (most recent call last):
     ** **> File "./try_get_random.py", line 5, in <module>
     ** **> get_random.print_random()
     ** **> AttributeError: 'module' object has no attribute 'print_random'
     ** **>
     ** **> Here is some more detail from the REPL that may give a clue. I'm
     ** **wondering
     ** **> if there is something in the setup.py that I haven't
     ** **> defined correctly or not defined at all. I could share that if
     someone
     ** **> thinks it is worth a look. The setup.py that I started with
     ** **> was the one that is provided from
     [9]https://github.com/pypa/sampleproject
     ** **>
     ** **> Python 2.7.10 (default, Oct 23 2015, 19:19:21)
     ** **> [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on
     darwin
     ** **> Type "help", "copyright", "credits" or "license" for more
     information.
     ** **> >>> import get_random
     ** **> >>> dir(get_random)
     ** **> ['__builtins__', '__doc__', '__file__', '__name__',
     '__package__',
     ** **> '__path__']
     ** **> >>> print(get_random.__file__)
     ** **>
     **
     **/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
     ** **> >>> print(get_random.__package__)
     ** **> None
     ** **> >>> print(get_random.__path__)
     ** **>
     ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
     ** **>
     ** **> If you have bothered to read this far, I hope this has made some
     sense
     ** **and
     ** **> I thank you for any ideas you may be willing to share!
     ** **> Steve Gambino
     ** **> --
     ** **> Best,
     ** **> Steve
     ** **> Hello friends,
     ** **> I'm attempting to learn how to create a pip installable package.
     So far
     ** **> the attempt has been relatively successful but for one nagging
     issue.
     ** **I'm
     ** **> hoping if I describe the issue here clearly enough that someone
     can
     ** **offer
     ** **> pointers to help me get to a resolution.
     ** **> I've created the package (named get_random) successfully with
     setuptools
     ** **> and have been able to do a pip install of it in a virtualenv.
     ** **> The package "get_random" contains a trivial program also named
     ** **> "get_random". The program, "get_random", imports one module,
     'random',
     ** **and
     ** **> it has the functions, main(), get_random() and print_random().
     After the
     ** **> pip install of the get_random package, I can call the get_random
     program
     ** **> from the command line in the virtualenv and get the expected
     results. In
     ** **> otherwords, if I run the following in a terminal I get a
     resulting
     ** **random
     ** **> number, like so:
     ** **> sgambino-m1:~/vnv_for_pip > get_random
     ** **> 0.0259274305036
     ** **> However, if I try to import the get_random package into a python
     REPL I
     ** **do
     ** **> not see
     ** **> a complete list of what is in the package. I only see the
     following:
     ** **> >>> dir(get_random)
     ** **> >>> ['__builtins__', '__doc__', '__file__', '__name__',
     ** **> '__package__',
     ** **> '__path__']
     ** **>
     ** **> but I was expecting to see:
     ** **>
     ** **> >>> dir(get_random)
     ** **> ['__builtins__', '__doc__', '__file__', '__name__',
     '__package__',
     ** **> 'get_random', 'main', 'print_random', 'random']
     ** **>
     ** **> If I try to import the get_random package into a script and run
     the
     ** **> script, I get the following error:
     ** **>
     ** **> ncralsgambino-m1:~/vnv_for_pip > ./try_get_random.py
     ** **> Traceback (most recent call last):
     ** **> ** File "./try_get_random.py", line 5, in <module>
     ** **> ****** get_random.print_random()
     ** **> AttributeError: 'module' object has no attribute 'print_random'
     ** **>
     ** **> Here is some more detail from the REPL that may give a clue.**
     I'm
     ** **> wondering if there is something in the setup.py that I haven't
     ** **> defined correctly or not defined at all. I could share that if
     someone
     ** **> thinks it is worth a look. The setup.py that I started with
     ** **> was the one that is provided from
     ** **[1][10]https://github.com/pypa/sampleproject
     ** **> Python 2.7.10 (default, Oct 23 2015, 19:19:21)
     ** **> [GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on
     darwin
     ** **> Type "help", "copyright", "credits" or "license" for more
     information.
     ** **> >>> import get_random
     ** **> >>> dir(get_random)
     ** **> ['__builtins__', '__doc__', '__file__', '__name__',
     '__package__',
     ** **> '__path__']
     ** **> >>> print(get_random.__file__)
     ** **>
     **
     **/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random/__init__.pyc
     ** **> >>> print(get_random.__package__)
     ** **> None
     ** **> >>> print(get_random.__path__)
     ** **>
     ['/Users/sgambino/vnv_for_pip/lib/python2.7/site-packages/get_random']
     ** **> If you have bothered to read this far, I hope this has made some
     sense
     ** **and
     ** **> I thank you for any ideas you may be willing to share!
     ** **> Steve Gambino
     ** **> --
     ** **> Best,**
     ** **> Steve
     ** **>
     ** **> References
     ** **>
     ** **> Visible links
     ** **> 1. [11]https://github.com/pypa/sampleproject
     ** **>
     _______________________________________________
     TriZPUG mailing list
     [12]TriZPUG at python.org
     [13]https://mail.python.org/mailman/listinfo/trizpug
     [14]http://tripython.org is the Triangle Python Users Group

   --
   Best,**
   Steve

References

   Visible links
   1. https://github.com/sgambino/project-random
   2. https://github.com/sgambino/project-random
   3. https://packaging.python.org/
   4. mailto:david at handysoftware.com
   5. mailto:stevegambino at gmail.com
   6. mailto:TriZPUG at python.org
   7. https://mail.python.org/mailman/listinfo/trizpug
   8. http://tripython.org/
   9. https://github.com/pypa/sampleproject
  10. https://github.com/pypa/sampleproject
  11. https://github.com/pypa/sampleproject
  12. mailto:TriZPUG at python.org
  13. https://mail.python.org/mailman/listinfo/trizpug
  14. http://tripython.org/


More information about the TriZPUG mailing list