Gettext, virtualenvs, file not found

For my application I create the virtualenv in /opt/anw/ivirtualenvs and start the application with

/opt/anw/.virtualenvs/Kalendarium/bin/python /opt/anw/Kalendarium/calendarium/startKalender.py
and I get " FileNotFoundError: [Errno 2] No translation file found for domain: ‘calendar’".

Looking for the reason I copied gettext.py into my development environment and put some prints into it. Also I put test prints into my start program:




    def start():
	import gettext
	print("hpt 68")
	_ = gettext.gettext
	print(70)
	de = gettext.translation('calendar', localedir='locales', languages=['de'])
	de.install()
	_ = de.gettext
	opt, args = options.ApplicationOptionParser().parse_args()
	app = App(opt, args)
	"""
	if options.profile:
	    import hotshot
	    profiler = hotshot.Profile('cal.Profil')
	    profiler.runcall(app.start)
	else:
	    app.start()
	"""
	app.start()

    if __name__ == '__main__':
	start()

In development environment I activated the virtualenvironment and started the program with

(Kalendarium) egon@HeraDebEinzel:~/Entw/Kalendarium/calendarium$ python startKalender.py

The test output is


 get 53
    get60
    get104
    get 166
    GET 179
    get 215
    get 257
    get 316
    get 466
    get 522
    get 557
    get  601
    get 550
    get 652
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False
    hpt 68
    70
    eigenes gettext 524 calendar locales ['de'] None False
    eigenes gettext 524 messages None None None False
    eigenes gettext 524 messages None None None False

The first lines shows the initialtion of gettext. Then the translation method is seven times invoked. Then the flow goes back to my start program. My program invokes the translation method. And then there are two other invokes. After that the file is found and my program works normally.

Why are more invokes of translation?

The command

~/Entw/.virtualenvs/Kalendarium/bin/python /home/egon/Entw/Kalendarium/calendarium/startKalender.py

doesn’t find the file but this command find it

(Kalendarium) egon@HeraDebEinzel:~/Entw/Kalendarium/calendarium$ python startKalender.py

Egon

Did you activate the venv or just invoke the version of Python in it?

I wrote “In development environment I activated the virtualenvironment” , in production environment I did “opt/anw/.virtualenvs/Kalendarium/bin/python /”

Egon

In the development environment it works, in the production it doesn’t. You need to activate the venv in the production environment as well.

source ~/Entw/.virtualenvs/Kalendarium/bin/activate && ~/Entw/.virtualenvs/Kalendarium/bin/python /home/egon/Entw/Kalendarium/calendarium/startKalender.py

will probably fix the issue.

Thank you Dan!
But why there is a difference between activation and the direct Python-call?

And why there are invokes of the translation method during initialising of gettext?

Egon

Sourcing activate sets up a lot of environment variables. That’s what actually makes the venv work. Just calling that version of Python without the environment variables means that it doesn’t take packages from the correct place (among other things.)

I hope this explanation helps.

I did this in a terminal:

egon@HeraDebEinzel:~$ source /opt/anw/.virtualenvs/Kalendarium/bin/activate && /opt/anw/.virtualenvs/Kalendarium/bin/python  /opt/anw/Kalendarium/calendarium/startKalender.py
Traceback (most recent call last):
  File "/opt/anw/Kalendarium/calendarium/startKalender.py", line 85, in <module>
    start()
  File "/opt/anw/Kalendarium/calendarium/startKalender.py", line 69, in start
    de = gettext.translation('calendar', localedir='locales', languages=['de'])
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/gettext.py", line 531, in translation
    raise FileNotFoundError(ENOENT,
FileNotFoundError: [Errno 2] No translation file found for domain: 'calendar'
(Kalendarium) egon@HeraDebEinzel:~$ 




Where is the translation file found? My immediate guess is it’s using a relative path and not an absolute one and therefore not finding it.

I looked up activate and may be I found my mistake. I’ll check and then I’'ll report.

It is not necessary to activate a venv to use it.
I cannot recall activating a venv but I use them all the time.

Just run the python in the venv.

I think you need to figure out the absolute path to your locale directory and use use a relative path. I suspect you wil need to start with the __file__ of the main script.

I put the absolut path in “localdir” and it finds the fle. Now iI have to construct the path so it works in development and production environment.
Barry thank you Egon

1 Like