Django: Can't run custom commands - python

I've written a simple custom command, hello.py:
from django.core.management.base import NoArgsCommand
class Command(NoArgsCommand):
help = "prints hello world"
def handle_noargs(self, **options):
print "Hello, World!"
When I run python manage.py hello it returns
Unknown command: 'hello'
I've put it in the management/commands directory beneath my app.
I've added __init__.py files to the management and commands directory.
I've checked my app is in INSTALLED_APPS in settings.py
I've tried installing it in different apps and from the project root too
Running python manage.py syncdb etc is fine. And if I type python at the command line I can import django.core.management ok.
I know I'm missing something obvious, but can't figure out what.
How can I debug this to work out why my custom command won't run?

The problem was that I had another project on my PYTHONPATH. D'oh! I think it was picking up the settings.py from there first so didn't see my app. What pointed me in this direction was I tried running python manage.py create_jobs myapp (from django command extensions) and it returned an error indicating the app couldn't be found. Also #knutin mentioned INSTALLED_APPS.

It is because the __init__.pyc does not get created automatically within "management" and "commands" folder.
Copy your_app/__init__.py and your_app/__init__.pyc and paste it within the management/ and commands/ folder.

Related

IIS can not access newly added files in a django project

In a django project that is served by IIS (windows), i added a local file, test.py. The project ran perfectly before and still runs perfect on localhost, however IIS appears to not recognize the new test.py file. It appears IIS access to this file fails, even if the users IUSR and IIS_USRS have full access (same as for all other files in the folder).
I get below error message, and somethimes also the same but "No module named 'app.test'. Removing the "import app.test as test" in views.py solves the issue.
Suprisingly the "import app.oldFile as oldFile" works without issue.
In my views.py, i import the python scripts like this
import app.oldFile as oldFile
import app.test as test
My django project has the structure:
djangoRest
-app
--__init__.py
--views.py
--oldFile.py
--test.py
-djangoRest
--__init__.py
--settings.py
--urls.py
--wsgi.py
I found the reason, and solution.
In the earlier import file oldFile.py i use the line
import os
os.chdir(r\\some\\other\\directory\\)
This obviously changes the current working directory and as a result the other file test.py can't be found, and cannot be imported.
This mistake does not matter when testing django on the localhost, but only manifests in an issue when served by IIS.

importing from another folder in virtualenv

