Import Error: No Module Named Common - problems with py2exe - python

I have a script that starts like this with many imports:
from reportlab.graphics import shapes
from reportlab.lib.utils import ImageReader
from reportlab.graphics import barcode
from reportlab.lib.units import mm
from reportlab.pdfbase.pdfmetrics import stringWidth
import reportlab.rl_settings
import PIL
from cStringIO import StringIO
import labels
import pyodbc
import pandas
from os.path import expanduser
from time import sleep
import sys
I struggled massively with py2exe to even get an executable file. I finally managed it with the following setup script (most of which is just copy and pasted from similar issues and suggests on stackoverflow).
from distutils.core import setup
import distutils
import py2exe
import sys
import zmq
import os
sys.setrecursionlimit(5000)
distutils.core.setup(
options = {
"py2exe": {
"dll_excludes": ["MSVCP90.dll"]
}
},
)
sys.path.append('C:\\WINDOWS\\WinSxS\\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9.0.30729.4148_none_5090ab56bcba71c2')
packages=[
'reportlab',
'reportlab.graphics'
'reportlab.lib.utils'
'reportlab.rl_settings'
'reportlab.lib.units'
'reportlabl.pdfbase.pdfmetrics',
],
os.environ["PATH"] = \
os.environ["PATH"] + \
os.path.pathsep + os.path.split(zmq.__file__)[0]
setup( console=[{"script": "working.py"}],
options={
"py2exe": {
"includes":
["zmq.utils", "zmq.utils.jsonapi",
"zmq.utils.strtypes"] } } )
I am sure my script is inelegant. It has three connected define functions and a final output.
try:
makeyourlabels()
except:
Print "Sorry, something went wrong."
I get an error when I run the file:
Traceback (most recent call last):
File "working.py", line 3, in <module>
File "reportlab\graphics\barcode\__init__.pyc", line 72, in <module>
File "reportlab\graphics\barcode\__init__.pyc", line 42, in _reset
File "reportlab\graphics\barcode\widgets.pyc", line 162, in <module>
File "reportlab\graphics\barcode\widgets.pyc", line 95, in _BCW
File "reportlab\lib\utils.pyc", line 243, in rl_exec
File "<string>", line 1, in <module>
File "<string>", line 1, in <module>
ImportError: No module named common
If anyone can make any sense of all this and get my .exe running, I would be ever grateful!

As there is no testable code in the question I can not guarantee this will fix your issue but I can explain why you get this error. It is because py2exe didn't bundle reportlab.graphics.barcode.common into your .exe.
How do I know this, I followed the traceback to see what Reportlab was doing in the rl_exec call, it turns out it is make this call using exec:
from reportlab.graphics.barcode.common import I2of5
But because the import is only done dynamically py2exe doesn't know about the need for this package.
So how do you fix it? Simply add 'reportlab.graphics.barcode.common' to your package list, that should help py2exe locate the module it is looking for.

Related

ImportError: cannot import name 'scan_videos' from 'subliminal'

I'm using the module subliminal to easily retrieve subtitles from the internet.
The CLI works perfectly, but I cannot import it as a module in a .py file.
The simplest code ever:
import os
from subliminal import scan_videos
videos = scan_videos('./')
print(videos)
And I always have this error:
Traceback (most recent call last):
File "subliminal.py", line 2, in <module>
from subliminal import scan_videos
File "/Users/louisbertin/Desktop/videos/subliminal.py", line 2, in <module>
from subliminal import scan_videos
ImportError: cannot import name 'scan_videos' from 'subliminal'
I don't know what I'm doing wrong. I had copy-pasted the code from the documentation:
https://github.com/Diaoul/subliminal
Seems like you called your script subliminal.py, which makes Python find it instead of the installed module. Rename the script and you should be fine.

"Import Error: cannot import name 'unicode_literals' "

Sorry if this is a dumb question but I'm trying to import and open a CSV using pandas in Python. Whenever I hit run I get the syntax error "cannot import name 'unicode_literals'". I have no idea why that is happening and I haven't been able to find any source online which details what this error means.
This is my code:
import pandas as pd
with open(r"FILEPATH\File.csv") as rawData:
pd.read_csv(rawData)
Here is the Error:
C:\Anaconda3\python.exe "FILEPATH"
Traceback (most recent call last):
File "FILEPATH/Main.py", line 1, in <module>
import pandas as pd
File "C:\Anaconda3\lib\site-packages\pandas\__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas\src\numpy.pxd", line 157, in init pandas.hashtable (pandas\hashtable.c:22997)
File "C:\Anaconda3\lib\site-packages\numpy\__init__.py", line 107, in <module>
from __future__ import division, absolute_import, print_function
File "C:\Anaconda3\lib\__future__.py", line 23, in <module>
from __future__ import unicode_literals
ImportError: cannot import name 'unicode_literals'
cannot import name 'unicode_literals'
Any suggestions for why this isn't working would be greatly appreciated.
You're on the right track! The only thing you have to do is add another parameter to open(). This would yield:
import pandas as pd
with open(r"FILEPATH\File.csv", encoding='utf-8') as rawData:
pd.read_csv(rawData)

