I have an existing application in Django.
I want to add a translation on the page.
On page I have:
{% trans 'Projects'%}
In .po file I added:
#: templates/staff/site.html: 200
msgid "Projects"
msgid "Projekty"
Then executes the command:
django-admin.py compilemessages -l pl
After this command, I get an error:
CommandError: This Should Be Run script from the Django Git checkout or your project or app tree, or with the settings Specified module.
$ python manage.py compilemessages --settings nsp.settings
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
I have got this error while I truly was inside project root folder.
The problem was, that I was running this command without python manage.py makemessages first.
The error message is misleading.
The error holds the answer, you could be running the script from anywhere so it cannot know which files to compile. Run the command from the project directory or specify the settings and you should be fine.
If you are using docker containers to build and deploy your application you should copy folder:
conf/
from root folder of your django project.
with the conf folder you should see i.e:
processing file django.po in /gamma/conf/locale/en/LC_MESSAGES
processing file django.po in /gamma/conf/locale/es/LC_MESSAGES
processing file django.po in /gamma/conf/locale/pt_BR/
without the conf folder you should see a clueless message like that:
CommandError: This script should be run from the Django Git checkout or your project or app tree, or with the settings module specified.
The error message is saying that it could not find the translations files where it expected them to be. Check that everything is correctly setup:
LOCALE_PATHS is defined in your settings.py
the files exist in the folder defined above (created by running python manage.py makemessages)
actually the error goes away even with just an empty locales folder
the compilemessages command is run from the project root folder
If you haven't set LOCALE_PATHS in your settings file, you need to do so:
import os
LOCALE_PATHS = [os.path.join(BASE_DIR, 'locale')]
Related
I find myself in a very weird situation:
I do not have my .mo files committed into my repo. I plan to generate them after I deploy
I installed the gettext buildpack availabe at https://github.com/piotras/heroku-buildpack-gettext.git
So this are the steps I did:
Deploy to heroku.
Run heroku run python manage,py compilemessages
This is being outputted:
processing file django.po in /app/.heroku/python/lib/python3.6/site-packages/django/contrib/redirects/locale/ru/LC_MESSAGES
processing file django.po in /app/.heroku/python/lib/python3.6/site-packages/django/contrib/redirects/locale/cy/LC_MESSAGES
processing file django.po in /app/prometheus/locale/zh_hans/LC_MESSAGES
processing file django.po in /app/.heroku/python/lib/python3.6/site-packages/django/contrib/gis/locale/sk/LC_MESSAGES
List one of files: heroku run ls /app/.heroku/python/lib/python3.6/site-packages/django/contrib/redirects/locale/zh_Hans/LC_MESSAGES RESULTS: django.mo django.po
List our file: heroku run ls /app/prometheus/locale/zh_hans/LC_MESSAGES/
RESULTS: django.po
django.mo is missing.
I ran python manage.py shell to see if I have some variables wrong:
heroku run python manage.py shell
In [1]: from django from django.conf import settings
In [3]: print(settings.BASE_DIR)
/app/prometheus
In [4]: print(settings.LOCALE_PATHS)
('/app/prometheus/locale',)
So i have no idea why my django.mo file is not being generated.
Any guesses?
Since Heroku 20, gettext is automatically added for the default Python build package. This is only during build and not at runtime. Therefore the command should be executed just after build.
Luckily this can be done using the post compile file in bin/post_compile (no extension like procfile) with the following line:
python manage.py compilemessages
Optionally you can add the specific language (e.g. python manage.py compilemessages -l nl)
Since Heroku automatically adds gettext, there is no more need to add additional build packs (at least for gettext) like the one from Piotras mentioned above.
These files are generated in the build phase and will be part of the compressed image. Therefore they should not be deleted afterwards.
TL;DR: Django.mo is generated and then deleted.
I'm in the same situation and tried to generate the files with
$ heroku run django-admin compilemessages
$ heroku run ls locale/LANG/LC_MESSAGES/
django.po
I didn't want to wait for connecting to Heroku on every command and used
$ heroku run "django-admin compilemessages; ls locale/LANG/LC_MESSAGES/"
django.mo django.po
$ heroku run ls locale/LANG/LC_MESSAGES/
django.po
Turns out, django.mo is created but for some reason it is immediately deleted.
Googling this gives Heroku Help -page Why are my file uploads missing/deleted?:
The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted. Each dyno boots with a clean copy of the filesystem from the most recent deploy.
What happens is
heroku run COMMAND
starts a new dyno, runs the command in it and shuts down the dyno once the command has exited, so all the files the command created are deleted.
As far as I know, options are either to commit the .mo-files to repo or serve them from storage, for example S3.
I am building a django+react app on Heroku, using django-npm which automatically installs all modules from package.json to node-modules dir and then copies everything to staticfiles/ during python manage.py collectstatic (which is triggered by Heroku during deploy).
However, for this configuration to work I need to pre-bundle my React app before deployment and put it into my static folder along with all the CSS, fonts, etc. to be picked up by collectstatic later.
But I don't want to pollute my git diffs with new bundle versions. So, is there a way to make webpack create a bundle during deployment?
I know there is a release command on Heroku where I can put my npm run build. But the problem is it only fires AFTER collectstatic, so my bundle will only be created in static/ folder after this folder is scanned, and won't get copied to staticfiles dir.
Update:
Using bin/pre_compile should no longer be necessary as of March 11, 2019. Heroku will now automatically call an app's build script (if defined in package.json) during Heroku's build process. Source: Heroku Changelog.
Original: You can accomplish this with the (undocumented) pre_compile hook exposed by the heroku/python buildpack.
Add an executable shell file named bin/pre_compile at the top level of your app and it will be called automatically as part of the build process.
cd my-django-app
mkdir bin
echo '#!/usr/bin/env bash' >> bin/pre_compile
echo 'npm run build' >> bin/pre_compile
chmod +x bin/pre_compile
I'm trying to work with Django on Heroku and I'm following this tutorial with its Django template https://devcenter.heroku.com/articles/django-app-configuration
https://devcenter.heroku.com/articles/deploying-python
But when i run 'heroku local web' it always look in the wrong directory. I've tried to move the project
(env) D:\Study\Workbench\heroku-testing\testing\env\codeShareApp>heroku local web
[WARN] No ENV file found
[WARN] ENOENT: no such file or directory, open 'C:\Users\William\Procfile'
[FAIL] No Procfile and no package.json file found in Current Directory - See
run_foreman.js --help
The project directory is as follows
env/
codeShareApp/
.idea/
codeShareApp/
.env
manage.py
Procfile
requirements.txt
runtime
include/
Lib/
Scripts/
tcl/
EDIT:
I tried to make a new project with directory like below, but still got the same error
codeShareApp/
codeShareApp/
env/
.env
manage.py
Procfile
requirements.txt
runtime
I encounter this problem also in a project; I solved this by Making sure that the Procfile is using the correct syntax, and that there are no extra tabs or newlines in the file.
especially whitespcae.
I dont know why, but I tried it on another machine and it works just fine. It seems that it has something to do with heroku cli 6.13 because the error only occurs after it updates from version 5.12 to 6.12
The documentation says:
The file must be placed in the root directory of your application. It will not function if placed in a subdirectory.
Try to do this and it should work. See the docs for more information.
So, I was working with an Ant build script to run some Django commands through manage.py and I started to see an error, but even when running from python;
C:\Users\markw\work\proj\src>python manage.py makemessages --all
--ignore=unittests\* --no-wrap
CommandError: This script should be run from the Django Git tree or your project
or app tree. If you did indeed run it from the Git checkout or your project or
application, maybe you are just missing the conf/locale (in the django tree) or
locale (for project and application) directory? It is not created automatically,
you have to create it by hand if you want to enable i18n for your project or
application.
The only change in my working copy is related to ANT and to confuse me further, the following ANT task completes correctly (as well as just running it from python);
<!-- Compile the translations -->
<target name="compile.trans" depends="init.properties">
<exec executable="${deps.python}"
dir="src"
failonerror="true">
<arg value="manage.py"/>
<arg value="compilemessages"/>
</exec>
</target>
Is there something special about makemessages? I've tried to run it from src like compilemessages which worked just a day ago, and also from mysite which along with django has a locale folder with the .po files.
Dir structure;
- proj
-- django
-- conf
-- locale
-- mysite
-- locale
manage.py
settings:
LOCALE_PATHS = (
os.path.join(settings_central.BASE_PATH, 'templates', 'locale',),
os.path.join(settings_central.BASE_PATH, 'mysite', 'locale',),
os.path.join(settings_central.BASE_PATH, 'django', 'locale',),
)
Your project layout is slightly incorrect. Your project locale directory should be proj/locale and not proj/mysite/locale, really.
When I initially created my Django project, I had this file folder structure:
project/
project/
manage.py
settings.py
...
In the topmost folder I have my git repo initialized as well as my vitual environment.
I want to collapse the top folder level (as if I had run `django-admin.py startproject .':
project/
manage.py
settings.py
However, when I tried to do this manually, it suddenly can't find the settings module. It's still looking for it at project.settings. Where might this be hardcoded?
When I run python manage.py runserver, I get this error:
ImportError: Could not import settings 'module.settings' (Is it on sys.path?): No module named settings
When I run django-admin.py help <some subcommands> I get this error:
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
I'm sure somewhere I have the path to the settings set, but I can't find it. Any ideas?
I've used these places in the past:
You might have set the PYTHONPATH environment variable in you .bashrc / .bash_history / some other shell startup file
You might have set said variable in your virtualenv in the $virtualenv/bin/activate script
You migth have symlinked your project from within $virtualenv/lib/pythonx.y/site-packages
The error is because of the following possibilities:
You aren't running the command from your project root i.e. project/ , not project/project.
Have not the project root i.e. project/ added to PYTHONPATH
Your manage.py isn't at project root i.e. project/ , instead it is in project/project.
Possible Solutions:
add the project root i.e. project/ to PYTHONPATH - because manage.py tries to do the equivalent of an import project.settings and for that to happen it needs to find project in sys.path
if you have the project root (project/) listed in PYTHONPATH - issue must be manage.py importing settings in form project.settings, where you have them both at the same level i.e. it has to be just settings - so move your manage.py one level up, to the project root, project/ from the current project/project.
set DJANGO_SETTINGS_MODULE to project.settings.
project.settings indicates that the setting module is one dir lower than manage.py - as in your case you're having both manage.py and settings.py at the same level i.e. project/project/
That is you have not used the default django project structure.
Anyways you can check /export settings just before running 'runserver' command: (from Official Django documentation - setting DJANGO_SETTINGS_MODULE)
Example (Unix Bash shell):
export DJANGO_SETTINGS_MODULE=mysite.settings
django-admin.py runserver
Example (Windows shell):
set DJANGO_SETTINGS_MODULE=mysite.settings
django-admin.py runserver
Suggestion: Try to abide by the conventional project structure (atleast till you become an expert). i.e. keep manage.py one dir level up the other project files .. settings.py, urls.py , etc