Here's the directory structure
.../Desktop/scratch/abc/greet.py
I have my import code module.py in abc
.../Desktop/scratch/module.py:
import sys
sys.path.append("C:\\Users\\Name\\Desktop\\scratch")
import abc.greet
I have created empty init file in abc and greet folders.
But when i run this code then error be like:
Traceback (most recent call last): File "<frozen importlib._bootstrap>", line 1519, 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 "module.py", line 4, in <module>
import abc.greet ImportError: No module named 'abc.greet'; abc is not a package
I don't understand why this happens? How is abc not a module when I have created the init file and also added the scratch directory to the search path.
PS: greet.py just contains a method hello which prints "hello world", if this info is of any use.
Python already has a module named abc. Choose a different name.
Related
I'm trying to get module imports to work in embeddable python, but it doesn't want to work
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\script.py
from module_test import test
print("Hello world!")
print(test())
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>type run_scripts\module_test.py
def test():
return "Test!"
C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32>#python.exe run_scripts\script.py
Traceback (most recent call last):
File "C:\Users\test\Desktop\winpy\python-3.10.10-embed-win32\run_scripts\script.py", line 1, in <module>
from module_test import test
ModuleNotFoundError: No module named 'module_test'
Why is the module not being imported? I tried changing PYTHONPATH but it didn't help
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
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 write a code in python language which uses copy module. when i run this code in pycharm console it has no error but in pycharm GUI environment it gives me this error:
Traceback (most recent call last):
File "C:/....../python/deepshallowcopy.py", line 2, in <module>
from copy mport deepcopy
File "C:\Python34\lib\copy.py", line 114, in <module>
types.BuiltinFunctionType, type(Ellipsis),
AttributeError: 'module' object has no attribute 'BuiltinFunctionType'
My code is:
from copy import deepcopy
col3=["rrrr","bbbb"]
col4=deepcopy(col3)
print(col3,col4)
col3[0]="jfjdhf"
print(col3,col4)
Taking a closer look at your traceback,
Traceback (most recent call last):
File "C:/....../python/deepshallowcopy.py", line 2, in <module>
from copy import deepcopy
File "C:\Python34\lib\copy.py", line 114, in <module>
types.BuiltinFunctionType, type(Ellipsis),
AttributeError: 'module' object has no attribute 'BuiltinFunctionType'
you must have a Python file named types.py in the same folder that you are running your deepshallowcopy.py file.
I was able to reproduce this error by running your script in the same folder as a file named types.py.
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.