[TriPython] Problems with import after attempting to create pip installable package
Steve Gambino
stevegambino at gmail.com
Thu Nov 2 13:13:20 EDT 2017
Hi Dave,
Thank you so much for taking the time to elaborate on each step of the
setup.py process that I had asked about. It makes so much more sense now.
I'll be able to take this knowledge and apply it to one of our internal
tools which we are currently working on. Yes, very helpful indeed!
Steve
On Thu, Nov 2, 2017 at 10:47 AM David Handy <david at handysoftware.com> wrote:
> Hi Steve -
>
>
>
> Actually people refer to a "distributable unit of Python code" as a
> "package" all the time, it was only confusing to me in this context
> because we were switching back and forth talking about both kinds of
> packages. :)
>
>
>
> Here's what I did:
>
>
>
> git clone [1]https://github.com/sgambino/project-random.git
>
> virtualenv -p python3 env
>
> source env/bin/activate
>
> pip install -e project-random/
>
> sample_random
>
>
>
> Which printed a random number:
>
> 0.5678855151148471
>
>
>
> So far so good! Now for importing it:
>
>
>
> python
>
> # This just imports the __init__.py file in the sample_random directory
>
> >>> import sample_random
>
> # This imports the try_random module from the sample_random directory
>
> >>> from sample_random import try_random
>
> # This of course calls the main() function in the try_random module
>
> >>> try_random.main()
> 0.20974369539964732
>
>
>
> This is all standard behavior for Python "packages" (importable
> directories) whether or not pip/virtualenv is involved.
>
>
>
> As for entry points - notice in your setup.py file, you have this
> parameter in your call to setup():
>
>
>
> entry_points={
>
> 'console_scripts': [
>
> 'sample_random=sample_random.try_random:main',
>
> ],
>
> }
>
>
>
> What this means is: Tell setuptools to create a list of scripts
> (currently
> a list of just one). Create a script named "sample_random" that imports
> the try_random module from the sample_random package and calls the
> main()
> function to run it.
>
>
>
> If you look in the bin directory of the virtual environment where pip
> install this "package" (distribution of code), you see that setuptools
> created a file named sample_random with these contents:
>
>
>
> #!/home/david/temp/env/bin/python3
> # EASY-INSTALL-ENTRY-SCRIPT:
> 'sample-random','console_scripts','sample_random'
> __requires__ = 'sample-random'
> import re
> import sys
> from pkg_resources import load_entry_point
>
> if __name__ == '__main__':
> sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
> sys.exit(
> load_entry_point('sample-random', 'console_scripts',
> 'sample_random')()
> )
>
>
>
> Notice at the top of the file is a #! line that points to the python
> interpreter for this virtual environment. The rest of the gobbledygook
> loads your loads your try_random module and runs that main function in
> such a way that it looks like it got run from the command line. It uses
> metadata installed by setuptools in the site-packages of your virtual
> environment to find the entry point, and to know which package and
> module
> to load and which function to run.
>
>
>
> I hope this helps. I gotta get back to work now!
>
> David H
>
>
>
>
>
> On Thursday, November 2, 2017 1:07am, "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
> > 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
> > 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/
> >
>
> References
>
> Visible links
> 1. https://github.com/sgambino/project-random.git
> _______________________________________________
> 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 Dave,
Thank you so much for taking the time to elaborate on each step of the
setup.py process that I had asked about. It makes so much more sense now.
I'll be able to take this knowledge and apply it to one of our internal
tools which we are currently working on.** Yes, very helpful indeed!
Steve
On Thu, Nov 2, 2017 at 10:47 AM David Handy <[1]david at handysoftware.com>
wrote:
** **Hi Steve -
** **Actually people refer to a "distributable unit of Python code" as a
** **"package" all the time, it was only confusing to me in this context
** **because we were switching back and forth talking about both kinds
of
** **packages. :)
** **Here's what I did:
** **git clone [1][2]https://github.com/sgambino/project-random.git
** **virtualenv -p python3 env
** **source env/bin/activate
** **pip install -e project-random/
** **sample_random
** **Which printed a random number:
** **0.5678855151148471
** **So far so good! Now for importing it:
** **python
** **# This just imports the __init__.py file in the sample_random
directory
** **>>> import sample_random
** **# This imports the try_random module from the sample_random
directory
** **>>> from sample_random import try_random
** **# This of course calls the main() function in the try_random module
** **>>> try_random.main()
** **0.20974369539964732
** **This is all standard behavior for Python "packages" (importable
** **directories) whether or not pip/virtualenv is involved.
** **As for entry points - notice in your setup.py file, you have this
** **parameter in your call to setup():
** **entry_points={
** **'console_scripts': [
** **'sample_random=sample_random.try_random:main',
** **],
** **}
** **What this means is: Tell setuptools to create a list of scripts
(currently
** **a list of just one). Create a script named "sample_random" that
imports
** **the try_random module from the sample_random package and calls the
main()
** **function to run it.
** **If you look in the bin directory of the virtual environment where
pip
** **install this "package" (distribution of code), you see that
setuptools
** **created a file named sample_random with these contents:
** **#!/home/david/temp/env/bin/python3
** **# EASY-INSTALL-ENTRY-SCRIPT:
** **'sample-random','console_scripts','sample_random'
** **__requires__ = 'sample-random'
** **import re
** **import sys
** **from pkg_resources import load_entry_point
** **if __name__ == '__main__':
** ** ** **sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '',
sys.argv[0])
** ** ** **sys.exit(
** ** ** ** ** **load_entry_point('sample-random', 'console_scripts',
** **'sample_random')()
** ** ** **)
** **Notice at the top of the file is a #! line that points to the
python
** **interpreter for this virtual environment. The rest of the
gobbledygook
** **loads your loads your try_random module and runs that main function
in
** **such a way that it looks like it got run from the command line. It
uses
** **metadata installed by setuptools in the site-packages of your
virtual
** **environment to find the entry point, and to know which package and
module
** **to load and which function to run.
** **I hope this helps. I gotta get back to work now!
** **David H
** **On Thursday, November 2, 2017 1:07am, "Steve Gambino"
** **<[3]stevegambino at gmail.com> said:
** **> _______________________________________________
** **> TriZPUG mailing list
** **> [4]TriZPUG at python.org
** **> [5]https://mail.python.org/mailman/listinfo/trizpug
** **> [6]http://tripython.org is the Triangle Python Users Group
** **> 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 [7]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
[8]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 [9]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
<[10]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"
** **> > <[11]stevegambino at gmail.com> said:
** **> >
** **> > > _______________________________________________
** **> > > 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
** **> > > 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
** **> > [15]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][16]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. [17]https://github.com/pypa/sampleproject
** **> > >
** **> > _______________________________________________
** **> > TriZPUG mailing list
** **> > [18]TriZPUG at python.org
** **> > [19]https://mail.python.org/mailman/listinfo/trizpug
** **> > [20]http://tripython.org is the Triangle Python Users Group
** **> >
** **> --
** **> Best,
** **> Steve
** **> 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][21]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][22]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][23]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][24]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][25]stevegambino at gmail.com> said:
** **>
** **> ** **> _______________________________________________
** **> ** **> TriZPUG mailing list
** **> ** **> [6][26]TriZPUG at python.org
** **> ** **> [7][27]https://mail.python.org/mailman/listinfo/trizpug
** **> ** **> [8][28]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][29]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][30]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][31]https://github.com/pypa/sampleproject
** **> ** **>
** **> _______________________________________________
** **> TriZPUG mailing list
** **> [12][32]TriZPUG at python.org
** **> [13][33]https://mail.python.org/mailman/listinfo/trizpug
** **> [14][34]http://tripython.org is the Triangle Python Users Group
** **>
** **> --
** **> Best,**
** **> Steve
** **>
** **> References
** **>
** **> Visible links
** **> 1. [35]https://github.com/sgambino/project-random
** **> 2. [36]https://github.com/sgambino/project-random
** **> 3. [37]https://packaging.python.org/
** **> 4. mailto:[38]david at handysoftware.com
** **> 5. mailto:[39]stevegambino at gmail.com
** **> 6. mailto:[40]TriZPUG at python.org
** **> 7. [41]https://mail.python.org/mailman/listinfo/trizpug
** **> 8. [42]http://tripython.org/
** **> 9. [43]https://github.com/pypa/sampleproject
** **> 10. [44]https://github.com/pypa/sampleproject
** **> 11. [45]https://github.com/pypa/sampleproject
** **> 12. mailto:[46]TriZPUG at python.org
** **> 13. [47]https://mail.python.org/mailman/listinfo/trizpug
** **> 14. [48]http://tripython.org/
** **>
References
** **Visible links
** **1. [49]https://github.com/sgambino/project-random.git
_______________________________________________
TriZPUG mailing list
[50]TriZPUG at python.org
[51]https://mail.python.org/mailman/listinfo/trizpug
[52]http://tripython.org is the Triangle Python Users Group
--
Best,**
Steve
References
Visible links
1. mailto:david at handysoftware.com
2. https://github.com/sgambino/project-random.git
3. mailto:stevegambino at gmail.com
4. mailto:TriZPUG at python.org
5. https://mail.python.org/mailman/listinfo/trizpug
6. http://tripython.org/
7. https://github.com/sgambino/project-random
8. https://github.com/sgambino/project-random
9. https://packaging.python.org/
10. mailto:david at handysoftware.com
11. mailto:stevegambino at gmail.com
12. mailto:TriZPUG at python.org
13. https://mail.python.org/mailman/listinfo/trizpug
14. http://tripython.org/
15. https://github.com/pypa/sampleproject
16. https://github.com/pypa/sampleproject
17. https://github.com/pypa/sampleproject
18. mailto:TriZPUG at python.org
19. https://mail.python.org/mailman/listinfo/trizpug
20. http://tripython.org/
21. https://github.com/sgambino/project-random
22. https://github.com/sgambino/project-random
23. https://packaging.python.org/****
24. mailto:david at handysoftware.com
25. mailto:stevegambino at gmail.com
26. mailto:TriZPUG at python.org
27. https://mail.python.org/mailman/listinfo/trizpug
28. http://tripython.org/
29. https://github.com/pypa/sampleproject
30. https://github.com/pypa/sampleproject
31. https://github.com/pypa/sampleproject
32. mailto:TriZPUG at python.org
33. https://mail.python.org/mailman/listinfo/trizpug
34. http://tripython.org/
35. https://github.com/sgambino/project-random
36. https://github.com/sgambino/project-random
37. https://packaging.python.org/
38. mailto:david at handysoftware.com
39. mailto:stevegambino at gmail.com
40. mailto:TriZPUG at python.org
41. https://mail.python.org/mailman/listinfo/trizpug
42. http://tripython.org/
43. https://github.com/pypa/sampleproject
44. https://github.com/pypa/sampleproject
45. https://github.com/pypa/sampleproject
46. mailto:TriZPUG at python.org
47. https://mail.python.org/mailman/listinfo/trizpug
48. http://tripython.org/
49. https://github.com/sgambino/project-random.git
50. mailto:TriZPUG at python.org
51. https://mail.python.org/mailman/listinfo/trizpug
52. http://tripython.org/
More information about the TriZPUG
mailing list