Load "whl" module dynamically - python

I want to be able to load modules dynamically from a *.whl files in my app.
I'm using sys.path.append('module.whl') for that and it works in most cases, I can't get it to work if the module has an *.so file in it, it cannot find it locally, for example using bcrypt module, for eample:
import sys
sys.path.append("bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl")
import bcrypt
I'm getting:
Traceback (most recent call last):
File "/Users/arossert/tests/whlimport/app.py", line 6, in <module>
import bcrypt
File "bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl/bcrypt/__init__.py", line 25, in <module>
ImportError: cannot import name _bcrypt
Inside the *.whl there is a _bcrypt.so file but cannot find it, is there a way around it?

Extract the wheel yourself:
import zipfile
import sys
whlPath = '/tmp/bcrypt-3.2.0-cp36-abi3-macosx_10_9_x86_64.whl'
targetDir = '/tmp'
with zipfile.ZipFile(whlPath, "r") as whl:
whl.extractall(targetDir)
sys.path.append(os.path.join(targetDir, 'bcrypt'))
import bcrypt
print(bcrypt)
Out:
python /tmp/script.py
<module 'bcrypt' from '/tmp/bcrypt/__init__.py'>
Note:
Make sure to have all dependencies installed!

One solution would be to unzip the file (as a whl is only an archived file) and then import it like this:
from zipfile import ZipFile
path = 'bcrypt-3.1.7-cp27-cp27m-macosx_10_6_intel.whl'
print(ZipFile(path).namelist())

Related

Building an executable file for python code using py2exe. ImportError: No module named cell

I installed "py2exe" for "Python2.7" , made a "Hello World" program, used py2exe to build its exe and it worked perfectly.
Now when I tried building the exe of my actual code, the folders get created and exe is also created without any problem but when I launch the exe the console appears for less than a second and closes.
Following are import commands which I am using:
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json
__version__='2.4.8'
from openpyxl import __version__
from openpyxl import Workbook
from Tkinter import *
import tkFileDialog
My setup file is like this:
from distutils.core import setup
import py2exe
__version__='2.3.5'
from openpyxl import __version__
from openpyxl.cell import Cell
setup(
windows=[{"script":'try.py'}],
options = {"py2exe":{"includes":["tweepy","openpyxl.reader.excel","openpyxl.cell","openpyxl.workbook","openpyxl.worksheet","jdcal","xlutils","json","xlrd","Tkconstants","Tkinter","tkFileDialog","xml.etree.ElementPath", "xml.etree.ElementTree","xml.etree.cElementTree"],
"packages": ["xml.etree", "xml"]}}
)
The error which I am getting is :
Traceback (most recent call last):
File "try.py", line 7, in <module>
File "openpyxl\__init__.pyc", line 29, in <module>
File "openpyxl\workbook\__init__.pyc", line 5, in <module>
File "openpyxl\workbook\workbook.pyc", line 8, in <module>
File "openpyxl\worksheet\__init__.pyc", line 4, in <module>
File "openpyxl\worksheet\worksheet.pyc", line 23, in <module>
File "openpyxl\utils\__init__.pyc", line 5, in <module>
ImportError: No module named cell
My file name is try.py
the solution is too use pip, just write in your terminal:
pip install openpyxl
If you are using windows, press:
Win + R
Type CMD in the run window which has opened
and then:
pip install openpyxl
for the future, sometimes (very often) you need to install python packages, for that reason pip is your best friend.
You problem is with openpyxl\utils\__init__.py that imports .cell, i.e. openpyxl\utils\cell.py which is not included in your application.
In your setup.py you listed some of the openpyxl subpackages in option include:
options = {"py2exe":{"includes":["tweepy","openpyxl.reader.excel","openpyxl.cell","openpyxl.workbook","openpyxl.worksheet",…
And openpyxl has a lot of subpackages.
You have to list openpyxl in packages:
setup(
…
options = {"py2exe":{
"packages": ["openpyxl", "xml.etree", "xml"]}}
)
No sure about listing all subpackages. If the packages above don't help try
listing them all:
setup(
…
options = {"py2exe":{
"packages": ["openpyxl", "openpyxl.workbook", etc…, "xml.etree", "xml"]}}
)

Basic python questino about import

I am trying to run some Python (2.7.6) code, and the compiler can't find the "util" module.
Here is the line from the code:
from util import getArgs, write
And the error is this:
Traceback (most recent call last):
File "rs.py", line 11, in
from util import getArgs, write
ImportError: No module named util
I can't find this util module anywhere, and pip install does not recognize util, getArgs, and write. How can I fix this problem?
As some of the comments have pointed out, it should be utils (plural) and not util
from utils import <your package>

Python module import error

when i am importing some modules from python shell, import work fine:
for example: (link for propy module https://code.google.com/p/protpy/downloads/list)
>>> import propy
>>>
but when i write script with python Default IDLE or with other IDE and save it to as .py script , import statements not work and generate error like this
python fragment_generator.py
>>>
Traceback (most recent call last):
File "J:\acetylome scripts\New folder\fragment_generator.py", line 1, in <module>
import propy
ImportError: No module named propy
>>>
please solve it
thanks in advance:
When you run the shell from the same directory the files are in the import will work. You are probably working from another directory when the import fails.
you can add a directory to the python path like this.
befor your import:
import sys
sys.path.append('/path/to/your/folder')
import propy
good luck.

ImportError: No module named test_data, but test_data.py in same directory as test.py under PyCharm using virtualenv

In test.py, I am trying to import test_data:
import unittest2
import re
from test_data import receipt1_example
test_data.py is in the same directory as test.py. I get the following error:
/Users/ahammond/.virtualenvs/ric2.6/bin/python2.6
/Applications/PyCharm.app/helpers/pycharm/utrunner.py
/Users/ahammond/src/hackfest_spring_2012/parse_me/test.py::test true
Testing started at 11:30 AM ... Traceback (most recent call last):
File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line
121, in
module = loadSource(a[0]) File "/Applications/PyCharm.app/helpers/pycharm/utrunner.py", line 44, in
loadSource
module = imp.load_source(moduleName, fileName) File "/Users/ahammond/src/hackfest_spring_2012/parse_me/test.py", line 4,
in
from test_data import receipt1_example ImportError: No module named test_data
Process finished with exit code 1
As you can see, I am running this under pycharm using a virtualenv. Here's a screenshot of the configuration:
The work around i use is:
import sys
import os
try:
import test_data
except ImportError:
sys.path.append(os.path.dirname(__file__))
try:
import test_data
finally:
sys.path.remove(os.path.dirname(__file__))
A friend told me that one can also add the directory entries to some include directories.
Please try PyCharm 2.5.1 RC, there was a bug with sys.path building (it contained incorrect, duplicated project source directory).
If it's not the case, you can mark additional directories as Source in Preferences | Project Structure or add them to the Paths in the Python Interpreters.

python: my package import fails on 3.1, but works on 2.6

I decided to develop my home project in python 3.x rather than 2.x. So I decided to check, if it works under 3.1. I run python3.1 above my package directory and then:
>>> import fathom
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "fathom/__init__.py", line 3, in <module>
from schema import Database
ImportError: No module named schema
when I enter fathom directory however schema can be imported:
>>> import schema
Also when I run python2.6 above my package directory I can do this:
>>> import fathom
My __init__.py has following import:
from schema import Database
from inspectors import PostgresInspector, SqliteInspector, MySqlInspector
Should I add something for python3.1?
Did you try a relative import?
from . import schema
from .inspectors import PostgresInspector
Works in Python 2.6 as well.
The 2to3 script can help you pinpoint more of these problems.

Categories