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?
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.
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!
Related
I am working with phonenumbers module in Python. I am having the issue of circular import. This error omits whenever I run the file from desktop location (C:\Users\AsadA\Desktop). But it raises an error whenever I tried to run this in a particular folder (C:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER ). Please help me!
Sample Code:
import phonenumbers
from phonenumbers import geocoder
from phonenumbers import carrier
from phonenumbers import timezone
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))
ERROR:
Traceback (most recent call last):
File "c:/Users/AsadA/Desktop/Python_projects/28-FindingTheNUMBER/phonenumbers.py", line 1, in <module>
import phonenumbers
File "c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py", line 2, in <module>
from phonenumbers import geocoder
ImportError: cannot import name 'geocoder' from partially initialized module 'phonenumbers' (most likely due to a circular import) (c:\Users\AsadA\Desktop\Python_projects\28-FindingTheNUMBER\phonenumbers.py)
You might probably named your file as "phonenumber.py". If you are importing something in python, please make sure that file name is not same as imported file. If it is same, then it will create an error.
This happens due to same name conflict with imported file as imported file name is also the same. And if this occur then python always give priority to file with current directory you are working.
So, let say your code is something like below.
import xyz
print(xyz.version)
And your file name is "xyz.py". Python compiler now sees that there are two files of same name "xyz.py", one in script folder where python is installed and another in current directory we are working. So, python compiler compiler choose file to import from current directory you are working on.
So, python read first line import xyz, it imports the file from current directory which means it import again this file and start reading it. In that, again first line is import xyz, then it again imports xyz in current folder causing a loop to occur.
This is called as circular loop.
So, in short, changing your file name can solve the problem.
You are importing the module phonenumbers using 'import phonenumbers' and then you are importing the relevant definitions inside that module in the next few lines. They are redundant.
Fixed code:
import phonenumbers
my_Num=phonenumbers.parse("SAMPLE_NUM")
print(phonenumbers.geocoder.description_for_number(my_Num,'en'))
print(phonenumbers.carrier.name_for_number(my_Num,'en'))
print(phonenumbers.timezone.time_zones_for_number(my_Num))
Or something like this:
from phonenumbers import (
parse,
geocoder,
carrier,
timezone,
)
my_Num=parse("SAMPLE_NUM")
print(geocoder.description_for_number(my_Num,'en'))
print(carrier.name_for_number(my_Num,'en'))
print(timezone.time_zones_for_number(my_Num))
My file is named "foo.py". It has only two lines.
import random
print(random.randint(10))
The error is...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py", line 45, in <module>
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
File "math.py", line 2, in <module>
from random import randint
ImportError: cannot import name randint
I am on MacOS 10.14.6
I note that I did not call random.randint() in this script, even
though it showed up in the error text.
I ran the script with $python
My /usr/bin/python is linked to python 2.7
I've tried this with python 3 as well, with the same error.
EDIT:
My script was originally named "math.py", but I changed it in response to another solution that pointed out the name conflict with the math.py library (even though my script was not importing that library). Even after my script name change, I'm still seeing --File "math.py"-- errors. Even after I'm no longer using random.randint(), I'm still seeing that function referenced in my errors.
I've tried deleting random.pyc and math.pyc to purge the artifacts of previous executions. But these do not see to eliminate the remnants of earlier errors.
Read the traceback:
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.py"
Python tries to do something inside the standard library random module...
from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
in particular, it tries to import the standard library math module...
File "math.py", line 2, in <module>
but it gets yours instead (notice there's no path on the filename this time; it's just math.py in the current directory); i.e. the script you started from. Python detects the circular import and fails:
ImportError: cannot import name randint
Actually using randint doesn't matter, because this is a problem with the actual import of the module.
This happens because Python is configured by default (using sys.path, which is a list of paths to try, in order) to try to import scripts from the current working directory before looking anywhere else. It's convenient when you just want to write a few source files in the same folder and have them work with each other, but it causes these problems.
The expected solution is to just rename your file. Unfortunately there isn't an obvious list of names to avoid, although you could peek at your installation folder to be sure (or just check the online library reference, though that's not quite so direct).
I guess you could also modify sys.path:
import sys
sys.path.remove('') # the empty string in this list is for the current directory
sys.path.append('') # put it back, at the end this time
import random # now Python will look for modules in the standard library first,
# and only in the current folder as a last resort.
However, this is an ugly hack. It might break something else (and it can't save you if you have a local sys.py).
I've been working on a project where I have a file which needs to call a function from a file in a sub package/directory, which in turn is calling a function from another file in the same sub package. As such, I have a main file which is importing a sub file. This sub file is also importing another sub file which is in the same package.
The first sub file has no issue whatsoever importing the second sub file. The main file also has no issue importing the first sub file. However, when I put it all together and run the main file, Python thinks that the second sub file doesn't exist, which I find strange. I've simplified and visualised my problem with an example below:
I have the following file hierarchy:
test_package\
__init__.py
main_file.py
test_sub_package\
__init__.py
subfile1.py
subfile2.py
main_file code:
import test_sub_package.subfile1
subfile1 code:
import subfile2
subfile2 code:
def get_string():
return ("Hello, World!")
So, I would expect main_file to import subfile2 via subfile1. However this doesn't seem to be the case because I get an error:
Traceback (most recent call last):
File "...\Test\main_file.py", line 1, in <module>
import test_package.subfile1
File "...\Test\test_sub_package\subfile1.py", line 1, in <module>
import subfile2
ModuleNotFoundError: No module named 'subfile2'
I was a little surprised that I got this error before I even attempted to call the functionality in subfile2. Either way, I'm confused why this doesn't work. Am I just doing something stupid here or am I trying to do something Python fundamentally doesn't support. If anyone can give me a solution it would be most appreciated.
I suspect this is probably a duplicate but I couldn't find an answer to my specific problem. So, sorry in advance.
When you import a module into another module from the same directory you must use must use a relative import in subfile1.py you will need to write:
from . import subfile2
Note, that doesn't give subfile 1 access to get_string to use it in subfile1, you would need to either write subfile2.get_string() or import it directly with:
from .subfile2 import get_string
I have tried this out and it works, I hope this helps :)
Note: that, if you are running a python script, and you need to import a module in that same directory, you can just say import module_name. It makes a difference if it is a script you are running, or a module that is being used in some other script. For a detailed explanation as to why see here
(I assume from your error message that you want to run main.py, if this is not the case you will need to change import test_sub_package.subfile1 to from . import test_sub_package.subfile1)
main file should be:
from test_sub_package.subfile1 import get_string
get_string()
subfile1.py
import test_sub_package.subfile2
I've found that I need to install 'glue' first.
But after that, this error is still there.
Traceback (most recent call last):
File "C:\Users\Saisa\Desktop\Code\python\myWebSpiderForPixiv_top100.py", line 1, in <module>
from gwpy.timeseries import TimeSeries
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\timeseries\__init__.py", line 27, in <module>
from .core import *
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\timeseries\core.py", line 45, in <module>
from ..data import (Array2D, Series)
File "C:\Users\Saisa\AppData\Local\Programs\Python\Python35-32\lib\site-packages\gwpy\data\__init__.py", line 29, in <module>
from glue.lal import (Cache, CacheEntry)
ImportError: No module named 'glue.lal'
Here is another idea of what you could try:
If I understand right, your directory hieracy looks something like this:
\root
\glue
\lal
\other
\another
and in lal there are the functions and classes and whatever you want. Is lal a directory or a file? If it is a directory, I don't understand why It is not working, I just wrote a analogue code and for me it worked perfectly. Maybe ther error is somewhere entierely else, sh*t happens in python, too. But if it is a file like lal.py you don't have to tell from glue.lal import (whatsoever)! This whatsoever are functions then in one file. Just type from glue import lal, it will import everything in there.
Your Problem is that Python does not know where to find the module. You must add the path to the Path Browser where the module code is saved in and then you can use the
import themoduleyouwant
Or
from themoduleyouwant import *
command.
You can find the Path Browser under "File >> Option >> Path Browser"
Usually, it will show you the standard libraries and your current working directory (cwd). So if you are looking for a quick solution, just take the directory of 'glue' and copy+paste it into your cwd.
Result: The Error will disappear.
I have a simple test script:
import requests
response = requests.get('http://httpbin.org/get')
print response.text
It works when the python script is named test.py but fails if named email.py or logging.py:
Traceback (most recent call last):
File "./email.py", line 3, in <module>
import requests
File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 53, in <module>
from urllib3.contrib import pyopenssl
File "/usr/lib/python2.7/dist-packages/urllib3/__init__.py", line 16, in <module>
from .connectionpool import (
File "/usr/lib/python2.7/dist-packages/urllib3/connectionpool.py", line 59, in <module>
from .request import RequestMethods
File "/usr/lib/python2.7/dist-packages/urllib3/request.py", line 12, in <module>
from .filepost import encode_multipart_formdata
File "/usr/lib/python2.7/dist-packages/urllib3/filepost.py", line 15, in <module>
from .fields import RequestField
File "/usr/lib/python2.7/dist-packages/urllib3/fields.py", line 7, in <module>
import email.utils
File "/home/ubuntu/temp/email.py", line 4, in <module>
response = requests.get('http://httpbin.org/get')
AttributeError: 'module' object has no attribute 'get'
It appears that requests imports urllib3, which imports email built-in module. Why is Python not finding the built-in email module first, instead of looking in the current path for email.py?
Is there a way to make this work, or do I just have to always avoid naming my Python scripts any built-in module that may be imported by any dependency?
First, as you indicated in a comment, Python checks for a 'built-in' module of that name. Not all modules in the standard library are 'built-in'. You can see the list by:
print sys.builtin_module_names
If it's not found there, the order searched is outlined by Burhan's accepted answer here:
What is the extent of the import statement in Python
Python searches for things it can import in the following order:
From the directory where the script was executed.
From the directories in the PYTHONPATH environment variable (if its set).
From the system-wide Python installation's module directory.
In your case, email is not built-in, so the current directory is checked first.
So, yes, don't shadow a python library name. Technically you could shadow a built-in module's name, but please - please don't. It makes Python cry.
Take a look at sys.path and you'll see '' as the first entry:
>>> import sys
>>> sys.path
['',
'/.../3.3/lib/python33.zip',
'/.../3.3/lib/python3.3',
'/.../3.3/lib/python3.3/plat-darwin',
'/.../3.3/lib/python3.3/lib-dynload',
'/.../3.3/lib/python3.3/site-packages']
That '' is the current directory.
You could modify sys.path, but it's a wiser decision, long-term, to just not give python files names that match builtin modules.
The reason Python is looking for the modules in the current directory first is that that's how Python works. If you look at sys.path you will see that its first element is almost always '' which indicates the current directory. This applies to everything but the built-in modules, which are "imported" at interpreter startup. (They are not really imported, just assigned into sys.modules.)
In general, you are supposed to not name your own modules the same names as other modules you're using (whether they came with Python or not). I tend to put my initials on scripts I'm working with just to avoid this pitfall.
Of course, you can just manipulate sys.path so it doesn't look for modules in the current directory:
import sys
if not sys.path[0]:
del sys.path[0]
import requests
Note that you only need to remove the '' entry when you import. You should put it back after you do your imports, in case you need to import modules of the same name from your own script's directory. A context manager is handy for this.
import sys
from contextlib import contextmanager
#contextmanager
def no_cwd_imports():
old_path = sys.path[:]
if not sys.path[0]:
del sys.path[0]
try:
yield
finally:
sys.path[:] = old_path
with no_cwd_imports():
import requests
That's not a problem with Requests.
The "problem" comes from Python. email and logging are standard modules, it is not advisable to name your own modules same unless you understand and are willing to resolve conflicts.