Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
6 Trả lời
17832 Lượt xem

Hello my friends;

I want to call a python library in a module?

But, as i know in static folder we add javascript library and we call them through xml file.

The question now is how i can use python library.

Can anyone help please.

Thank you very much in advance.

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi Drees,

When you've installed a Python library on your system it is available throughout the system. 
The only thing you have to do after installing the library is importing it in your Python file. For example if I would want to install pysftp (a Python library to access remote servers) I would first install the library on my system:

sudo pip install pysftp

P.S: Don't forget to keep an eye out for which Python version you want the app. If you need apps for Python 3 you should install them with pip3 install, if you want them for Python 2.x you should do pip install.


After doing this I can import this library in the Python file (usually under models/your_file.py) like this:

# -*- encoding: utf-8 -*- import pysftp from odoo import models, fields, api, tools, _

class YourClass(models.Model):

# Your functions and code here.

You can find an example in one of my public modules at https://github.com/Yenthe666/auto_backup/blob/10.0/auto_backup/models/db_backup.py#L35-L38


Regards,
Yenthe

Ảnh đại diện
Huỷ bỏ
Tác giả

Hi Yenthe thank you very much for your answer. But here is the link of the library i want to use. I have downloaded it and put it under static/lib but now i dont know how to call it in the xml file or __init__.py

Why don't you install the package? Anyways if you really want to add them in you should include them in the init.py file.

Tác giả

How exactly because i have installed the package and include the dependency in the manifest.py

If you add the Python files to your custom module (for example yourmodule/extra) then you should import all the Python files in your __init__.py.

Câu trả lời hay nhất

Vale Trood

firstly you have to install the python library by using

 sudo apt-get install python-yourlibrary

OR

sudo pip install yourlibrary

After that, you just simply have to import in py file like,

import yourlibrary

Thank You


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 3 21
6445
2
thg 8 20
5878
3
thg 7 20
20720
2
thg 1 19
6754
1
thg 12 18
2827