How can I use gettext in a kivy app on android? - python

I have a kivy app that uses a module from another package. The packages are organised as follows:
.
├── kivy_app
│ ├── bin
│ │ └── myapp-0.1-debug.apk
│ ├── buildozer.spec
│ └── kivy_app
│ ├── __init__.py
│ ├── __main__.py
│ ├── main.py
│ ├── python_pkg -> /home/jeff/projects/kivy_pkg/python_pkg/python_pkg
│ └── source
│ └── kivy_app.py
└── python_pkg
└── python_pkg
├── __init__.py
├── locale
│ └── en_GB
│ └── LC_MESSAGES
│ └── python_pkg.mo
└── source
└── version.py
The module kivy_app.py accesses version.py in python_pkg. This latter package uses a translation file based on gettext, set up in the normal way.
The application works perfectly on the PC, but when the app is run on an android device it crashes. The relevant lines in the logcat output are:
09-07 16:28:22.142 10274 10340 I python : File "/home/jeff/projects/kivy_pkg/kivy_app/.buildozer/android/platform/build/build/other_builds/python3-libffi-openssl-sqlite3/armeabi-v7a__ndk_target_21/python3/Lib/gettext.py", line 524, in translation
09-07 16:28:22.142 10274 10340 I python : FileNotFoundError: [Errno 2] No translation file found for domain: 'python_pkg'
I cannot find where gettext.py is trying to locate the mo files. Can someone please suggest how I might change things so that it works?
PS It works if I set the fallback value to True in gettext.translation(), but then no translation gets done!

I see that it is an old question, but recently I had the same problem: on PC it worked, on android crashed. And the output in the logcat was the same.
All .mo files were included in package. So, I've copied gettext.py to my project and added print-statements in function find. There is a part, where it goes through all possible languages, builds a path to .mo file and checks if it exists.
It appeared that on PC and android these language arrays are different. All I've got to do, was to change in my project locale directory name from EN to en, etc. Now it works.

Related

Import project's subpackages in Python

i'm experimenting with DDD in Python so i've decided to implement a toy project.
I've created different directories in other to separate shared concepts from specific bounded contexts concepts.
As i try to import these files, i'm facing a No module named error exceptions
For example, with this project structure:
.
└── src/
├── Book/
│ ├── application
│ ├── domain/
│ │ ├── Book.py
│ │ └── __init__.py
│ ├── infrastructure
│ └── __init__.py
└── Shared/
├── application
├── domain/
│ ├── Properties/
│ │ ├── __init__.py
│ │ └── UuidProperty.py
│ ├── ValueObjects/
│ │ ├── __init__.py
│ │ └── BookId.py
│ └── __init__.py
└── infrastructure
On src/Book/domain/Book.py i have:
from Shared.domain.ValueObjects.BookId import BookId
class Book:
bookId: BookId
pages: int
As i've seen in other answer (pretty old ones) it can be fixed by adding these folders to PYTHONPATH or PATH like sys.path.insert(*path to file*) but i'm wondering if there is a more pythonic way to achieve that.
I've also tried to add an __init__.py file to src and import as from src.Shared.domain.ValueObjects.BookId import BookId but none of previous attempts worked for me
On other repos i've saw that they use setuptools to install the src package in order to import it at unit tests (i cant either import them at tests), but i don't know if that is recommended or would work inside package imports
In case someone is facing the same issue as me, i managed to import subpackages and the full package in tests directory.
Just include, in each subpackage, an __init__.py file, and inside the package/subpackages, use relative imports (it looses semantic of imports, when we know where each imports comes from by absolute path from root directory, but works)
from ..Properties import UuidProperty
# inside __init__.py of Properties directory
from .UuidProperty import UuidProperty
And, by including an __init__.py inside src/ we could import them at tests directory like
from src.Book.domain import Book
Hope this helps someone!

Issues with project setup and imports

