0

I'm following some online python tutorials and i ran across this syntax error code twice now and can't figure out what's wrong.

Here's my code:

import urllib
import re

htmlfile = urllib.urlopen("http://finance.yahoo.com/q?s=AAPL")
htmltext = htmlfile.read()

regex = '<span id="yfs_l84_aapl">(.+?)</span>'
pattern = re.compile(regex)
price = re.findall(pattern, html)

print price

I am using Enthought Python Distribution package (python version 2.7.3)

Here's the syntax error when i run the above script.

 Traceback (most recent call last):
  File "E:\python\scripts\stocks.py", line 4, in <module>
    htmlfile = urllib.urlopen("http://finance.yahoo.com/q?s=AAPL")
  File "e:\python27\lib\urllib.py", line 86, in urlopen
    return opener.open(url)
  File "e:\python27\lib\urllib.py", line 207, in open
    return getattr(self, name)(url)
  File "e:\python27\lib\urllib.py", line 291, in open_http
    import httplib
  File "e:\python27\lib\httplib.py", line 79, in <module>
    import mimetools
  File "e:\python27\lib\mimetools.py", line 6, in <module>
    import tempfile
  File "e:\python27\lib\tempfile.py", line 34, in <module>
    from random import Random as _Random
  File "E:\python\scripts\random.py", line 1
    def random
             ^
SyntaxError: invalid syntax

I tried searching around to understand what's going on but no avail. What's python trying to tell me with all these traceback lines and why do I get a syntax error?

2
  • It looks like you have an (incomplete?) file called random.py in your directory that conflicts with a system library with the same name. Try changing the name of your random.py Commented Mar 9, 2014 at 4:22
  • Also note that you have used 'html' as argument instead of 'htmltext' in re.findall() ;) Commented Mar 9, 2014 at 4:38

2 Answers 2

6

If you look at the stack trace, towards the bottom, you can see that the syntax error is in a file called E:\python\scripts\random.py. This script is one you've added to the system, and it appears to contain a syntax error. Because it's named random and is located in the Scripts directory, it is "overriding" the built-in random library.

Remove or rename that file and you should be good to go.

Sign up to request clarification or add additional context in comments.

4 Comments

ah perfect, i did have a random.py script. Thank you. So why doesn't python access the random.py library in the python27/lib folder instead of whichever folder the script is being run from?
@user2241963: Because the default is to look first in the directory you're in, before looking for packages.
s/you're/the script is/
@user2241963: See docs.python.org/2/tutorial/modules.html#the-module-search-path for more details on how the import module search finds what it's looking for. Basically, the input script directory is always searched in first.
1

The import of module is controlled by sys.path. To a first approximation when your program executes import module the interpreter visits the directories on sys.path in turn. It looks for a module.pyc file and a module.py file in each directory, moving on to the next if it finds neither. When it finds one or both, if the .py is newer than the .pyc (in other words, if the source has been modified since the file was last compiled, or if the compiled file does not exist) then the interpreter compiles the .py file and attempts to write to it. There are no guarantees your process will be able to write in that particular directory.

Having identified the correct file the interpreter creates a new namespace, executes the code of the module with that namespace as local, then binds the name of the module with the namespace just created. So if you have bound a name in module it should (modulo any __all__ assignment in the module's code) be available within your importing module as module.name.

I suspect this question has taught you why it's not a good idea to duplicate the names of system libraries in your own code - a lesson we all have to learn!

2 Comments

Indeed it has. Thanks for the in depth answer which cemented this concept. Cheers!
Something else: the interpreter also looks for a directory called module containing a file called __init__.py[c]. On finding such a package the interpreter executes the --init__.py code in a new namespace and binds the package name to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.