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)?

2
  • Name your file something other than urllib.py Commented Apr 4, 2013 at 20:54
  • By default Python searches the current directory first for modules to import. Commented Apr 4, 2013 at 20:54

3 Answers 3

16

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

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

4 Comments

+1 for submitting an answer instead of leaving a drive-by comment
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 <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 Pythong Launcher (3.3)?
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?
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 stackoverflow.com/questions/514371/… Thanks for you help Pavel!
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

1 Comment

See stackoverflow.com/questions/17846908/… for how to set a good shebang line.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.