Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #82508 > unrolled thread

Python console rejects an object reference, having made an object with that reference as its name in previous line

Started bySimon Evans <musicalhacksaw@yahoo.co.uk>
First post2014-12-14 08:15 -0800
Last post2015-01-04 09:14 -0500
Articles 20 — 11 participants

Back to article view | Back to comp.lang.python


Contents

  Python console rejects an object reference, having made an object with that reference as its name in previous line Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-12-14 08:15 -0800
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Ned Batchelder <ned@nedbatchelder.com> - 2014-12-14 11:36 -0500
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-12-14 10:37 -0800
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-12-14 20:51 +0200
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Billy Earney <billy.earney@gmail.com> - 2014-12-14 10:43 -0600
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-12-14 15:21 -0800
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-12-15 11:36 +1100
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Chris Roy-Smith <chris_roysmith@internode.on.net> - 2014-12-15 13:32 +1100
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Chris Angelico <rosuav@gmail.com> - 2014-12-15 13:42 +1100
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-12-15 02:47 +0000
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Michael Torrie <torriem@gmail.com> - 2014-12-14 22:27 -0700
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Chris Angelico <rosuav@gmail.com> - 2014-12-15 16:32 +1100
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Michael Torrie <torriem@gmail.com> - 2014-12-14 22:38 -0700
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Zachary Ware <zachary.ware+pylist@gmail.com> - 2014-12-14 23:51 -0600
        Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Chris Angelico <rosuav@gmail.com> - 2014-12-15 16:52 +1100
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Simon Evans <musicalhacksaw@yahoo.co.uk> - 2014-12-18 08:19 -0800
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Michael Torrie <torriem@gmail.com> - 2014-12-18 09:35 -0700
    Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Simon Evans <musicalhacksaw@yahoo.co.uk> - 2015-01-04 04:49 -0800
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-04 13:11 +0000
      Re: Python console rejects an object reference, having made an object with that reference as its name in previous line Dave Angel <davea@davea.name> - 2015-01-04 09:14 -0500

#82508 — Python console rejects an object reference, having made an object with that reference as its name in previous line

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-12-14 08:15 -0800
SubjectPython console rejects an object reference, having made an object with that reference as its name in previous line
Message-ID<01937423-e700-4825-a502-568dcb2d5324@googlegroups.com>
Dear Python programmers,
Having input the line of code in text: 
cd Soup 
to the Windows console, and having put the file 'EcologicalPyramid.html' into the Directory 'Soup', on the C drive, in accordance with instructions I input the following code to the Python console, as given on page 30 of 'Getting Started with Beautiful Soup':
----------------------------------------------------------------------------
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup = BeautifulSoup(ecological_pyramid,"lxml")
... producer_entries = soup.find("ul")
 
           ^
SyntaxError: invalid syntax
>>> producer_entries = soup.find("ul")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'soup' is not defined
>>>
           ^
----------------------------------------------------------------------------
so I cannot proceed with the next line withh would 've been :
----------------------------------------------------------------------------
print(producer_entries.li.div.string)
----------------------------------------------------------------------------
which would've given (according to the book) the output:
---------------------------------------------------------------------------
plants

Maybe that is getting a bit far ahead, but I can't quite see where I have gone wrong - 'soup' has been defined as an object made of file 'EcologicalPyramid.html

I hope you can help me on this point. 
Yours 
Simon

[toc] | [next] | [standalone]


#82512

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-12-14 11:36 -0500
Message-ID<mailman.16950.1418575028.18130.python-list@python.org>
In reply to#82508
On 12/14/14 11:15 AM, Simon Evans wrote:
> Dear Python programmers,
> Having input the line of code in text:
> cd Soup
> to the Windows console, and having put the file 'EcologicalPyramid.html' into the Directory 'Soup', on the C drive, in accordance with instructions I input the following code to the Python console, as given on page 30 of 'Getting Started with Beautiful Soup':
> ----------------------------------------------------------------------------
> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
> 32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from bs4 import BeautifulSoup
>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = BeautifulSoup(ecological_pyramid,"lxml")
> ... producer_entries = soup.find("ul")
>
>             ^
> SyntaxError: invalid syntax

This SyntaxError is indicating that none of the code you have just typed 
was run.  The reason it's a syntax error is because the interactive 
prompt is a bit simplistic about how to handle multiple statements.  It 
wants a blank line after the with-clause.

