Wagtail 2.14.2 ModulNotFound error after running makemigrations - python

3rd try of wagtail
I did install it after having created a virtual environment named wag-env with virtualenv within the directory websites/wag/, not in the .virtualenv folder. Command was "virtualenv wag-env".
Did pip install wagtail
Then ran wagtail start ready_wag
I then ran python3 manage.py makemigrations and get this error messages "ModuleNotFound error wagtail.models". A check in the lib confirmed that there is a models folder at wagtail root level, but no models.py.
That's my third try since yesterday, first was with mkvirtualenv, had the same problem, second was with virtualenv, trying to integrate a finished django project, and now trying to start a project from scratch.
I ran a pip freeze --user, there's a wagtail installed globally. Should I get rid of it ?
Thanks in advance for your answers

I've always used venv to create my environments, never had issue. Sounds like you have conflicting versions of Django to match your version of Wagtail.
Here are my old notes from learning Wagtail, the process I still generally use for every project:
Create project folder, open terminal session in that folder
In project folder, create virtual environment (python -m venv .venv)
Start virtual environment (.\.venv\Scripts\Activate.bat or source .venv/bin/activate)
Install Wagtail (pip install wagtail==3.0.3) <- adjust version as needed
Start new project in current directory (wagtail start project .)
Install wagtail requirements (pip install -r requirements.txt)
If using POSTGRES, follow the POSTGRES setup instructions BEFORE migrating.
Provision database (python manage.py migrate)
Run server and check default site (python manage.py runserver)
Create superuser (python manage.py createsuperuser) and log in to admin area
Move database settings, allowed hosts and secret key from base.py to new file local.py in the same folder. Also any other settings that apply only to the local machine.
Create the git repository.
Setting up POSTGRES backend
With POSTGRES installed, from the command prompt, enter the POSTGRES command prompt:
psql -U username
username is the db global admin login created during install
Enter the following commands to add a project database and a user with full rights on the project db:
CREATE DATABASE myproject;
CREATE USER myprojectuser WITH PASSWORD 'password';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myproject TO myprojectuser;
Type \q to exit the POSTGRES prompt.
In your virtual environment, install psycopg2:
pip install psycopg2
Note, you may get a warning to install from an alternative source:
pip install psycopg2-binary
Create a blank database for the project. Set this up in the project local.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'projectdb',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Continue on to step 8 above.

Related

Django set environment variable via command line and use in Migrations

Following on from this question - Specify a .env file in Django in command line or vscode launch.json. So we have an environment file for dev, test, and production - which is specified in an environment variable ENVIRONMENT_FILE.
The .env file holds the database connection information (connection strings).
However, I'm struggling to pass this information when running the migration command:
python manage.py migrate
I need to be able to specify which environment I want the migrations to run on.
Is it possible to setup environment variables before migrations can be run?
You can load in the environment variables in your settings.py file.
If you have separate .env files for separate environments, and it is managed by the ENVIRONMENT_FILE variable, you can use something like python-dotenv to read your env files,
import os
from dotenv import load_dotenv
load_dotenv(os.getenv("ENVIRONMENT_FILE", ".env"))
Then, if your env file looks like this
DATABASE_URL=some-url-over-here
you can access these anywhere in your django application using os.getenv('DATABASE_URL')
You can set environment variables before you run the migration command. In PowerShell you can do something like:
$env:ENVIRONMENT_FILE='test.env'
python manage.py migrate

mod_wsgi can't import flask with pipenv

I'm trying to deploy my first flask application and I'm running into some issues. I had my app working on my local machine with the build in flask development server, and all my dependencies were managed by pipenv. I uploaded my app to /var/www/directory_printer and ran pipenv install. Then I created a apache vhost file and pointed it to my .wsgi file:
WSGIScriptAlias / /var/www/directory_printer/directory.wsgi
In directory.wsgi, I import my app. At the beginning of my main app file, I import flask. When I try to access my app, I get a 500 error. In the apache error log I get:
ModuleNotFoundError: No module named 'flask'
If I start an interactive python shell in the directory_printer folder with the pipenv shell activated, I can import flask just fine.
I tried putting the path to my virtual env at the beginning of my directory.wsgi file:
#!/path/to/venv
but that doesn't seem to help. I'm sure I'm missing something simple, but I can't seem to see what it is. Any help would be appreciated. Thanks!
Often on Linux systems a home directory is not accessible to other users so the Apache user will not be able to read anything under the directory. Permissions can also be wrong on Python packages that have been installed making them inaccessible as well.
Ok, not sure if this is the correct answer, but it is now working for me.
First
create a .venv folder in the project root folder
then change permissions:
sudo chown www-data:www-data .venv
Then create a virtual environment and install your requirements from Pipfile as the user wsgi will run as:
sudo -su www-data python3 -m virtualenv -p python3 .venv pipenv install
This will install your virtual environment in the project folder. Check out this answer for more:
How to set PIPENV_VENV_IN_PROJECT on per-project basis
Then add this to your .wsgi folder:
with open(activate_this) as file_:
exec(file_.read(), dict(__file__=activate_this))
Bottom of this page for more info:
https://flask.palletsprojects.com/en/2.0.x/deploying/mod_wsgi/
And now it works! Hopefully this will help somebody else out as well.

