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

Steve Gambino stevegambino at gmail.com
Wed Nov 1 15:29:23 EDT 2017


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
-------------- next part --------------
   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


More information about the TriZPUG mailing list