Because none of these lines were run, you never opened your HTML file, 
never parsed it, and never assigned to the name "soup".

>>>> producer_entries = soup.find("ul")
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> NameError: name 'soup' is not defined
>>>>
>             ^
> ----------------------------------------------------------------------------
> so I cannot proceed with the next line withh would 've been :
> ----------------------------------------------------------------------------
> print(producer_entries.li.div.string)
> ----------------------------------------------------------------------------
> which would've given (according to the book) the output:
> ---------------------------------------------------------------------------
> plants
>
> Maybe that is getting a bit far ahead, but I can't quite see where I have gone wrong - 'soup' has been defined as an object made of file 'EcologicalPyramid.html
>
> I hope you can help me on this point.

For complex code experiments, it's better to put the code in a file, and 
run the file.  But if you do want to use the interactive interpreter, 
enter code carefully, and watch out for the error messages.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [next] | [standalone]


#82521

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-12-14 10:37 -0800
Message-ID<e5791b9a-9a60-41b6-b8c6-c92c2cc23246@googlegroups.com>
In reply to#82508
I had another attempt at inputting the code perhaps with the right indentation, I still get an error return, but not one that indicates that the code has not been read, as you suggested. re:- 
----------------------------------------------------------------------------

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup = BeautifulSoup(ecological_pyramid,"lxml")
... producer_entries = soup.find("ul")
  File "<stdin>", line 3
    producer_entries = soup.find("ul")
                   ^
SyntaxError: invalid syntax
>>>  from bs4 import BeautifulSoup
  File "<stdin>", line 1
    from bs4 import BeautifulSoup
----------------------------------------------------------------------------
If, as you suggest I left a free line after the "with open( etc" line, console returns an error, if I leave a free line after the "soup = etc" line which comes after, again I get an error return, my only point is that with the above input, console return does not seem to infer that soup has not been defined. You recommend that I put all the code into a file then run it - how do I do that ? I am new to Python, as you might have gathered. 
Thank you for your help.
Yours Simon

[toc] | [prev] | [next] | [standalone]


#82522

FromJussi Piitulainen <jpiitula@ling.helsinki.fi>
Date2014-12-14 20:51 +0200
Message-ID<qot388ixdxu.fsf@ruuvi.it.helsinki.fi>
In reply to#82521
Simon Evans writes:

> I had another attempt at inputting the code perhaps with the right
> indentation, I still get an error return, but not one that indicates
> that the code has not been read, as you suggested. re:-
> ----------------------------------------------------------------------
> 
> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit
> (Intel)] on win 32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from bs4 import BeautifulSoup
> >>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = BeautifulSoup(ecological_pyramid,"lxml")
> ... producer_entries = soup.find("ul")
>   File "<stdin>", line 3
>     producer_entries = soup.find("ul")
>                    ^
> SyntaxError: invalid syntax

It should look like this, except I'm using built-in functions to get a
single line from a file I happened to have around, and naming it beef
instead of soup:

Python 2.7.3 (default, Sep 26 2013, 16:38:10) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> with open('let.scm') as whatever:
...    beef = next(whatever)
... 
>>> beef
'(define-syntax lets ;let-values, attempt to mod\n'
>>> 

Note the line with only "..." on it? That's where I pressed enter to
let the interpreter know that I had written all the code I wanted it
to handle at that point. I was rewarded with (1) no error message, (2)
a new prompt, where I entered, er, the four letters b e e f and then
pressed enter, and the interpreter printed the first line of my file
for me.

[toc] | [prev] | [next] | [standalone]


#82527

FromBilly Earney <billy.earney@gmail.com>
Date2014-12-14 10:43 -0600
Message-ID<mailman.16956.1418594429.18130.python-list@python.org>
In reply to#82508

[Multipart message — attachments visible in raw view] — view raw

It looks like the last line (producer_entries...) is not indented at the
same extent as the previous line.  Maybe this is causing the issue?

