Eclipse/PyDev/Django Import Requires Project Name - django

Developing a project in Django with my IDE setup as Eclipse with PyDev. The following import statement:
from polls.models import Poll, Choice
works when running the project from the command line via:
python manage.py runserver
However, built in error-checking with Eclipse fails to find polls.models ("unresolved import Port"). I can fix this by adding the project name before the class and then running this. That is, make the import statement:
from projectName.polls.models import Poll, Choice
The issue is that I'm collaborating on the project and can't do this.
Question is: Is there a way to have Eclipse auto-detect or assume the projectName from the import statement?

Using projectName on import statements is not a good idea.
When working with django/python start to use virtualenv. Especially when working with eclipse/pydev. You can than configure a new interpreter for every virtualenv. Just add the virtualenv to the list of interpreters under "Preferences > PyDev > Interpreter - Python" and be sure to add your djangoproject root to the PYTHONPATH on the same preferences page.
This is essentially what django does for you when running from the command line.

Related

How I can run a telegram bot with the Django project?

Now, to run the bot through Django, I first use the python manage.py runserver command and then follow the link to launch a view with my bot. Can you tell me if there is an easier way to start my bot automatically when starting a Django project?
Actually, you can use a management command to run your bot with something like
python manage.py runbot
All Django context, including DB and settings, will be available
Reference to management command page:
https://simpleisbetterthancomplex.com/tutorial/2018/08/27/how-to-create-custom-django-management-commands.html
Maybe is a little late but you can do the following:
create the bot.py file in the same folder where manage.py is.
inside the bot.py make sure you import the following:
import django
import os
os.environ['DJANGO_SETTINGS_MODULE'] = '{Folder where your settings are}.settings'
django.setup()
and in order to run you just type python bot.py

Pydev Django1.4 ImportError

I have an old Django project. I upgraded to Django 1.6 and I want to see if it still runs. I did all the upgrades like change url names, but I did not change the structure. It runs perfectly fine from the command line, but when I run it in Pydev I get
ImportError: Could not import settings 'project.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named project.settings
What can I do to run it in Pydev?
I have just experienced the same problem and the solution was to set the Django settings module in the project properties. Right click on the project, then click Properties, go to PyDev - Django and set the Django settings module text field's value to the desired settings file.

How to run manage.py in Eclipse Pydev Interactive Console

I am trying to run
manage.py to validate models (as: manage.py validate) in the Interactive Shell of a Django project (called djangonew) using Pydev
The PYTHONPATH is set to include /djangonew ... so import djangonew and then dir(djangonew) actually gives me a name as 'settings' in the subfolder /djangonew/djangonew
but at the command-line I am unable run manage.py (and even find it)
How do I solve this issue? Thanks much
right click on project, open menu django-> custom command
and type verify ( or other command after "manage.py")

Error: Could not import settings

I'm switching a very large multi-package, multi-app Django (1.4.5) project from using Django's native test runner to py.test (2.3.5). The tests for the lowest level package, web-framework, were discovered and run with no errors after creating a setup.cfg (to manage the py.test options) and a conftest.py (to ignore setup.py). When running py.test (with a setup.cfg and conftest.py) on any higher level package, which all require web-framework, I receive the following error:
ERROR: Could not import settings 'high_level_package.test_settings' (Is it on sys.path?): No module named web_framework.settings
I'm using a virtual environment, and web-framework is in the venv at the following location: ENV/lib/python2.7/site-packages/
I've tried with the venv built in the package's root directory and with it built outside the project path, to no avail. Also, I can import web_framework.settings from the Python interactive command line in the higher level package's root directory.
My current conftest.py is just the following line: collect_ignore = ["setup.py"]
I've tried adding the following lines above it:
import os
import sys
sys.path.append(os.path.dirname(__file__))
I also tried hardcoding in the path to the web-framework package in the above sys.path.append.
Neither worked.
In case it's relevant, my setup.cfg is:
[pytest]
python_files = *test*.py
norecursedirs = node_modules ENV
addopts = --junitxml=python_result.xml, --doctest-modules
Edit:
Forgot to mention the traceback relationship. higher_level_package.test_settings imports higher_level_package.settings, which itself imports web_framework.settings.
in order to have it work you either need to have a develop-install of the worktree, or add . to the PYTHONPATH env var
Change to the folder where your main python website module is located
django-admin runserver --settings=mysite.settings
where mysite was created by
django-admin startproject mysite

Configuration error in django

I am using django with mod_python (I know it is deprecated -- I am just doing this for an exercise), and am getting the following error --
"Could not import project.views. Error was: No module named app.models"
I am getting the 'No module named app.models" in several other places as well. My syncdb is working fine, and if I go into the manage.py shell I can import the models fine. Why is this occurring and what do I need to change? Thank you.
You should use absolute imports everywhere. If your project is structured like so:
/project/settings.py
/project/app/models.py
/project/app/views.py
In INSTALLED_APPS you would use project.app. In app you'd import your models into views: import project.app.models, etc. Alternately you can try adjusting your PYTHONPATH so your imports work. When you run ./manage.py you are in your project folder, and Python automatically adds it to the PYTHONPATH. This doesn't happen automatically in most deployment scenarios (mod_python or other wise).