Error when trying to execute a python script using py2exe

I tried many ways to make my code executable and I tried (pypinstaller,py2exe,cx_Freeze) without any results, therefore I focused on py2exe and I solved almost all errors which I faced while using this way, but the last error as I attached below is very difficult to solve it and I wasted a lot of time to solve it and I didn't catch any result, when I tried to run the executable file I got a CMD window with the below error and it disappear quickly, kindly help me to solve it or inform me by a way is supporting (xlrd,openpyxl,selenium,xlsxwriter,pytesser) libraries to I use it to execute my code instead of py2exe,
Traceback (most recent call last):
File "semiFULLTWO.py", line 8, 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 17, in <module>
File "openpyxl\writer\write_only.pyc", line 23, in <module>
File "openpyxl\writer\excel.pyc", line 36, in <module>
File "openpyxl\packaging\extended.pyc", line 4, in <module>
ImportError: cannot import name _version_
the libraries which I'm using :
*# -*- coding: utf-8 -*-
import os
import time
import xlrd
import base64
import urllib
import requests
import openpyxl
import xlsxwriter
from PIL import Image
from lxml import etree
from io import BytesIO
from pytesser import *
from openpyxl import Workbook
from datetime import datetime
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select*
the error related to openpyxl lib :
only_date = str ( dt.date ( ) )
workbook = xlsxwriter.Workbook ( only_date + '.xlsx' )
worksheet = workbook.add_worksheet ( )

matplotlib RuntimeError in cron job before actual import

I have a module I'm writing that uses matplotlib. However, I need it to work with a display(e.g. command-line execution), or when theres no display ( SGE/qsub cluster job, or a cron job).
I found this answer ( Automatic detection of display availability with matplotlib ), which works for the command-line execution and within a cluster job. But, in a cron job, it fails to properly import matplotlib, and seems to throw an exception before I import it.
In the cronjob, I try to import my module with the following script:
import os
os.environ['PYTHONPATH'] = 'path/to/my/module/dir:%s' % os.environ['PYTHONPATH']
print 'now loading mymodule...'
import mymodule
and mymodule.py just has the code from the earlier linked answer:
#!/usr/bin/python
import os
print 'testing import method...'
import matplotlib
r = 0
try :
r = os.system('python -c "import matplotlib.pyplot as plt;plt.figure()"')
except RuntimeError :
pass
print 'r=%d' % r
if r != 0:
print 'matplotlib: running in cluster, using Agg!'
matplotlib.use('Agg')
import matplotlib.pyplot as plt
I get this error message from the cron service:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/pyplot.py", line 109, in <module>
_backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
globals(),locals(),[backend_name],0)
File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 14, in <module>
from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
now loading mymodule...
testing import method...
r=256
matplotlib: running in cluster, using Agg!
I think its trying and failing to import matplotlib, which is throwing a RuntimeError, before getting to my code in mymodule.py . I can see that it gets to my code and runs it, but the Traceback message makes me nervous.
Why is it loading matplotlib(and crashing) before I tell it to(with Agg)? How can I fix/hide that traceback message?
As a dirty trick to hide the traceback message, just pipe all errors in the cronjob to /dev/null:
script.py 2> /dev/null

invalid elf header error for .so file

I'm trying to run a script in python, and I'm getting an "invalid elf header" error. I have a series of scripts that call one another, I'll go ahead and include those as well:
import sys
sys.path.append("/home/smh/Linux/Desktop/gras-03-03/python")
from python_utilities import GRASTestor
which calls GRASTestor.py
from BaseTestor import BaseTestor
import numpy as np
import hepunit as unit
from gdml_writer import gdml_writer
from GDMLGeometryBuilder import GDMLGeometryBuilder
from GRASMacroBuilder import GRASMacroBuilder,GRASRMCMacroBuilder
from Plotters import Plotter
import os
import sys
import SpenvisCSVFileHandler
which calls SpenvisCSVFileHandler.py
import string
import Spenvis.so
import os
from numpy import *
which is where we get our error, specifically with the line "import Spenvis.so"
/home/smh/Linux/Desktop/gras-03-03/python/python_utilities
Traceback (most recent call last):
File "perform_gras_rmc_tests.py", line 6, in <module>
from python_utilities import GRASTestor
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/GRASTestor.py", line 19, in <module>
import SpenvisCSVFileHandler
File "/home/smh/Linux/Desktop/gras-03-03/python/python_utilities/SpenvisCSVFileHandler.py", line 8, in <module>
import Spenvis.so
ImportError: /home/smh/Linux/Desktop/gras-03-03/python/python_utilities/Spenvis.so: invalid ELF header
And I'm not certain why it's not working. Any suggestions would be appreciated!
Never mind. Upon looking at the file architecture, it appears the file Spenvis.so is mac specific for some reason. Just need to get the correct file, then.

Categories