KeyError "RDS_DB_NAME" when trying to connect PostgresQL with Django with Elastic Beanstalk

I have deployed my Django app (Python 3.7) with AWS Elastic Beanstalk and are trying to connect it with a postgresQL database.
Following AWS' tutorial I have three files in my .ebextensions folder:
01_packages.config
commands:
01_postgres_activate:
command: sudo amazon-linux-extras enable postgresql10
02_postgres_install:
command: sudo yum install -y postgresql-devel
03_remove_cmake:
command: "yum remove cmake"
packages:
yum:
git: []
amazon-linux-extras: []
libjpeg-turbo-devel: []
cmake3: []
gcc-c++: []
I need cmake3 and gcc-c++ for some python packages I'm running, and I'm removing cmake not to worry about a wrong version of cmake that seems to be installed by default.
02_python.config
option_settings:
aws:elasticbeanstalk:container:python:
WSGIPath: mainapp.wsgi:application
NumProcesses: 3
NumThreads: 20
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: mainapp.settings
db-migrate.config
container_commands:
01_migrate:
command: "python manage.py migrate"
leader_only: true
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SETTINGS_MODULE: mainapp.settings
I've added a postgresQL DB through EB console. I've also tried first activating the virtual environment in the db-migrate.config file like this:
command: "source /var/app/venv/*/bin/activate && python manage.py migrate"
But no success.
The logs are telling me the 01_migrate command is returning an error. When I eb ssh into the EC2 instance, navigate to my app, activate the venv and then run python manage.py migrate, it gives me a KeyError that os.environ doesn't have a key named 'RDS_DB_NAME'. When I do printenv none of the environment variables show up either.
This is how the database part in my settings.py file look like:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
When I go to the console the health shows up as severe. I don't understand why these environment variables are not there after I connected the postgres database in the EB console.
Any ideas would be really appreciated? I've tried terminating and creating a new environment but no luck.

Deploying to Heroku: ImportError:Couldn't import Django

When I want to deploy my code to Heroku using
git push heroku master
I have ImportError:Couldn't import Django.
So I try python manage.py runserver to see what is going on and it shows this in the terminal.
Full description of ImportError:Couldn't import Django.
Before I tab all the process for deploying my code to Heroku,python manage.py runserver works well (the system gives me a link to open my Web page).
Here is my requirements.txt
requirements.txt
Usually, when you deploy a python app to Heroku, you must have a requirements.txt file. I had this same error and when I added all of my dependencies to a requirements.txt file, it all worked. Best way to do this is if you are running on a virtual environment is
pip freeze > requirements.txt

Unable to config django for mysql

I'm trying to connect Django app to MySQL on windows 10. I have installed mysqlclient as an interface between my Django app and MySQL. Whenever I try to open dbshell I'm getting error something like this:-
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Here is my setting.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'blog',
'USER':'root',
'PASSWORD':'root',
'HOST':'127.0.0.1',
'PORT':'3306'
}
}
I have tried other solutions on StackOverflow but it didn't work for me.
I also tried deleting the dbsqlite3 file from the project directory.
Versions for different elements are as follows:-
Python (3.6)
Django (1.11.4)
mysqlclient (1.3.12)
pip (9.0.1)
MySQL(5.7)
DJANGO_SETTINGS_MODULE variable is not available. run
python manage.py shell
if you linux user and run
python3 manage.py shell
as you use python 3.6
Finally, I solved this problem. I was using django-admin dbshell command instead of python manage.py dbshell
In case of windows, set env variable for MySQL in user variables section, otherwise you will get CommandError saying that mysql is not installed.
For Ubuntu use python3 manage.py dbshell

Categories

Resources