Distinguish local module with installed module - python

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

Related

XLS2XXLSX: No module named 'currency-symbols'

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

Flask not importing in virtualenv [duplicate]

I have a script named requests.py that needs to use the third-party requests package. The script either can't import the package, or can't access its functionality.
Why isn't this working, and how do I fix it?
Trying a plain import and then using the functionality results in an AttributeError:
import requests
res = requests.get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "/Users/me/dev/rough/requests.py", line 1, in <module>
import requests
File "/Users/me/dev/rough/requests.py", line 3, in <module>
requests.get('http://www.google.ca')
AttributeError: module 'requests' has no attribute 'get'
In more recent versions of Python, the error message instead reads AttributeError: partially initialized module 'requests' has no attribute 'get' (most likely due to a circular import).
Using from-import of a specific name results in an ImportError:
from requests import get
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import get
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests import get
ImportError: cannot import name 'get'
In more recent versions of Python, the error message instead reads ImportError: cannot import name 'get' from partially initialized module 'requests' (most likely due to a circular import) (/Users/me/dev/rough/requests.py).
Using from-import for a module inside the package results in a different ImportError:
from requests.auth import AuthBase
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests.auth import AuthBase
File "/Users/me/dev/rough/requests.py", line 1, in <module>
from requests.auth import AuthBase
ImportError: No module named 'requests.auth'; 'requests' is not a package
Using a star-import and then using the functionality raises a NameError:
from requests import *
res = get('http://www.google.ca')
print(res)
Traceback (most recent call last):
File "requests.py", line 1, in <module>
from requests import *
File "/Users/me/dev/rough/requests.py", line 3, in <module>
res = get('http://www.google.ca')
NameError: name 'get' is not defined
This happens because your local module named requests.py shadows the installed requests module you are trying to use. The current directory is prepended to sys.path, so the local name takes precedence over the installed name.
An extra debugging tip when this comes up is to look at the Traceback carefully, and realize that the name of your script in question is matching the module you are trying to import:
Notice the name you used in your script:
File "/Users/me/dev/rough/requests.py", line 1, in <module>
The module you are trying to import: requests
Rename your module to something else to avoid the name collision.
Python may generate a requests.pyc file next to your requests.py file (in the __pycache__ directory in Python 3). Remove that as well after your rename, as the interpreter will still reference that file, re-producing the error. However, the pyc file in __pycache__ should not affect your code if the py file has been removed.
In the example, renaming the file to my_requests.py, removing requests.pyc, and running again successfully prints <Response [200]>.
The error occurs because a user-created script has a name-clash with a library filename. Note, however, that the problem can be caused indirectly. It might take a little detective work to figure out which file is causing the problem.
For example: suppose that you have a script mydecimal.py that includes import decimal, intending to use the standard library decimal library for accurate floating-point calculations with decimal numbers. That doesn't cause a problem, because there is no standard library mydecimal. However, it so happens that decimal imports numbers (another standard library module) for internal use, so a script called numbers.py in your project would cause the problem.
In one especially pernicious case, having a file named token.py in a project (or the current working directory, when starting up Python in interactive mode) causes the interactive help to break:
$ touch token.py
$ python
Python 3.8.10 (default, Nov 14 2022, 12:59:47)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> help
Type help() for interactive help, or help(object) for help about object.
>>> help()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.8/_sitebuiltins.py", line 102, in __call__
import pydoc
File "/usr/lib/python3.8/pydoc.py", line 66, in <module>
import inspect
File "/usr/lib/python3.8/inspect.py", line 40, in <module>
import linecache
File "/usr/lib/python3.8/linecache.py", line 11, in <module>
import tokenize
File "/usr/lib/python3.8/tokenize.py", line 35, in <module>
from token import EXACT_TOKEN_TYPES
ImportError: cannot import name 'EXACT_TOKEN_TYPES' from 'token' (/current/working/directory/token.py)
The traceback tells us all we need to know: calling help triggers a deferred import of the standard library pydoc, which indirectly attempts to import the standard library token, but finds our token.py which doesn't contain the appropriate name. In older versions of Python, it was even worse: tokenize would do a star-import from token, and then its top-level code would try to use a name defined there, resulting in NameError - and a stack trace not mentioning the file name token.py.
If you still encounter problems like this after tracking own and renaming or removing the appropriate .py files in your project, also check for .pyc files that Python uses to cache bytecode compilation when importing modules. In 3.x, these will be stored in folders with the special name __pycache__; it is safe to delete such folders and files, and possible to suppress them (but you normally won't want to).

What are the reserved Python module/package names?

I got a strange error while working with Python unittest. I have two folders in my project:
project
code
__init__.py (empty)
app.py (defines my App class)
test
test.py (contains my unit tests)
test.py is:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from code.app import App
class test_func1(unittest.TestCase):
...
When I run test.py I get the message:
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 "...test.py, line 5, in <module>
from code.app import App
ImportError: No module named 'code.app': 'code' is not a package
After verifying that __init__.py was present and banging my head for a while, on a whim I changed the name of the app directory from code to prog:
import os, sys, unittest
sys.path.insert(1, os.path.join(sys.path[0],'..'))
from prog.app import App
... and everything was suddenly fine. Unittest imported my app properly and ran the tests.
I've searched through https://docs.python.org/3.5/reference/lexical_analysis.html#keywords and https://docs.python.org/3/reference/import.html#path-entry-finders and don't see any indication that code is an illegal directory name. Where would this be documented, and what other directory names are reserved?
System: python 3.4.3 [MSC v1600 32 bit] on win32, Windows 7
code isn't reserved, but it is already defined in the standard library, where is it a regular module and not package. To import from your package, you should use a relative import.
from .code.app import App

Can't get the "stem" module to work

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.

How do I install a Python extension module using distutils?

I'm working on a Python package named "lehmer" that includes a bunch of extension modules written in C. Currently, I have a single extension module, "rng". I am using Python's Distutils to build and install the module. I can compile and install the module, but when I try to import the module using import lehmer.rng or from lehmer import rng, the Python interpreter throws an ImportError exception. I can import "lehmer" fine.
Here are the contents of my setup.py file:
from distutils.core import setup, Extension
exts = [Extension("rng", ["lehmer/rng.c"])]
setup(name="lehmer",
version="0.1",
description="A Lehmer random number generator",
author="Steve Park, Dave Geyer, and Michael Dippery",
maintainer="Michael Dippery",
maintainer_email="mpd#cs.wm.edu",
packages=["lehmer"],
ext_package="lehmer",
ext_modules=exts)
When I list the contents of Python's site-packages directory, I see the following:
th107c-4 lehmer $ ls /scratch/usr/lib64/python2.5/site-packages/lehmer
__init__.py __init__.pyc rng.so*
My PYTHONPATH environment variable is set correctly, so that's not the problem (and as noted before, I can import lehmer just fine, so I know that PYTHONPATH is not the issue). Python uses the following search paths (as reported by sys.path):
['', '/scratch/usr/lib64/python2.5/site-packages', '/usr/lib/python25.zip', '/usr/lib64/python2.5', '/usr/lib64/python2.5/plat-linux2', '/usr/lib64/python2.5/lib-tk', '/usr/lib64/python2.5/lib-dynload', '/usr/lib64/python2.5/site-packages', '/usr/lib64/python2.5/site-packages/Numeric', '/usr/lib64/python2.5/site-packages/PIL', '/usr/lib64/python2.5/site-packages/SaX', '/usr/lib64/python2.5/site-packages/gtk-2.0', '/usr/lib64/python2.5/site-packages/wx-2.8-gtk2-unicode', '/usr/local/lib64/python2.5/site-packages']
Update
It works when used on an OpenSUSE 10 box, but the C extensions still fail to load when tested on Mac OS X. Here are the results from the Python interpreter:
>>> sys.path
['', '/usr/local/lib/python2.5/site-packages', '/opt/local/lib/python25.zip', '/opt/local/lib/python2.5', '/opt/local/lib/python2.5/plat-darwin', '/opt/local/lib/python2.5/plat-mac', '/opt/local/lib/python2.5/plat-mac/lib-scriptpackages', '/opt/local/lib/python2.5/lib-tk', '/opt/local/lib/python2.5/lib-dynload', '/opt/local/lib/python2.5/site-packages']
>>> from lehmer import rng
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name rng
>>> import lehmer.rngs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rngs
>>> import lehmer.rng
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named rng
>>> from lehmer import rngs
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name rngs
For the record (and because I am tired of seeing this marked as unanswered), here were the problems:
Since the current directory is automatically added to the Python packages path, the interpreter was first looking in the current directory for packages; since some C modules were not compiled in the current directory, the interpreter couldn't find them. Solution: Don't launch the interpreter from the same directory in which your working copy of the code is stored.
Distutils did not install the module with the correct permissions on OS X. Solution: Fix the permissions.

Categories