When I import a module, it works on linux, but fails in windows, with the error:
<Directory>\src>main.py
Traceback (most recent call last):
File "<Directory>\src\main.py", line 12, in <module>
from parser.parser import Parser
ImportError: No module named parser
On windows it seems that it cannot find the file parser.py (created by me). I don't understand why because it found all the other modules.
[folder:
- main.py
- parser/__init__.py
- parser/parser.py]
The problem is with the package name parser . By importing from parser you import the parser module from the standard library that has no parser.parser submodule. See parser.
I encountered the same problem, and got it solved.
Here's how.
Check your sys.path (import it first) to see whether your coding-file's directory is included in it. If not, append it.
That's my code:
import sys
import os
sys.path[0]=os.path.dirname(os.path.realpath(__file__))
Tell me if it doesn't work.
BTW, I'm Chinese so ignore any grammatical errors. I think you understand what I mean.
Related
My colleagues shared some programs and modules with me. I put them in ...../anaconda3/lib/python3.7/site-packages/pdbParser. Below show the content.
I set this directory in $PATH. However, when I run my program at another directory (let's say user directory), I received below error message.
File "run.py", line 20, in <module>
from pdbParser.pdbParser import pdbParser
File "/<my system dir>/anaconda3/lib/python3.7/site-packages/pdbParser/__init__.py", line 14, in <module>
from __pkginfo__ import __version__, __author__
ModuleNotFoundError: No module named '__pkginfo__'
Actually, my program (run.py) can call the module (pdbParser.py) in "//anaconda3/lib/python3.7/site-packages/pdbParser/" but the module (pkginfo.py) called by pdbParser.py cannot be found. I do not understand why it happened. I read related questions (1 and 2) in this community, but I could not get the issue fixed. Is there anything wrong on my end? Any further help and suggestion would be highly appreciated.
Can you fix the error syntax in the __init__.py file like this?
append .
from
from __pkginfo__ import __version__, __author__
to
from .__pkginfo__ import __version__, __author__
Am trying to run a python script that i downloaded from the internet
import os, subprocess, sys, socket, time, struct, random, xml.sax, getopt
import shutil
import Output
import numpy as np
...
I get the error
Traceback (most recent call last):
File "downtown.py", line 20, in <module>
import Output
ImportError: No module named Output
Am totally new to python , and I want to know whether the missing import is python's or a user library
As a first hint, observe the missing module Output starts with a capital O, which fails to follow convention of using lowercase-only for module names. Therefore Output is most certainly a user library. Alternatively, Output might be a class that would correctly need to be imported via from somelib import Output.
It is not a standard module - it looks like a user module that you should also download from wherever you got the original script.
I need to make a portable app to put on a device and I want to put only the modules needed so there will not be all the standard modules so I need to see what my app needs, but if that module misses some imports I cannot see that, because it gives an error without being explicit what fails in that module:
Traceback (most recent call last):
File "./packaging.py", line 30, in <module>
import simplejson
ImportError: No module named simplejson
Is there a way to see what import exactly fails in that module?
The error means, that the import import simplejson fails because there is “[n]o module named simplejson”. The solution is to simply install said module.
It could be that the string /usr/lib/python2.7/dist-packages is not in your sys.path folder list so it's not being searched when attempts are made to import modules or packages.
you can catch the exceptions, to know what went wrong and handle them appropriately:
try:
import simplejson
except ImportError:
print "simplejson module not found"
#or do something else here, may be install that module
Here is a snippets from my python shell, i can't understand what is wrong there, if somebody has any suggestion i would be glad to hear.
>>> import pydbg
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\pydbg\__init__.py", line 47, in <module>
from pydbg import *
File "C:\Python27\lib\pydbg\pydbg.py", line 32, in <module>
import pydasm
ImportError: DLL load failed: The specified module could not be found.
>>> import pydasm
>>>
how come that i can't import pydbg since there it can't import pydasm, and i can import pydasm directly ?
I was able to fix this by deleting pydasm.pyd in the site-packages directory for pydbg.
Move your pydbg directory from C:\Python27\lib\ to C:\Python27\lib\site-packages\, the standard installation location for 3rd party packages. Alternatively, try the unofficial pydbg binaries from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pydbg.
Find pydasm for the python version you are running. I think that pydbg comes with pydasm for python2.6. I don't know why.
I found pydasm for python 2.7 on the Internet. You can also find information about how to convert it yourself. I am not getting any errors but I still struggle with pydbg so I am not sure if this is the solution.
If you are running python2.7 try this: http://blog.csdn.net/qq_lhz/article/details/6922130
Maybe you can try my precompiled binaries of PyDasm for Python 2.7:
https://breakingcode.wordpress.com/2012/04/08/quickpost-installer-for-beaenginepython/
I'm a Python newbie, so bear with me :)
I created a file called test.py with the contents as follows:
test.py
import sys
print sys.platform
print 2 ** 100
I then ran import test.py file in the interpreter to follow an example in my book.
When I do this, I get the output with the import error on the end.
win32
1267650600228229401496703205376
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named py
Why do I get this error and how do I fix it? Thanks!
Instead of:
import test.py
simply write:
import test
This assumes test.py is in the same directory as the file that imports it.
This strange-looking error is a result of how Python imports modules.
Python sees:
import test.py
Python thinks (simplified a bit):
import module test.
search for a test.py in the module search paths
execute test.py (where you get your output)
import 'test' as name into current namespace
import test.py
search for file test/py.py
throw ImportError (no module named 'py') found.
Because python allows dotted module names, it just thinks you have a submodule named py within the test module, and tried to find that. It has no idea you're attempting to import a file.
You don't specify the extension when importing. Just do:
import test
As others have mentioned, you don't need to put the file extension in your import statement. Recommended reading is the Modules section of the Python Tutorial.
For a little more background into the error, the interpreter thinks you're trying to import a module named py from inside the test package, since the dot indicates encapsulation. Because no such module exists (and test isn't even a package!), it raises that error.
As indicated in the more in-depth documentation on the import statement it still executes all the statements in the test module before attempting to import the py module, which is why you get the values printed out.