Today I have come across wired error running the Django application with virtual environments.
The application runs without errors both locally and Heroku under python-3.5.1 with Procfile
web: gunicorn --pythonpath='src/' proj.wsgi:application --log-file -
And my project structure is:
```
.
├── LICENSE.txt
├── Procfile
├── README.md
├── db.sqlite3
├── docs
├── env3
├── env
├── logs
├── manifest.yml
├── requirements
├── requirements.txt
├── run.sh
├── runtime.txt
├── site
└── src/proj
├── logger.py
├── settings
│ ├── __init__.py
│ ├── base.py
│ ├── development.py
│ └── production.py
├── urls.py
├── views.py
├── wsgi.py
```
But when I run it with python 2.7.11 I get the following error
ImportError: No module named proj.settings.development
I could easily guess the error is related pythonpath config. However I am curious why in first place I am getting an error?
Fixed now! The reason is that as the default django structure is rewritten every nested folder contains module need an __init__.py.
src/proj missing the __init__.py. Adding it fixed the error for python2. From python3.3 this is not an requirement
Reference PEP420 https://www.python.org/dev/peps/pep-0420/
Related
I have a python project and want to write a command line launcher. My project works fine when I run tests or launch from my IDE (pycharm). However when I try to launch via the command line I get a ModuleNotFoundError: No module named 'python' error.
Here is my directory structure:
.
├── Makefile
├── Pipfile
├── Pipfile.lock
├── README.md
├── create_jenkins_jobs.sh
└── python
├── __init__.py
├── create_credentials.py
├── create_jenkins_jobs_cli.py
├── credential_builder.py
├── jenkins_multibranch_job_builder.py
├── templates
│ ├── job.xml
│ ├── multibranch_job_template.xml
│ ├── ssh_cred_template.xml
│ └── user_pw_cred_template.xml
└── tests
├── __init__.py
├── integration
│ ├── __init__.py
│ ├── build_credential_test.py
│ ├── build_job_test.py
│ ├── conftest.py
│ ├── test_id_rsa
│ └── test_id_rsa.pub
└── unit
The shell script create_jenkins_jobs.sh contains:
#!/bin/bash
python3 python/create_jenkins_jobs_cli.py "${#}"
the file create_jenkins_jobs_cli.py contains the following code:
from typing import Set
from shared.common import preflight_env_check
from python.jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder
def configure_jenkins_jobs(git_repos: Set[str]):
jenkins_job_builder = JenkinsMultibranchJobBuilder(jenkins_server_url=JENKINS_BASE_SERVER_URL,
jenkins_folder=JENKINS_ROOT_JOB_FOLDER,
user=JENKINS_USER, token=JENKINS_TOKEN,
git_cred_id=GIT_SSH_CRED_ID,
git_repos=git_repos)
jenkins_job_builder.configure_jobs()
configure_jenkins_jobs(GIT_REPOS)
When I launch create_jenkins_jobs.sh or create_jenkins_jobs_cli.py I always get
ModuleNotFoundError: No module named 'python'
$>./create_jenkins_jobs.sh
/Users/pswenson/dev/cc-cicd-automation/.venv/bin/python3: Error while finding module specification for 'python/create_jenkins_jobs_cli.py' (ModuleNotFoundError: No module named 'python/create_jenkins_jobs_cli')
I've tried all sorts of different techniques to get this to work with PYTHONPATH, working directories, etc...
It seems there is something basic about how python modules work that I don't understand.
Does changing the line:
from python.jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder
to
from jenkins_multibranch_job_builder import JenkinsMultibranchJobBuilder
Sort you?
I installed a package in my anaconda environment by entering the following line:
pip3 install -e git+https://github.com/gauravmm/jupyter-testing.git#egg=jupyter-testing
I keep getting ModuleNotFoundError: No module named 'testing' on line: from testing.testing import test. I have no idea why this is happening, and believe it has something to do with the way my directory structure is set up.
My directory tree looks like this:
├── hw1_get_started.ipynb
├── requirements.txt
└── src
└── jupyter-testing
├── jupyter_testing.egg-info
│ ├── dependency_links.txt
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ └── top_level.txt
├── LICENSE
├── README.md
├── setup.py
└── testing
├── __init__.py
└── testing.py
I am trying to use this module : https://github.com/gauravmm/jupyter-testing.git#egg=jupyter-testing to do some testing in an online class.
I appreciate any help and explanation as to what I am doing wrong! :)
Is there a method of having manage.py outside of the Django project directory without having to change all the imports to from backend.<app>.<module> import ...?
├── project
│ ├── api
│ ├── frontend
│ ├── project
│ ├── manage.py
│ ├── Pipfile
│ ├── Pipfile.lock
├── project
│ ├── backend
│ │ ├── api
│ │ ├── project
│ │ ├── __init__.py
│ ├── frontend
│ ├── manage.py
│ ├── Pipfile
│ ├── Pipfile.lock
As expected after moving the Django project into the backend directory and modifying manage.py with the line below there's a ModuleNotFoundError when trying to run the server.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'backend.project.settings')
To enable import api instead of import backend.api, you need to add the parent directory backend to the python path.
import sys
sys.path.append('/path/to/project/backend')
You can also use a relative path if manage.py is in /path/to/project/:
import sys
sys.path.append('backend')
You should then use
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')
After making the changes, make sure you import api everywhere. Mixing import api and import backend.api can cause problems when the same modules are imported in different places. You can probably remove backend/__init__.py if you are going to stop importing backend.
Trying to configure pytest with django, the project already has a lot of test not written with pytest (written with unittest) but I am trying to get them run with pytest so I can write pytest tests and get it work with old tests.
I know pytest-django checks for the manage.py file in the root dir of a django project but this project the manage.py file is not in the root dir so I guess that's why the error below is thrown when I run pytest however running pytest and supplying a particular file works. How do I specify where manage.py is? As I can't find this in the documentation
pytest-django could not find a Django project (no manage.py file could be found).
You must explicitly add your Django project to the Python path to have it picked up.
you can define a python path to python commands that you want to run:
PYTHONPATH=/your/path/to/your/django/project/ pytest
or export your pythonpath before you run the pytest command:
export PYTHONPATH=/your/path/to/your/django/project/
pytest
As a standard practice, you should add a setup.cfg file to your root, with the following block -
[tool:pytest]
DJANGO_SETTINGS_MODULE=<package_name>.settings.py
You can later use the same file for linters by adding specific blocks for them.
In my case, I created a configure file named pytest.ini in the tests folder with the following content:
[pytest]
pythonpath = ../bitpin
DJANGO_SETTINGS_MODULE = bitpin.settings
python_files = tests.py test_*.py *_tests.py
Repository tree:
bitpin-repo
├── bitpin
│ ├── bitpin
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── __init__.py
│ └── manage.py
├── tests
│ ├── test_models.py
│ ├── pytest.ini
│ └── conftest.py
└── setup.py
[NOTE]
If the tests/ is next to the project, replace bitpin with .:
[pytest]
pythonpath = .
I have the following structure:
.
├── apps
│ ├── app1
│ │ ├── app1
│ │ └── setup.py
│ ├── app2
│ │ ├── app2
│ │ └── setup.py
├── my_django_project
│ ├── appA
│ ├── appB
│ ├── my_django_project
│ ├── db.sqlite3
│ ├── manage.py
│ └── requirements.txt
├── deployment
│ └── some files
├── locale
│ └── fr
│ └── LC_MESSAGES
│ ├── django.mo
│ └── django.po
├── log
│ └── some files
├── media
├── README.rst
├── run
│ └── some files
└── static
I have started to translate my project. I have sucessfully translated everything that was in my_django_project which is the actual django project. I have 2 apps installed with python setup.py develop, that are located outside the django project scope. I don't seem to be able to run the makemessages command. Running it with the manage.py helper doesn't find the apps in the apps folder. i.e nothing has been added to locale/fr/LC_MESSAGES/django.po. It worked fine for appA and appB located in my_django_project.
How do I run make messages for these apps located outside the django project ?
I need to create symlinks in the django project:
cd my_django_project
ln -s ../apps/app1/app1 .
and then I need to add an extra parameter to follow synlinks:
./manage.py makemessages -l fr --symlinks
EDIT:
A better way to fix this is to add a locale directory in the folder app, and to run the following command (in the app folder):
django-admin makemessages -l fr
And then messages are compiled by running the foloowing command (run in the app folder):
django-admin compilemessages