On Sun, Dec 14, 2014 at 10:15 AM, Simon Evans <musicalhacksaw@yahoo.co.uk>
wrote:
>
> Dear Python programmers,
> Having input the line of code in text:
> cd Soup
> to the Windows console, and having put the file 'EcologicalPyramid.html'
> into the Directory 'Soup', on the C drive, in accordance with instructions
> I input the following code to the Python console, as given on page 30 of
> 'Getting Started with Beautiful Soup':
>
> ----------------------------------------------------------------------------
> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
> on win
> 32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from bs4 import BeautifulSoup
> >>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = BeautifulSoup(ecological_pyramid,"lxml")
> ... producer_entries = soup.find("ul")
>
>            ^
> SyntaxError: invalid syntax
> >>> producer_entries = soup.find("ul")
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'soup' is not defined
> >>>
>            ^
>
> ----------------------------------------------------------------------------
> so I cannot proceed with the next line withh would 've been :
>
> ----------------------------------------------------------------------------
> print(producer_entries.li.div.string)
>
> ----------------------------------------------------------------------------
> which would've given (according to the book) the output:
> ---------------------------------------------------------------------------
> plants
>
> Maybe that is getting a bit far ahead, but I can't quite see where I have
> gone wrong - 'soup' has been defined as an object made of file
> 'EcologicalPyramid.html
>
> I hope you can help me on this point.
> Yours
> Simon
> --
> https://mail.python.org/mailman/listinfo/python-list
>

[toc] | [prev] | [next] | [standalone]


#82532

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-12-14 15:21 -0800
Message-ID<41c32aed-315a-43df-9dc6-380d0ecc3425@googlegroups.com>
In reply to#82508
Dear Jussi, and Billy
I have changed the input in accordance with your advice, re:
------------------------------------------------------------------------------
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup = next(ecological_pyramid,"lxml")
...      producer_entries = soup.find("ul")
...      print(producer_entries.li.div.string)
... print(producer_entries.li.div.string)
  File "<stdin>", line 5
    print(producer_entries.li.div.string)
        ^
SyntaxError: invalid syntax
>>> print (producer_entries.li.div.string)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'producer_entries' is not defined
>>> from bs4 import BeautifulSoup
>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup = next(ecological_pyramid,"lxml")
...      producer_entries = soup.find("ul")
...      print(producer_entries.li.div.string)
...
----------------------------------------------------------------------------
As no doubt you can see, the last line, indented as it is, does not provide the output that the book's text says it will return - ie the word 'plants'
If I do not indent it, it returns an 'invalid syntax error' stating that 'producer_entries' is not defined. Though code in the previous line is meant to do just that - isn't it ? 

[toc] | [prev] | [next] | [standalone]


#82538

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2014-12-15 11:36 +1100
Message-ID<548e2d2d$0$12978$c3e8da3$5496439d@news.astraweb.com>
In reply to#82532
Simon Evans wrote:

> Dear Jussi, and Billy
> I have changed the input in accordance with your advice, re:
> ------------------------------------------------------------------------------
> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)]
> on win 32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from bs4 import BeautifulSoup
>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = next(ecological_pyramid,"lxml")
> ...      producer_entries = soup.find("ul")
> ...      print(producer_entries.li.div.string)
> ... print(producer_entries.li.div.string)


Simon, you are not leaving a blank space between the indented block and the
unindented line.

The interactive interpreter is quite simple, and you MUST leave a blank line
after indented blocks. You need to press the ENTER key *twice* at the end
of the block:

with open("ecologicalpyramid.html","r") as ecological_pyramid: ENTER
TAB soup = next(ecological_pyramid,"lxml") ENTER
TAB producer_entries = soup.find("ul") ENTER
TAB print(producer_entries.li.div.string) ENTER
ENTER
print(producer_entries.li.div.string) ENTER


Don't type the letters E N T E R, press the ENTER key. The same goes for
TAB. Note carefully that after the indented block, there is a blank line.
It should look like this:


>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup = next(ecological_pyramid,"lxml")
...      producer_entries = soup.find("ul")
...      print(producer_entries.li.div.string)
... 
>>> print(producer_entries.li.div.string)

If you don't leave the blank line, then *nothing* in that "with" block will
run and consequently the last print line will fail.

Until you fix that, we cannot even begin to help with any other issues you
might be having.



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#82541

FromChris Roy-Smith <chris_roysmith@internode.on.net>
Date2014-12-15 13:32 +1100
Message-ID<548e4837$0$13012$c3e8da3$5496439d@news.astraweb.com>
In reply to#82532
On 15/12/14 10:21, Simon Evans wrote:
> Dear Jussi, and Billy
> I have changed the input in accordance with your advice, re:
> ------------------------------------------------------------------------------
> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win

here the user is using python 2.7.6