I'm following the Flask Mega Tutorial, and I'm running into an issue once I get to the second part and restructure my folder structure to match theirs, I cannot import Flask.
My current folder structure is as follows
/FlaskTest
/app
/static, templates etc
/flask
/virtualenv folders etc
/tmp
run.py
as far as I can tell, the folder structures are identical other than naming of the top level directory.
in my __init__.py file (/app/__init__.py), I'm doing as instructed in the tutorial,
from flask import Flask
app = Flask(__name__)
from app import views
I'm getting an Import Error that "cannot import name 'Flask'". I'm guessing the issue is because the flask package was installed to /flask/lib/site-packages.
My question: How can I reference the sub folder of flask/site-packages?
I've read through the python import system documentation and from what I can make of it through the first pass of reading it over, I would need to likely do something like from flask import flask.Flask or something to that effect.
UPDATE: So after cd'ing around the directory and checking pip list, I realized that flask wasn't accessible to my app directory. I ran pip install flask in the app directory. Now my site runs, but I'm not sure if this is the best practice of doing things with Python. Please provide some clarity as what the best practice is for installing packages and where the packages reside.
UPDATE 2: After creating a directory called standalone. In this folder, I created a virtual environment called standalone-test. Once, I did that, I also mkdir'ed app and copied it's contents from FlaskTest so that way the code would be identical. I was able to run the run.py script by using python run.py, but I can't run python -m app like you had said without running into an error. The error is as follows if it helps.
"No module name app.main; 'app' is a package and cannot be directly executed.
I am able to run python run.py as I mentioned, but I'm not able to run the python -m app command as you had mentioned
I think something went wrong in your execution environment. Here are some explanations.
The virtualenv
See the documentation of virtualenv
If you have followed the tutorial:
The flask directory is your virtualenv,
On posix system, you have a flask/bin subdirectory, or
On Windows system, you have a flask\Scripts subdirectory.
I make the assumption that you are on posix system.
To activate your virtualenv, run:
source flask/bin/activate
Your prompt should change to something like: (flask)$.
To list the installed libraries use pip:
pip list
Make sure you see Flask. The tutorial encourages you to install a lot of Flask plugins, so there are a lot of Flask-Something…
If Flask is missing, install it:
pip install Flask
Run your app
Your application is in the app directory, it has an __init__.py file (it's a Python package).
In this file, you have:
from flask import Flask
app = Flask(__name__)
from app import views
From your FlaskTest/ directory, try to run this script like this:
cd FlaskTest/ # if not in this directory
python -m app
This should import Flask, instanciate your app (but don't run it), import the views module.
If app/views.py exist you should have no error.
=> at this point, we have simulated what run.py imports…
Now write run.py in your FlaskTest/ directory:
#!flask/bin/python
from app import app
app.run(debug=True)
Run it like this:
python run.py
Note that the shebang #!flask/bin/python is unusual, but should work in the context of the tutorial.
This should start your http server…

django-extensions: Run a script using runscript which is inside a folder within scripts

I am getting problems running a script that is inside a folder inside of scripts folder using runscript command included in django-extensions.
The folder structure in my project is like:
-apps
-scripts
-syllabus
-first.py
-second.py
The files first.py and second.py are identical.
It has a run function as required by the django-extension runscript command.
def run(*args):
# my function call for the script.
I have well placed init.py and I can run second.py from the command:
./manage.py runscript second --script-args=excel.xlsx
But somehow I cannot run the first.py file from the runscript command. With this command:
./manage.py runscript first --script-args=excel.xlsx
I get
No (valid) module for script 'first' found
Try running with a higher verbosity level like: -v2 or -v3
I even tried running with higher verbosity level adding -v2 and -v3 at the end. But got this error:
No (valid) module for script 'first' found
I know that I am missing something simple, can anyone help me out?
You need to run the script as follows:
/manage.py runscript scripts.syllabus.first --script-args=excel.xlsx
Here scripts and syllabus are two directories containing __init__.py.
I dont have any experience with django. But the problem can be solved by adding syllabus to module lookup path. To do that add the following code in the __init__.py file under scripts directory.
import sys
import os
curr_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, curr_path + '/syllabus')
Or another way is to tell python that syllabus is a module itself. In that case you just need to rename first.py to __init__.py. You will then have to invoke it as
./manage.py runscript syllabus --script-args=excel.xlsx

Django ImportError: Could not import settings 'mysite.settings.local' (Is it on sys.path?): No module named settings.local

I'm setting up a site in django with multiple settings files as recommended in 2 scoops of django where settings.py is replaced with a settings directory that contains multiple settings files (local.py, etc.). My development server will run fine when I leave settings.py in the default location, but when I move it into the new settings directory I created and run the development server with:
python manage.py runserver --settings=mysite.settings.local
I get the error:
ImportError: Could not import settings 'mysite.settings.local' (Is it on sys.path?): No module named settings.local
I have added the directory to the python path and it shows up when I go into python and enter:
import sys
sys.path
I am running out of ideas. Any help would be great.
try touch __init__.py in your settings directory
Python needs to have an __init__.py file in your root directory so it can consider the subfolders as importable.
Please try adding __init__.py to your root directory and run
python manage.py runserver in the terminal
```

How do I get all files for my django app into one folder?

I currently have a Django app named reserve. In the folder named "reserve" I have most of the content of my app (views.py, urls.py, models.py, templates folder). However, I have a folder outside of "reserve" named "booking" that has only my settings.py. I tried consolidating by putting the settings.py in "booking" into "reserve" but I seem to be getting an error. Any advice on how to have only one folder with all contents?
The error I get is: ImportError: Could not import settings 'booking.settings' (Is it on sys.path?): No module named booking.settings
overall project folder
booking folder
settings.py
init.py
reserve folder (the app)
views.py
admin.py
models.py
...
what version of django are you using? what command are you running ? assuming that you are just trying to run a command using manage.py you could just chnage manage.py to reflect this if you are going to permanently keep this project structure
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
you should change os.environ.setdefault to the correct directory for your settings.py
I'm assuming you have manage.py at the overal project directory? I'm also assuming you want to just runserver.
It seems you moved the settings.py file outside the overal project directory. If you do this, you'll have to specify the settings file path when you do runserver, or you can also export the django settings module to point to your new settings path (using dot notation). In this case, I think it would be (in the command line):
export DJANGO_SETTINGS_MODULE=<overal-project-folder-name>.booking.settings
python manage.py runserver

Categories