While working on my first "bigger" Python project, I'm running into a multitude of issues while trying to debug and import various modules/sub-modules. Here's my tree:
netbox-setup/
├── README.md
├── TODO.md
├── netboxsetup
│ ├── __init__.py
│ ├── constants.py
│ ├── helpers
│ │ ├── __init__.py
│ │ ├── custom_napalm
│ │ │ ├── __init__.py
│ │ │ └── ios.py
│ │ ├── infoblox.py
│ │ ├── ise.py
│ │ ├── netbox.py
│ │ ├── solarwinds.py
│ │ └── utilities.py
│ └── main.py
├── requirements.txt
├── setup.py
└── tests
main.py imports:
from netboxsetup.helpers import utilities
from netboxsetup.helpers import solarwinds
utilities.py imports:
from napalm import get_network_driver
from netboxsetup.constants import USER
From what I've been reading, it's recommended to use absolute imports in a package rather than relative. If I try to run main.py from within the netboxsetup folder, it states that netboxsetup cannot be found. So I removed that and just called from helpers import utilities. Now running main.py works, but when it imports the utilities file, the imports in the utilities file fail. Information I'm finding regarding import use in a package/module seems to be inconsistent on what to do/use.
Finally, if I run a python3 shell from the the netbox-setup folder, and use from netboxsetup.helpers import utilities it imports. If I do the same from the netboxsetup folder, it states that "ModuleNotFoundError: No module named 'netboxsetup'". Going back to where it worked, I then followed https://napalm.readthedocs.io/en/latest/tutorials/extend_driver.html to create the same setup exactly, and it states that my new method isn't found when I run the grab_inventory function I defined in the utilities.py. And I did appropriately create the respective get_inventory function in the class in the ios.py file as the Napalm docs advise.
def grab_inventory(ip, password):
# make less assumption, account for nx-os as well
driver = get_network_driver('ios')
with driver(ip, USER, password) as client:
result = client.get_inventory()
return result
I'm guessing all of my issues are related to pathing - whether absolute or relative, but I'm just having a very difficult time determining what the exact pathing is that works for everything. Is anyone able to point me into a proper source for proper import of modules in custom packages? Thanks.
P.S. Is it possible to debug an individual function in VSCode, giving it arguments at runtime as well (rather than having to run through all of the main sequence code to get to the function)?

How to structure an AWS Lambda with python in subfolders?

I'm struggling in figuring out how to properly set up a python Lambda in a subdirectory and have imports work correctly. For example, if you don't put your python in the root folder as recommended by AWS and instead, put it in a src folder with a lambda_handler.py that is the main handler in there and then packages/folders inside that, so you might have src/api, as an example. I am using the new SAM accelerate and they acknowledge a bug where it doesn't ignore the .aws-sam folder, so it will infinitely loop with the project in root, so they recommend a subfolder, but that greatly complicates things with Python, apparently.
I think I properly figured out how to get it to properly to read my own packages and modules in subfolders using init.py, but I can't get my requirements.txt to install, so they don't show up in the local build or the cloud build. I have found quite a few StackOverflows that are seemingly on the subject, but none of them seem to work for me or give an example that I can follow that works. The following is my structure:
/
.aws-sam
└── src
├── app_folder_1
│ ├── __init__.py
│ ├── example1.py
├── lambda_handler.py
├── app_folder_2
│ ├── __init__.py
│ ├── example2.py
├── requirements.txt
└── template.yml
I have an import of pymysql as an example and my dependencies for requirements.txt are never installed, so pymysql never is found. I feel like things shouldn't be this difficult. Can anyone assist?
UPDATE: I may have figured out the issue, which this post gave me a cluse to https://github.com/aws/serverless-application-model/issues/1927 It appears that sam invoke local has the same issue with custom templates -- something I was utilizing -- and that isn't very intuitive, even though they claim that is working as intended.
UPDATE 2: Definitely assisted with my progress, but it still isn't working as intended.
I forgot to return to this, so I figured I would post this here to assist others that may be struggling as Python is really finicky.
The following is the structure of a functional project in Lambda:
├── README.md
├── buildspec.yml
├── lambda_handler.py
├── locales
│ ├── en
│ │ └── LC_MESSAGES
│ │ └── base.po
│ ├── es
│ │ └── LC_MESSAGES
│ │ └── base.po
│ ├── fr
│ │ └── LC_MESSAGES
│ │ └── base.po
├── my_project_name
│ ├── __init__.py
│ ├── python_file_1.py
│ ├── python_file_2.py
│ ├── python_file_3.py
│ ├── repositories
│ │ ├── __init__.py
│ │ ├── resolvers_1.py
│ │ ├── resolvers_2.py
├── requirements-dev.txt
├── requirements.txt
├── template.yml
└── tests
├── __init__.py
├── tests_file_1.py
├── tests_file_2.py
├── tests_file_3.py
└── tests_file_4.py
Note that the base.mo files are generated in the build in the same location as the base.po files and that requirements-dev.txt has dev-only libraries, such as pep8 for formatting and is only run within my local conda environments [I set up virtual environments for each project using miniconda]. The purpose of the resolvers in the nested repositories folder is to separate out the controller-like python files from the calls to the repositories to retrieve data.

Spell checking in PyQtWebEngine

