Odoo custom module with external Python library - python-2.7

I created an Odoo Module in Python using the Python library ujson.
I installed this library on my development server manually with pip install ujson.
Now I want to install the Module on my live server. Can I somehow tell the Odoo Module to install the ujson library when it is installed? So I just have to add the Module to my addons path and install it via the Odoo Web Interface?
Another reason to have this automated would be if I like to share my custom module, so others don't have to install the library manually on their server.
Any suggestions how to configure my Module that way? Or should I just include the library's directory in my module?

You should try-except the import to handle problems on odoo server start:
try:
from external_dependency import ClassA
except ImportError:
pass
And for other users of your module, extend the external_dependencies in your module manifest (v9 and less: __openerp__.py; v10+: __manifest__.py), which will prompt a warning on installation:
"external_dependencies": {
'python': ['external_dependency']
},
Big thanks goes to Ivan and his Blog

Thank you for your help, #Walid Mashal and #CZoellner, you both pointed me to the right direction.
I solved this task now with the following code added to the __init__.py of my module:
import pip
try:
import ujson
except ImportError:
print('\n There was no such module named -ujson- installed')
print('xxxxxxxxxxxxxxxx installing ujson xxxxxxxxxxxxxx')
pip.main(['install', 'ujson'])

In python file using the following command, you can install it (it works for odoo only). Eg: Here I am going to install xlsxwriter
try:
import xlsxwriter
except:
os.system("pip install xlsxwriter")
import xlsxwriter

The following is the code that is used in odoo base module report in base addons inside report.py (odoo_root_folder/addons/report/models/report.py) to install wkhtmltopdf.
from openerp.tools.misc import find_in_path
import subprocess
def _get_wkhtmltopdf_bin():
return find_in_path('wkhtmltopdf')
try:
process = subprocess.Popen([_get_wkhtmltopdf_bin(), '--version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
except (OSError, IOError):
_logger.info('You need Wkhtmltopdf to print a pdf version of the reports.')
basically you need to find some python code that will run the library and install it and include that code in one of you .py files, and that should do it.

Related

Flask-mongoengine: Unable to import MongoEngine From flask-mongoengine

I must be missing something but I look around and couldn't find reference to this issue.
I have the very basic code, as seen in flask-mongoengine documentation.
test.py:
from flask import Flask
from flask_mongoengine import MongoEngine
When I run
python test.py
...
from flask_mongoengine import MongoEngine
ImportError: cannot import name 'MongoEngine'
Module in virtual environment contain (requirements.txt):
click==6.7
Flask==1.0.2
flask-mongoengine==0.9.5
Flask-WTF==0.14.2
itsdangerous==0.24
Jinja2==2.10
MarkupSafe==1.0
mongoengine==0.15.3
pymongo==3.7.1
six==1.11.0
Werkzeug==0.14.1
WTForms==2.2.1
My interpreter is Python 3.6.5
Any help would be appreciated. Thanks.
Since your using a virtual environment did you try opening your editor from your virtual environment?
For example opening the vscode editor from command-line is "code". Go to your virtual environment via the terminal and activate then type "code" at your prompt.
terminal:~path/to/virtual-enviroment$ source bin/activate
(virtual-enviroment)terminal:~path/to/virtual-enviroment$ code
If that doesn't work I, myself, haven't used flask-mongoengine. I was nervous of any issues that would come from the abstraction of it and instead just used Mongoengine with Flask.
I'm assuming you're only using this library for connection management so if you can't solve your issue with flask-mongoengine but are still interested in using mongoengine this was my approach. ~
I would put this in a config file somewhere and import it where appropriate-
from flask import Flask
MONGODB_DB = 'DB_NAME'
MONGODB_HOST = '127.0.0.1' # or whatever your db address
MONGODB_PORT = 27017 # or whatever your port
app = Flask(__name__) # you can import app from config and it will keep its configurations
then I would connect and disconnect from the database within each HTTP request function like this-
from config import MONGO_DB, MONGODB_HOST, MONGODB_PORT
# to connect
db = connect(MONGODB_DB, host=MONGODB_HOST, port=MONGODB_PORT)
# to close connection before any returns
db.close()
Hope this helps.
I had this issue and managed to fix it by deactivating, reinstalling flask-mongoengine and reactivating the venv (all in the Terminal):
deactivate
pip install flask-mongoengine
# Not required but good to check it was properly installed
pip freeze
venv\Scripts\activate
flask run

Python 2.7: No module named hexchat

I'm trying to write a Python script that does something with the inputs from the IRC chat that is connected to twitch.
However, when I do import hexchat, python keeps telling me there is no module named hexchat. I'm using Pycharm btw. I just want to be able to use the functionality in the hexchat module. Any help would be appreciated!
You need to first download and install Hex Chat module from here Downloads and then do the import hexchat.
For Linux - sudo apt-get install hexchat
Documentation for Python API is here Documentation
You load scripts inside hexchat. Window > Plugins and Scripts > Load.
I was having this issue on Windows 10 but I managed to resolve it by loading .py scripts in Hexchat like the below:
/py load "C:\where\your\file\is.py"
You also want to have module info in your .py file too otherwise Hexchat will complain:
__module_name__ = "helloworld"
__module_version__ = "1.0"
__module_description__ = "Python module example"

ImportError: No module named comments django

I'm using os x , I tried to run the project that I installed from Github
https://github.com/cfpb/idea-box
but there is error says : ImportError: No module named comments
maybe I miss something but since I'm newbie in Django I can't figure it out
Django comments are deprecated in latest version. You can either install older django version (such as 1.5).
Or the better solution is to install comments from external repository like so:
pip install django-contrib-comments
and then change all imports like this:
from django.contrib.comments.models import Comment # old
from django_comments.models import Comment # new
Sources:
Django's docs
Another question

Import error web "no module named web"

I did an web application using web.py and after a while I realize that my python compiler was configure to use python2 instead of python3 (both are installed). With python2 my application was working but I got string in unicode so I try to execute my code using python3:
python3 mycode.py
But I get different kind of error, the lib urlparse was not found (this problem was cause by a new name in python3 which is urllib) but I got a problem with:
import web
The return error is:
ImportError: No module named 'web'
I can't figure out where the problem come from.
Web.py cannot work with python3, this is the linked to the gitub project.
https://github.com/webpy/webpy/issues/108
Maybe you have installed web in your python2 lib but you have none in python3 lib... Check it out

Python can't locate modules which I have created

I am new to programming and have recently installed the Enthought Canopy distribution and can't seem to import certain modules.
Python 2.7 MacOSX
Numpy works when I import it, however other modules which I have created or downloaded as a simple module.py file return this error message:
import numfun1
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-297-cb46e477a372> in <module>()
----> 1 import numfun1
ImportError: No module named numfun1
Could it have something to do with where those modules are saved? If so, how do I point python in their direction? or where should I put those modules so that Python sees them.
Thank you in advanced for your suggestions.
Information about the module search path is included in the official Python tutorial: http://docs.python.org/2/tutorial/modules.html#the-module-search-path.
A lot of python libraries come with a setup.py script that will automatically install them into locations that are on the search path.
The installation process can be even more automated by using a Python package manager like pip.
If you create a module you must put it where your script is.