where is the 'itertools' file - python

import itertools
print itertools#ok
the code is ok
but i can't find the itertools file.
who can tell me where is the 'itertools file'
my code is run python2.5
import itertools
print itertools.__file__
Traceback (most recent call last):
File "D:\zjm_code\mysite\zjmbooks\a.py", line 5, in <module>
print itertools.__file__
AttributeError: 'module' object has no attribute '__file__'

>>> import itertools
>>> itertools.__file__
'/usr/lib64/python2.6/lib-dynload/itertools.so'
The fact that the filename ends with .so means it's an extension module written in C (rather than a normal Python module). If you want to take a look at the source code, download the Python source and look into Modules/itertoolsmodule.c (you can also view it in your browser at http://svn.python.org/view/python/trunk/Modules/itertoolsmodule.c?view=log).
Edit (as an answer to the comment below): it also works with Python 2.5:
Python 2.5.2 (r252:60911, Oct 5 2008, 19:29:17)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import itertools
>>> itertools.__file__
'/usr/lib/python2.5/lib-dynload/itertools.so'

If you're looking for the source file (in C, of course), it's for example online here.

For the windows users (I'm running Python 2.7.2, Win7x64, default installer package) the call to __file__ will flame out as #zjm1126 has noted, I suspect the problem being that itertools is a builtin on the windows package. If you'd picked say, exceptions? You'd get the same behaviour on another platform (e.g. Python 2.6.1 on my macbook) - Windows just happens to have a few more builtins like itertools.
It's not strictly an answer as such, but you could parse sys.modules which would give you a hint as to where it's coming from:
>>> import sys
>>> sys.modules['itertools']
<module 'itertools' (built-in)>
which points at itertools being built-in to your python executable.
Also, the imp.find_module response is providing the same information: the weird return tuple is by spec (see: http://docs.python.org/2/library/imp.html#imp.find_module) and telling you that the module is of type 6, which is the enumeration for imp.C_BUILTIN

try this
>>> import imp
>>> imp.find_module("itertools")
update:
since yours is None, another go through a manual way. Do a sys.path
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6/lib-dynload' ]
then depending on your system, use your system's search facility to find it. on my linux system
$ find /usr/lib/python2.6/lib-dynload -type f -iname "*itertools*"
/usr/lib/python2.6/lib-dynload/itertoolsmodule.so
OR, just search the entire system for the file with name "itertools".

Related

Why is my script's directory not in the Python sys.path?

Python 3.6.5
I am aware of this one: Why does my python not add current working directory to the path?
But the problem there is that he's doing something more complicated (referring to a sub-folder but executing from a main folder). The answers there are to either simplify things or to add package definitions.
And the selected answer even says: "It is the script's directory that is added"
However, my problem is really more simple: My script's directory ISN'T added.
Basically, all the tutorials on the internet say: import mymodule
When I do that, I get a name error...
My folder structure:
C:/Projects/interner
interner.py # this is the main program body
aux.py # this is the auxiliary file I would like to import into the above
I've tried both coding 'import aux' inside interner.py, and also using the interactive console:
cd c:/Projects/interner
python
import aux
To no avail (ModuleNotFoundError: No module named 'aux')
My sys.path:
['C:\\Tools\\Python\\python365\\python36.zip', 'C:\\Tools\\Python\\python365']
(both from inside the script and from interactive console)
Could you please tell me why I can't import local scripts? Is it because my sys.path is missing the PWD? If so, why is it missing it?
Edit: Doing this to help investigation:
>>> import os; print(os.listdir("."))
['aux.py', 'hw.py', 'interner.py', 'my_funcs.py']
I believe this is a Python bug, specific to the embeddable (ZIP file without an installer) Windows distribution. I’ve filed https://bugs.python.org/issue34841.
Removing the python37._pth file (presumably python36._pth in your case) from the distribution fixed it for me.
I don't know why but it seems that "" is missing from your sys.path variable, and that prevents from importing modules from current directory all right!
I can somehow reproduce your issue (eatcpu.py is in my current dir):
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', 'C:\\Windows\\system32\\python27.zip', 'D:\\AppX64\\Python27\\DLLs', ... etc...]
>>> import eatcpu
works. Now in another python session:
$ python.exe
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.remove("")
>>> import eatcpu
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named eatcpu
>>>
So the quickfix for you is to do:
import sys
sys.path.append("")
It looks like you are using the embeddable distribution of CPython rather than one of the regular installers. As described on the documentation page:
The embedded distribution is a ZIP file containing a minimal Python environment. It is intended for acting as part of another application, rather than being directly accessed by end-users.
Since you seem to be directly accessing Python rather than embedding it, you should consider using the regular (or Microsoft Store) installer (also described on the page I linked above).
Try making it explicit:
from . import aux

What I cannot import a verified code in the current directory?

So I have the following script, test.py:
>cat test.py
def foo(x):
y=x*2
return y
print foo(100)
x = "aaa100"
print x
I can run it well:
>python test.py
200
aaa100
But inside the interactive interpreter, I cannot import it:
>python
Python 2.6.6 (r266:84292, Aug 18 2016, 15:13:37)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-17)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 6, in <module>
x + 10
TypeError: cannot concatenate 'str' and 'int' objects
Do you see what is wrong?
There is already a test module (https://docs.python.org/3/library/test.html or https://docs.python.org/2/library/test.html), which is loaded before your local test.py on the PYTHONPATH because you used import.
In the interpreter, you can see where it is
> import test
> test
<module 'test' from 'C:\Python27\lib\test\__init__.pyc'>
Yours may not be the test module. It tends to successfully import. You must have another test.py on your PYTHONPATH.
Your command line does not have an import, so it is free to use the local filename when you pass it directly to the python executable as an argument.
A simple way to avoid the name conflict, name your file "test_foo.py" and import test_foo. (unless you use test_foo.py in multiple places)
Additionally, consider a run function, in combination with
if __name__ == __main__:
because right now it will execute those prints as part of the import. You will want to do that also in ... whatever other "test.py" you have.
It's either these two cases:
1) Python built-in module "test" (https://docs.python.org/2.7/library/test.html) is being loaded and not your local test.py, however, you shouldn't have any problem importing your local test.py since its a default module, its strange that you are getting an error when you try importing it (unless you have modified it). You can try this to see the imported module cache and check for the test module path
import sys
print sys.modules
If this is the case then you simply have to open a new python interpreter to make sure no test module has been cached. If its still cached, then change the name of your test.py to something_test.py
2) The code you are representing above is not complete and you have a line "x + 10" and since you are assigning x as a string and trying to add integer, your import isn't working. I doubt this being true as I am sure if this was the case you would be aware of it :-)

Python Snakefood module import generalized failure

I want to run snakefood (an AST-based dependency graph analyser; source code can be found here). My project has a structureinvolving several levels of Python packages, like this:
myproject
|code
|Utils
|AdaptedConfigParser
Configs_Parser.py
...
...
main.py
However, when I start running snakefood on the root directory of my project it claims that it can't find the modules from my Python package being imported:
$ sfood --internal --follow --ignore-unused ./PycharmProjects/myproject/ > ~/static_analysis.txt
WARNING : Line 9: Could not import module 'myproject.Utils.AdaptedConfigParser.Configs_parser'
I tried to get around it by adding a .pth file with the project root to the lib/python2.7/site-packages
Now when I call python with that virtual environment activated from anywhere, I can do the following:
$ python
Python 2.7.6rc1 (default, Jan 19 2014, 18:57:40)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import myproject.Utils.AdaptedConfigParser.Configs_parser
>>>
And it works just fine.
However, when snakefood is launched with that virtual environement, it still returns the same error.
$ sfood --internal --follow --ignore-unused ./PycharmProjects/myproject/ > ~/static_analysis.txt
WARNING : Line 9: Could not import module 'myproject.Utils.AdaptedConfigParser.Configs_parser'
At this point I don't even understand where the problem with imports could come from.
In addition to that, when --internal flag is taken away, it fails imports even of the python builtin module, which doesn't make any sense to me:
WARNING : Line 80: Could not import module 'builtins'
WARNING : Line 190: Could not import module 'pyamg'
Have anyone encountered such a problem previously? If yes, is there a way of getting around it?
I had similar issues, turns out the warning in my case was generated because of __all__ declarations.
e.g.
__all__ = ['abc',
'aaa',
...
]
Above code gives out the warnings:
WARNING : Line xx: Could not import module 'abc'
WARNING : Line xx: Could not import module 'aaa'
I modified the warning constant ERROR_IMPORT within snakefood\lib\python\find.py to add the name of the file where the error occurs along with the line number and module name. That way you can target the specific file and line number pretty much figure out the problem.
Hope this helps!

No module Named Subprocess

I am writing a program in python and it came back with this error. I have tried the solutions from other questions but none of them work for me hence why am asking again. I am using python on the Raspberry Pi running Rasbian.
Python 2.7.3 (default, Mar 18 2014, 05:13:23)
[GCC 4.6.3] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Traceback (most recent call last):
File "/home/pi/Desktop/Docs/rc/serial transmit.py", line 2, in <module>
import Subprocess
ImportError: No module named Subprocess
>>>
Thanks in advance to anyone who can get me subprocess back :)
subprocess is not capitalized. Try
import subprocess
It's important to use proper case for identifiers in python. Subprocess and subprocess are two different modules from python's point of view, and only second one is a part of standard library. Variables are also case sensitive in Python. And since you're using raspberry pi I would add that case sensitivity also applies to Unix filesystems. So, that should work:
import subprocess

Python: "import posix" question

If I import os module, I can run the following to infer the location of os.py
>>> import os
>>> print os.__file__
/usr/lib/python2.6/os.pyc
However, when I import posix, it does not have the __file__ attribute. Is it because it is implemented as a part of the python runtime, not as a standard library?
How can I find out more information like this using solely the python official documentation?
It's a C module. It can be either built into the Python binary or compiled as a shared library. In your case it is compiled in
The official docs say not to import it directly, and you should use the functionality provided via os
Run python interactively.
>>> import posix
>>> help(posix)
There's a lot of good stuff there.
FILE
(built-in)
You can also use the 'inspect' module to find information (say source file path etc) about a python module. For example:
import inspect
import os
inspect.getsourcefile(os)
'/usr/local/lib/python2.7/os.py'
inspect.getsourcefile(inspect)
'/usr/local/lib/python2.7/inspect.py'
import sys
inspect.getsourcefile(sys)
Traceback (most recent call last):
[...]
raise TypeError('{!r} is a built-in module'.format(object))
TypeError: <module 'sys' (built-in)> is a built-in module

Categories