> 32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> from bs4 import BeautifulSoup
>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = next(ecological_pyramid,"lxml")
> ...      producer_entries = soup.find("ul")
> ...      print(producer_entries.li.div.string)
> ... print(producer_entries.li.div.string)
>    File "<stdin>", line 5
>      print(producer_entries.li.div.string)
>          ^

and here he uses a python 3.x print syntax which triggers the following 
error message

> SyntaxError: invalid syntax
>>>> print (producer_entries.li.div.string)
> Traceback (most recent call last):
>    File "<stdin>", line 1, in <module>
> NameError: name 'producer_entries' is not defined
>>>> from bs4 import BeautifulSoup
>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup = next(ecological_pyramid,"lxml")
> ...      producer_entries = soup.find("ul")
> ...      print(producer_entries.li.div.string)
> ...
> ----------------------------------------------------------------------------
> As no doubt you can see, the last line, indented as it is, does not provide the output that the book's text says it will return - ie the word 'plants'
> If I do not indent it, it returns an 'invalid syntax error' stating that 'producer_entries' is not defined. Though code in the previous line is meant to do just that - isn't it ?
>

[toc] | [prev] | [next] | [standalone]


#82542

FromChris Angelico <rosuav@gmail.com>
Date2014-12-15 13:42 +1100
Message-ID<mailman.16966.1418611339.18130.python-list@python.org>
In reply to#82541
On Mon, Dec 15, 2014 at 1:32 PM, Chris Roy-Smith
<chris_roysmith@internode.on.net> wrote:
> and here he uses a python 3.x print syntax which triggers the following
> error message

On the contrary; parens around a single argument will work just fine
in Py2 (they're simply redundant parentheses, in the same way that you
can write "i = (1)" and it's the same as "i = 1"). The syntax error
comes from unindenting without ending the block, as several have
pointed out already.

ChrisA

[toc] | [prev] | [next] | [standalone]


#82543

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2014-12-15 02:47 +0000
Message-ID<mailman.16967.1418611665.18130.python-list@python.org>
In reply to#82541
On 15/12/2014 02:32, Chris Roy-Smith wrote:
> On 15/12/14 10:21, Simon Evans wrote:
>> Dear Jussi, and Billy
>> I have changed the input in accordance with your advice, re:
>> ------------------------------------------------------------------------------
>>
>> Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit
>> (Intel)] on win
>
> here the user is using python 2.7.6
>
>
>> 32
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> from bs4 import BeautifulSoup
>>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
>> ...      soup = next(ecological_pyramid,"lxml")
>> ...      producer_entries = soup.find("ul")
>> ...      print(producer_entries.li.div.string)
>> ... print(producer_entries.li.div.string)
>>    File "<stdin>", line 5
>>      print(producer_entries.li.div.string)
>>          ^
>
> and here he uses a python 3.x print syntax which triggers the following
> error message
>
>> SyntaxError: invalid syntax
>>>>> print (producer_entries.li.div.string)
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> NameError: name 'producer_entries' is not defined
>