I am writing a program which is mainly in python but some interactive features are done through a web-app that talks to flask. It would be nice to have the web-app inside the python program so I am looking at using PyQtWebEngine.
This works surprisingly well except that I cannot get spell checking to work. I have run
self.page().profile().setSpellCheckEnabled(True)
self.page().profile().setSpellCheckLanguages({"en-GB"})
from inside my child class of QWebEngineView, and I have checked isSpellCheckEnabled() is True.
I wonder if it cannot find the languages. No qWarning is detected which I would expect if it cannot find the dictionary. As suggested by the non-python example.
I have an en-GB.bdic which I copied from the Chromium hunspell git. I have tried putting the file at:
<directory_my_py_file_is_in>/qtwebengine_dictionaries/en-GB.bdic
When I run
app = QApplication(sys.argv)
print(app.applicationDirPath())
the result is
/usr/bin
so I tried
/usr/bin/qtwebengine_dictionaries/en-GB.bdic
This wouldn't have been OK because I cannot edit this location when the program is pip installed, but it was worth a try.
With the .bdic file in either place I never see any spell check feature.
Has anyone got spellchecking working in PyQtWebEngine? I have not been able to find much in the way of documentation.
Assuming that the .bdic are valid then I have established the path of the dictionaries through the environment variable QTWEBENGINE_DICTIONARIES_PATH, for example I have translated the official example into python with the following structure:
├── data
│ ├── icon.svg
│ ├── index.html
│ ├── spellchecker.qrc
│ └── style.css
├── dict
│ ├── de
│ │ ├── de-DE.aff
│ │ ├── de-DE.dic
│ │ └── README.txt
│ └── en
│ ├── en-US.aff
│ ├── en-US.dic
│ └── README.txt
├── main.py
├── spellchecker_rc.py
├── qtwebengine_dictionaries
│ ├── de-DE.bdic
│ └── en-US.bdic
└── README.md
main.py
# ...
CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
os.environ["QTWEBENGINE_DICTIONARIES_PATH"] = os.path.join(
CURRENT_DIR, "qtwebengine_dictionaries"
)
# ...
Note: To get the bdic I have used the qwebengine_convert_dict tool executing:
qwebengine_convert_dict dict/en/en-US.dic qtwebengine_dictionaries/en-US.bdic
qwebengine_convert_dict dict/de/de-DE.dic qtwebengine_dictionaries/de-DE.bdic
The complete code is here.

No module named utils error on compiling py file

I'm trying to run a .py file through the command prompt using the command "python filename.py". I've already set the environment variables for python after I installed it, so I don't get any error when I type python. The file I'm running imports a few directories, all of which are preexistent in the same directory as the file I'm running, apart from the file web.py, which I can't seem to locate in the directory, so I'm assuming it's somewhere inside the python package, I have downloaded. But, I couldn't find it there either, so would I need to install an extension for python for the web.py file to be successfully imported or is there another way around this.
I've downloaded Python 3.4, I'm using windows 7 as my operating system and the exact error I receive when I try to compile the file is
ImportError: No module named 'utils'
Can someone please explain or direct me to a page which shows in detail how to install extensions for python?
The specific error happens when the Python interpreter can't find a particular ".py" file. In your case, it is the file "utils.py".
First you need to find which file is trying to import "utils.py". Starting with your main file, look up all the files you are importing. (I am guessing this issue is coming from one of the non-library files, but I could be wrong.)
Once you have the "top level" import list, check each of those files to see what THEY are importing, and repeat the process for them. Eventually, you will find the .py file which is trying to import "utils". There might be a directory specification forcing Python to look in the wrong place.
Finally, using windows' file manager, perform a search for "utils.py". As a temporary fix, you can copy it from its current location into your working directory. That will at least allow you to get your project up and running until you sort out the real cause.
This error occurs due to file(s)/folder(s) that are not in their respective locations.
I had a very similar error with a Python Flask Framework app, it turns out that my manage.py and config.py files were inside the app folder with the other folders(they were supposed to be outside the app directory), and that cause the error in my situation.
Once I placed the files in their proper location boom error was gone.
So Check you application framework and make sure things are located were they're supposed to be.
Good luck
I installed via apt (I use debian linux) and had this same error in one project. For me, the solution was to install via pip:
$ pip install utils
It should work for both python 2 and python 3.
So in my case I ran tree command in my Pipenv environment and it should be look like as below: I hope this helps.
.
├── README.md
├── __init__.py
├── core.yaml
├── core_blueprints
│ ├── __init__.py
│ ├── ami_lookup.py
│ ├── chef_buckets.py
│ ├── custom_resources
│ │ ├── __init__.py
│ │ └── cfn_custom_classes.py
│ ├── cw_alarm.py
│ ├── roles.py
│ ├── security_groups.py
│ ├── shared_iam
│ │ ├── __init__.py
│ │ └── iam_policies.py
│ ├── sns_subscription.py
│ ├── sns_topic.py
│ ├── ssm_chefrun_documents.py
│ ├── tf_state.py
│ ├── utils . #### This is not correct location.
│ │ ├── __init__.py
│ │ ├── standalone_output.py
│ │ ├── version.py
│ │ └── version_check.py
│ ├── vpc.py
│ ├── vpn_eip.py
│ └── vpn_server.py
├── core_hooks
│ ├── __init__.py
│ ├── cookbook_archive.py
│ ├── core_lambda.py
│ ├── keypair.py
│ ├── s3.py
│ ├── s3_cache.py
│ └── ssm.py
├── platform_version.py
├── prd1-ca-central-1.env
├── setup.py
└── utils ###### This is a correct location.
├── __init__.py
├── standalone_output.py
├── version.py
└── version_check.py

Categories