Is there a way to use compiled C modules with zipimport? - python

When I have a python shared object file (.so) in my sys.path, I can simply do :
import _ctypes
And it will import python2.7/lib-dynload/_ctypes.so.
However, if I use a zipfile called tmp.zip that contains :
Hello/_World.so
with world.so containing a well formatted init_World function, then :
Python 2.7.10 (default, Jun 1 2015, 18:05:38)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.insert(0, 'tmp.zip')
>>> import _World
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zipimport.ZipImportError: can't find module _World
I read it was impossible to load shared object files outside a filesystem in C.
Does this mean what I’m trying to achieve is impossible and that _World.soshould be extracted from the archive ?
I’m only interested about doing it directly with thezipimport. I know there are other ways of doing it like extracting the archive manually and create files.

It seems I have definitely huge problems at reading texts entirely :
This module adds the ability to import Python modules (*.py, *.py[co]) and packages from ZIP-format archives..
So the answer is clear, I’m trying to achieve something impossible… There’s no way to do escape the python sandbox with that since it can’t be done even with a normal environment.

Related

Python import working on host but not inside a VM

Pythonproject directory structure is like
--test
--upperlevel
-- __init__.py
-- manager.py
-- UpperLevel.py
this files in turn contains
# __init__.py
msg = "YAYY printing !!!"
print msg
# UpperLevel.py
from upperlevel import msg
# manager.py
import UpperLevel
So in my local MAC book with python 2.7.10, started a python shell in test directory.
From that shell,
Python 2.7.10 (default, Jul 30 2016, 19:40:32)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
>>>
it worked !!!!
However i started a virtual machine (ubuntu 14.04 and python 2.7.10) with vagrant and added same test directory to it.
so if i did the same thing
Python 2.7.10 (default, Jul 13 2017, 19:26:24)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import upperlevel.manager
YAYY printing !!!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "upperlevel/manager.py", line 1, in <module>
import UpperLevel
File "upperlevel/UpperLevel.py", line 1, in <module>
from upperlevel import msg
File "upperlevel/upperlevel.py", line 1, in <module>
from upperlevel import msg
ImportError: cannot import name msg
>>>
So my questions are
1) why it is not working in the later case, i tried the same in docker and getting the same error
2) there is no such file in my project, File "upperlevel/upperlevel.py", line 1, in
3) why it is searching for upperlevel.py instead of UpperLevel.py
FYI
It looks like if we do "import upperlevel" from UpperLevel.py it is refering back to itself instead of going to upperlevel/init.py.
UPDATE:
I understood where the problem is from.... my test directory(volume) is being shared between mac and vagrant/docker, somehow UpperLevel.pyc is being treated as upperlevel.pyc in that shared volume.
Instead of running in a shared directory i created same folders/files in /home/vagrant and it worked.
It seems you are running from a Mac environment, and it is possible that the Python default search paths are different for those builds, despite the version being similar.
Try comparing:
import sys
print(sys.path)
It is probable that the default installation search paths might differ.
You can use the environment variable $PYTHONPATH to add additional import paths, while I don't really like this method it can be sufficient in most cases.
You can also setup your package in a proper module installation path.
Finally answering my own question...the problem is mac has a case insensitive file system and when it is mounted on linux, python is trying to use ubuntu mode of module reading like in the case sensitive way on a case insensitive File system.
After a lot of research found this link for docker https://github.com/docker/for-mac/issues/320 so those when using ubuntu docker with python on a mac be careful with your naming conventions.

Python turtle graphics malfunction on a Mac running OSX 10.6