I didn't realise that Python was so smart.  It can indicate a syntax 
error at the final 't' in print before it gets to the opening bracket 
that is required for the print function in Python 3 (and Python 2 if 
you're using "from __future__ import print_function")?

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#82546

FromMichael Torrie <torriem@gmail.com>
Date2014-12-14 22:27 -0700
Message-ID<mailman.16969.1418621243.18130.python-list@python.org>
In reply to#82541
On 12/14/2014 07:47 PM, Mark Lawrence wrote:
> I didn't realise that Python was so smart.  It can indicate a syntax 
> error at the final 't' in print before it gets to the opening bracket 
> that is required for the print function in Python 3 (and Python 2 if 
> you're using "from __future__ import print_function")?

Not really.  Python2 just interprets "print (value)" as an expression to
the print statement.  It's like saying "a=(value)" and then "print a".
Redundant but works.  However "print(value)" is interpreted as a
function call, and if you haven't imported it from future, it will error
out on Python2.

Python 3, on the other hand treats spaces between a function and its
opening paren to be optional whitespace.  Thus to python3,
"print(value)" and "print (value)" are the same.

So if you always put a space between print and (value) you can achieve
some measure of cross-version compatibility with print.  However by
including print_function from __future__ you are guaranteeing you won't
forget to add the parens.

[toc] | [prev] | [next] | [standalone]


#82547

FromChris Angelico <rosuav@gmail.com>
Date2014-12-15 16:32 +1100
Message-ID<mailman.16970.1418621552.18130.python-list@python.org>
In reply to#82541
On Mon, Dec 15, 2014 at 4:27 PM, Michael Torrie <torriem@gmail.com> wrote:
> On 12/14/2014 07:47 PM, Mark Lawrence wrote:
>> I didn't realise that Python was so smart.  It can indicate a syntax
>> error at the final 't' in print before it gets to the opening bracket
>> that is required for the print function in Python 3 (and Python 2 if
>> you're using "from __future__ import print_function")?
>
> Not really.  Python2 just interprets "print (value)" as an expression to
> the print statement.  It's like saying "a=(value)" and then "print a".
> Redundant but works.  However "print(value)" is interpreted as a
> function call, and if you haven't imported it from future, it will error
> out on Python2.

Did you actually test that?

Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> print("hello")
hello

Since print is a keyword when not imported from future, there's no way
for print(value) to be parsed as a function call.

ChrisA

[toc] | [prev] | [next] | [standalone]


#82548

FromMichael Torrie <torriem@gmail.com>
Date2014-12-14 22:38 -0700
Message-ID<mailman.16971.1418621897.18130.python-list@python.org>
In reply to#82541
On 12/14/2014 10:32 PM, Chris Angelico wrote:
> Did you actually test that?
> 
> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit
> (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>>>> print("hello")
> hello
> 
> Since print is a keyword when not imported from future, there's no way
> for print(value) to be parsed as a function call.

Thought I had indirectly, since I've been using this print technique for
the last few days.  Good to know it works either way, though.  Guess the
future import is only to make not having parens and error.

[toc] | [prev] | [next] | [standalone]


#82549

FromZachary Ware <zachary.ware+pylist@gmail.com>
Date2014-12-14 23:51 -0600
Message-ID<mailman.16972.1418622717.18130.python-list@python.org>
In reply to#82541
On Sun, Dec 14, 2014 at 11:38 PM, Michael Torrie <torriem@gmail.com> wrote:
> Guess the future import is only to make not having parens and error.

Python 2.7.8+ (default, Nov  2 2014, 00:32:19) [MSC v.1500 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print(1, 2)
(1, 2)
>>> from __future__ import print_function
>>> print(1, 2)
1 2

Also, without the future import you can't use the 'sep', 'end', and
'file' keyword arguments.

-- 
Zach

[toc] | [prev] | [next] | [standalone]


#82550

FromChris Angelico <rosuav@gmail.com>
Date2014-12-15 16:52 +1100
Message-ID<mailman.16973.1418622731.18130.python-list@python.org>
In reply to#82541
On Mon, Dec 15, 2014 at 4:38 PM, Michael Torrie <torriem@gmail.com> wrote:
> Thought I had indirectly, since I've been using this print technique for
> the last few days.  Good to know it works either way, though.  Guess the
> future import is only to make not having parens and error.

Yeah. I usually recommend using parens and a single native-string
argument, for maximum portability. That'll work even on pre-2.6 where
the future import isn't supported.

ChrisA

[toc] | [prev] | [next] | [standalone]


#82636

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2014-12-18 08:19 -0800
Message-ID<70b86783-13dc-419b-a69b-0922ba976520@googlegroups.com>
In reply to#82508
@Steven D'Aprano,
I input the following to Python 2.7, which got the following:- 
----------------------------------------------------------------------------
>>> from bs4 import BeautifulSoup
>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup= next(ecological_pyramid,"lxml")
...      producer_entries = soup.find("ul")
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'ecologicalpyramid.html'
>>>
----------------------------------------------------------------------------
- I kept to your instructions to input the 'Enter' after the fourth line and then before the fifth line, ie between the indented block and the unindented one, which as above, doesn't give me a chance to actually input the fifth line. If I do it both ways, ie: pressing enter after the fourth and before the fifth or just pressing enter after the fourth and then after the fifth line of input, which again it won't actually let me input because before I do, I still get an error return.

[toc] | [prev] | [next] | [standalone]


#82639

FromMichael Torrie <torriem@gmail.com>
Date2014-12-18 09:35 -0700
Message-ID<mailman.17033.1418920552.18130.python-list@python.org>
In reply to#82636
On 12/18/2014 09:19 AM, Simon Evans wrote:
> @Steven D'Aprano,
> I input the following to Python 2.7, which got the following:- 
> ----------------------------------------------------------------------------
>>>> from bs4 import BeautifulSoup
>>>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
> ...      soup= next(ecological_pyramid,"lxml")
> ...      producer_entries = soup.find("ul")
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> IOError: [Errno 2] No such file or directory: 'ecologicalpyramid.html'
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is the problem here. And it's not a syntax error.

> - I kept to your instructions to input the 'Enter' after the fourth
> line and then before the fifth line, ie between the indented block and
> the unindented one, which as above, doesn't give me a chance to actually
>input the fifth line. If I do it both ways, ie: pressing enter after the
>fourth and before the fifth or just pressing enter after the fourth and
>then after the fifth line of input, which again it won't actually let me
>input because before I do, I still get an error return.

Did you read the actual error message? In this case it's not a syntax
error.  Do you understand what the error actually is and why it's
happening?  (IE did your read what the error was? Hint. It's not the
same as your previous errors.)

[toc] | [prev] | [next] | [standalone]


#83204

FromSimon Evans <musicalhacksaw@yahoo.co.uk>
Date2015-01-04 04:49 -0800
Message-ID<c16ce690-c44d-4ace-b010-552794aa7461@googlegroups.com>
In reply to#82508
Dear Michael Torrie,
Thanks for pointing that out to me re: it not being a syntax problem.
The thing is there is a file called 'EcologicalPyramid.html'. I put it in a folder called 'Soup' as the text advised on page 28. For what its worth I also shifted the Windows Command Prompt to that folder (re: cd Soup)as instructed on page 30, and put a duplicate file of 'EcologicalPyramid.html' in the python 2.8 directory. 
I therefore am wondering where I ought put this html file where the Python console will recognize it ? 
Thank you for your attention,
Yours 
Simon  

[toc] | [prev] | [next] | [standalone]


#83205

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-01-04 13:11 +0000
Message-ID<mailman.17372.1420377108.18130.python-list@python.org>
In reply to#83204
On 04/01/2015 12:49, Simon Evans wrote:
> Dear Michael Torrie,
> Thanks for pointing that out to me re: it not being a syntax problem.
> The thing is there is a file called 'EcologicalPyramid.html'. I put it in a folder called 'Soup' as the text advised on page 28. For what its worth I also shifted the Windows Command Prompt to that folder (re: cd Soup)as instructed on page 30, and put a duplicate file of 'EcologicalPyramid.html' in the python 2.8 directory.
> I therefore am wondering where I ought put this html file where the Python console will recognize it ?
> Thank you for your attention,
> Yours
> Simon
>

You could put your file on the moon provided you give the code a 
complete path to it, something like 
r'c:\the\path\to\your\file\called\EcologicalPyramid.html'

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#83207

FromDave Angel <davea@davea.name>
Date2015-01-04 09:14 -0500
Message-ID<mailman.17373.1420380898.18130.python-list@python.org>
In reply to#83204
On 01/04/2015 07:49 AM, Simon Evans wrote:
> Dear Michael Torrie,
> Thanks for pointing that out to me re: it not being a syntax problem.
> The thing is there is a file called 'EcologicalPyramid.html'. I put it in a folder called 'Soup' as the text advised on page 28.
> For what its worth I also shifted the Windows Command Prompt to that folder (re: cd Soup)as instructed on page 30,
> and put a duplicate file of 'EcologicalPyramid.html' in the python 2.8 directory.

Never put any user files in the "python 2.8" directory, whatever that is.

> I therefore am wondering where I ought put this html file where the Python console will recognize it ?

Since your code expects it in the current directory, either change the 
code, or make sure it IS in the current directory.

The only obvious thing I can see is that you didn't spell the name the 
same.  But since Windows makes the error of ignoring case, it ought to 
be close enough.  However, you're typing the name, not pasting it, so 
maybe there's some other subtle difference, like you have more than one 
Soup directory.

Try the following, from the cmd.exe prompt:

myprompt> cd Soup
myprompt> dir *.html
   (something shows here)
myprompt> python
   (something shows here)


 >>> from bs4 import BeautifulSoup
 >>> with open("ecologicalpyramid.html","r") as ecological_pyramid:
...      soup= next(ecological_pyramid,"lxml")
...      producer_entries = soup.find("ul")
...

and see what happens.  Paste the whole thing into a message if it 
doesn't make sense to you.  And of course don't forget to include the 
traceback.



-- 
DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web