3

When I try to run the following Python 3.3 code on OS X 10.8 in PyCharm 2.7 (or run the .py file with the Python 3.3/2.7.3 launcher):

import urllib.request
f = urllib.request.urlopen('http://www.python.org/')
print(f.read(300))

I get the following error message:

/System/Library/Frameworks/Python.framework/Versions/3.3/bin/python3 /Users/username/PycharmProjects/urllib/urllib.py
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 1512, in _find_and_load_unlocked
AttributeError: 'module' object has no attribute '__path__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in <module>
    import urllib.request
  File "/Users/username/PycharmProjects/urllib/urllib.py", line 3, in <module>
    import urllib.request
ImportError: No module named 'urllib.request'; urllib is not a package

Process finished with exit code 1

The only way I can succesfully run the code is via the Python shell.

Any ideas on how to solve this?

Thanks.


I changed the filename to url.py, now it succesfully executes in PyCharm.

But when executing the file via Python Launcher 3.3 it gives me the following error:

 File "/Users/username/PycharmProjects/urllib/url.py", line 3, in <module>
import urllib.request
ImportError: No module named request

Why is the code running fine in PyCharm (3.3) but giving me an error when launched with Python Launcher (3.3)?

narzero
  • 2,199
  • 5
  • 40
  • 73

3 Answers3

16

You named your file urllib, it is shadowing the standard library package. Rename your file.

Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
  • 3
    +1 for submitting an answer instead of leaving a drive-by comment – mechanical_meat Apr 04 '13 at 20:55
  • I changed the filename to url.py, now it succesfully executes in PyCharm. But when executing the file via Python Launcher 3.3 it gives me the following error: File "/Users/narekaramjan/PycharmProjects/urllib/url.py", line 3, in import urllib.request ImportError: No module named request Why is the code running fine in PyCharm (3.3) but giving me an error when launched with Pythong Launcher (3.3)? – narzero Apr 04 '13 at 21:02
  • I have no idea what "Python Launcher" is. Does it use python3? Do you have an `__init__.py` file? Can your project (it's also named urllib) shadow the `urllib` package? – Pavel Anossov Apr 04 '13 at 21:05
  • On OS X the Python Launcher is the app that launches/runs your *.py file. Solved the problem by deleting the url.pyc file using http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error Thanks for you help Pavel! – narzero Apr 04 '13 at 21:13
2

In mac, VSCode is using Python 2. If you print urllib module, you will find out that this module is for Python 2.

So, I added one comment to choose Python version:

#!/usr/local/bin/python3
from urllib.request import urlopen
colidyre
  • 4,170
  • 12
  • 37
  • 53
  • See https://stackoverflow.com/questions/17846908/proper-shebang-for-python-script for how to set a good shebang line. – colidyre Apr 05 '20 at 13:40
0

In my case, it waw solved activating in command palette (shift+commant+p) Linting: Enable Linting: on and in VMStudio Settings (Preferences => Settings) select flake8 in Python:linter => Linter to use as flake8.

Nuria Ruiz
  • 149
  • 1
  • 9