This is the result when I attempt to run Python turtle graphics on my Mac.
Has anyone else seen this. Are there suggestions for fixing this problem. Thanks
in advance!
$ python
Python 2.6.1 (r261:67515, Aug 2 2010, 20:10:18)
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import turtle
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/turtle.py", line 114, in <module>
from copy import deepcopy
File "/Users/morrison/copy.py", line 3, in <module>
Interface summary:
IndexError: list index out of range
>>>
Is your current directory /Users/morrison/ when you run this?
If so, the problem is that the interpreter's current working directory is being used for looking up python modules at runtime in addition to the standard locations. You have a file copy.py in this directory and it is being imported when the standard library copy module is what was intended by the turtle module.
This happens because when you run the python interpreter interactively, it will add the current working directory to the front of sys.path automatically. (There's a likewise effect if you were trying to run a script in this dir from a different directory - the script's directory would be added to the front of sys.path.)
The easiest way around this is to just rename copy.py to mycopy.py (and don't forget to remove copy.pyc in that dir).

problems importing python module

I'm trying to use python's bitstring module in a script and am getting an import error. This error does not happen when running from interactive mode.
Here's the code:
import bitstring
b = bitstring.BitArray(bin='001001111')
When run like this:
python test.py
I get this:
AttributeError: 'module' object has no attribute 'BitArray'
However, when I do this:
$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import bitstring
>>> b = bitstring.BitArray(bin='001001111')
>>> print b
0b001001111
It works just fine! It's the same interpreter being run by the same user. Any pointers?
I predict you have created a bitstring.py in your current directory.
The problem is caused by a bitstring.py file in sys.path of test.py, but not in that of the interactive python shell. Most likely, there's a bitstring.py file in the directory test.py is in, and you started your shell from another working directory.
Since python traverses sys.path from front to end, modules in the current directory - even if accidentally created - overshadow those in system library directories.
Google App Engine actually had a similar issue at one point. The easiest solution there was simply comment the offending line or use try...except. Obviously that won't work here.
In that case, the problem was initialization order. A half second later a similar line of code was called again with success. Their solution? refactor. :-(
The best I've seen is a dynamic lookup of the class: bitstring.__dict__.get("BitArray") or getattr(bitstring, "BitArray");. It isn't ideal (and I believe I've even seen those return null), but hopefully it can get you somewhere.

Python2.7.1 import OpenCV2.2 error Windows XP

I'm having some trouble using OpenCV2.2 with Python2.7.1 (which should be compatible). I've installed OpenCV to D:\OpenCV2.2PreCom\, added the D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages path to sys.path as well as to the environment variable PYTHONPATH.
I've also made sure the D:\OpenCV2.2PreCom\bin path is added to the Path environment variable. However, when I try to access the cv.pyd file (which is in D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages\) by typing 'import cv',
I get an importerror:
The IDLE looks like this:
Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import sys
>>> sys.path.append('D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages')
>>> import cv
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
import cv
ImportError: DLL load failed: The specified module could not be found.
>>>
So what else can I try to make Python import OpenCV?
With kind regards.
Not a Windows user, but seems that the Python module cannot find the DLL. It is not a problem of PYTHONPATH.
Did you install it or just unpack it? Try installing it with the installer, it should take care of positioning the DLL in the right path.
Can you try escaping the backslashes? Try this:
>>>> sys.path.append('D:\\OpenCV2.2PreCom\\Python2.7\\Lib\\site-packages')
You should copy the content of D:\OpenCV2.2PreCom\Python2.7\Lib\site-packages (there should be two files inside ) in the site-packages of your python install, the default one being C:\Python2.7\Lib\site-packages and escape you back lashes or replace them with /

Python 2.5.2 and Solaris 8 (gcc 3.4.2) build issues

I'm trying to build python 2.5.2 on Solaris 8 using gcc 3.4.2. I can't see any immediate errors in the ./configure step but, once built and i enter the python shell doing an import time errors with :
Python 2.5.2 (r252:60911, Nov 21 2008, 18:45:42)
[GCC 3.4.2] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named time
What am i doing wrong? From what i can see with a cursory google is that there might be an error with libstdc++.so, but i can't find any hard details.
Any suggestions would be most welcome.
Many thanks,
Al.
The time module is not built by default in Python, if you build from a source distribution you need to explicitly enable all the modules you want to compile.
Open up Modules/Setup.dist in the python source tree and comment out the line which says:
#time timemodule.c
To enable the build of time module. Also remember that you need to recompile Python for this to take an effect.

Categories

Resources