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)?
You named your file urllib, it is shadowing the standard library package. Rename your file.
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
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.
Related
I'm trying to use the xls2xlsx module to convert several .xls files to .xlsx format, but I get the following error message:
ModuleNotFoundError: No module named 'currency-symbols'
The code:
import os
from xls2xlsx import XLS2XLSX
path = r'./ammcfiles'
p = os.listdir(path)
for f in p:
if f.endswith('.xlsx'):
x2x = XLS2XLSX(f)
x2x.to_xlsx(f)
I tried pip installing the module, but it didn't solve the problem.
My Python version is 3.10.4.
Note: More of the stacktrace would have been helpful to see the full issue.
Had a similar issue with a script that was developed and previously run with Python 3.6.
Running the script in Python 3.10 returned the following error:
Traceback (most recent call last):
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py", line 37, in
import currency_symbols.constants as currency_symbols_constant
ModuleNotFoundError: No module named 'currency_symbols.constants'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/script/./script.py", line 20, in
from xls2xlsx import XLS2XLSX
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/init.py", line 3, in
from .htmlxls2xlsx import HTMLXLS2XLSX
File "<virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py", line 40, in
currency_symbols_constants = importlib.import_module('currency-symbols.constants')
File "/usr/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'currency-symbols'
Investigation
Ensure the packages are installed
python -m pip install xls2xlsx currency-symbols
Lines 36-40 of <virtual env>/lib/python3.10/site-packages/xls2xlsx/htmlxls2xlsx.py
try:
import currency_symbols.constants as currency_symbols_constants
except Exception:
import importlib
currency_symbols_constants = importlib.import_module('currency-symbols.constants')
This code seems to be responsible for loading the currency-symbols module regardless of the Python version and by extension the module version.
Therefore, the original error was received because both import attempts failed.
<virtual env>/lib/python3.10/site-packages/currency_symbols/ contains the file
_constants.py and not constants.py.
Note the folder is currency_symbols and not currency-symbols, underscore (_) vs dash (-). Therefore, htmlxls2xlsx.py is using the new currency-symbols module name currency_symbols but not the new name of the constants sub module _constants
Fix
Edit htmlxls2xlsx.py to use _constants.py
try:
import currency_symbols._constants as currency_symbols_constants
This fixes the import and usage issues.
As Yuli L mentioned, inside the htmlxls2xlsx.py which is in xls2xlsx module directory I found next:
import currency_symbols.constants as currency_symbols_constants
And change by (take care with typos):
import currency_symbols._constants as currency_symbols_constants
I installed django python module on my machine, and used it like this
a.py:
import django.core
...
then, I created an new file django.py in the same folder of file a.py, and re-run a.py, it throw import error, since it just imported my local django.py
File "a.py", line 1, in <module>
import django.core
ImportError: No module named core
So, how to distinguish them when importing python module?
You can use the imp module to import your script directly from a local path:
mymodule = imp.load_source('mymodule', 'django.py')
You can then use mymodule as if you had imported it normally.
Use caution, however; the python import internals must be subverted responsibly.
You could temporarily remove the current directory from PYTHONPATH:
$ python -c'import numpy.core' # works
$ touch numpy.py # add conflicting module
$ python -c'import numpy.core' # it fails now
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, 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 "<string>", line 1, in <module>
ImportError: No module named 'numpy.core'; 'numpy' is not a package
$ python -c'import sys; sys.path.remove(""); import numpy.core' # works again
Needless to say, you should use such hacks sparingly if at all -- avoid manipulating sys.path manually. Rename your local module, to avoid the conflict instead (move it inside a package at least e.g.: your_package/numpy.py).
I am following the example from the python online document(21.12.3) for practice. When I try to run my script with Run Module(F5), I always get an import error. But if I type them directly into IDLE command line, python does not complain. I am not sure what am I doing wrong.
The python version I am using is
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:38:22) [MSC v.1600 32 bit (Intel)] on win32
The script is
import http.client
conn = http.client.HTTPConnection("192.168.1.2", 8080)
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print(r1.status, r1.reason)
conn.close()
The error message is
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 2218, 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 "D:\User\Downloads\http.py", line 1, in <module>
import http.client
File "D:\User\Downloads\http.py", line 1, in <module>
import http.client
ImportError: No module named 'http.client'; 'http' is not a package
You probably have created a Python script and named it http.py in local directory. This conflicts with Python 3's builtin module with the same name and leads to this error. Quick solution is to rename your file to something less generic to avoid conflict with Python builtin modules.
But if you insist, you can clear the name ambiguity by fully qualifying the local python module name using absolute imports:
from . import http
or less confusingly:
from . import http as myhttp
or
from .http import something
On Python 2, it is necessary to enable the absolute import feature at the very top of the importing module using a future statement before using this feature:
from __future__ import absolute_import
I had the same problem. In my case, there was another file named http.py in the same folder. I just renamed it, problem solved.
I installed the stem module and did some copy/paste from the tutorials on their official site. None of them worked here.
In fact it doesn't even work when I type "from stem.control import Controller" in the command line. That gets me the following "error code":
>>> from stem.control import Controller
Traceback (most recent call last):
File "<frozen importlib._bootstrap>", line 1521, 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 "<pyshell#0>", line 1, in <module>
from stem.control import Controller
File "C:\Python33\_PROJECTS\stem.py", line 6, in <module>
import build.lib.stem.process
File "C:\Python33\lib\build\lib\stem\__init__.py", line 421, in <module>
import stem.util.enum
ImportError: No module named 'stem.util'; stem is not a package
I'm using Python 3 and stem is supposed to work with it. Am I missing out something super obvious here?
From your traceback, what is this file?
File "C:\Python33\_PROJECTS\stem.py"
You are probably attempting to import this file, instead of the actual package (The last line of the traceback reveals so much).
Be careful when naming scripts identical to package name: the current working directory is added to the front of sys.path, and thus, such a script may get imported instead of the actual package. I assume you actually tried the imports from the C:\Python33\_PROJECTS\ directory.
I am new to Python 3 and still learning but I need help. The first part of the script is:
import mysql.connector #this is failing as a .py but works in the shell
cnx = mysql.connector.connect(user='root', password='mypassword', host='my_ip_address', database'name_of_database') #this works in the shell
cursor = cnx.cursor()
I have tried the above line by line in the Python shell which works fine: I can import the connector to connect to my database and fetch back data. But when I save the script as a .py the import mysql connector does not work. I do have the Path variable setup and when I installed the mySQL connector it placed the relevant files in my Python install path \Lib\site-pacakges folder.
I get the following error:
>Traceback (most recent call last):
> File "<frozen importlib._bootstrap>", line 1518, 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 "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
> File "C:\Users\Paul\Desktop\scripts\mysql.py", line 2, in <module>
> import mysql.connector
>ImportError: No module named 'mysql.connector'; mysql is not a package
I have the same question as your, and I found an answer in another website where you asked the same question. I'm pasting the answer here as reference.
The above happens because your script is named 'mysql.py', which is
actually the same name as the 'mysql' package. Python correctly
reports ImportError. Simply rename your 'mysql.py' to something else,
like 'mysqlhacks.py'.
Taken from https://answers.launchpad.net/myconnpy/+question/226992