Django manage.py question - python

Why is it that I have to run python manage.py somecommand and others simply run manage.py somecommand? I'm on OSX 10.6. Is this because there is a pre-set way to enable .py files to automatically run as Python scripts, and I've somehow disabled the functionality, or is that something that you explicitly enable?

If you are using a recent version of Django, the manage.py file should be an "executable" file by default.
Please note, you cannot just type manage.py somecommand into the terminal as manage.py is not on the PATH, you will have to type ./ before it to run it from the current directory, i.e. ./manage.py somecommand.
If that does not work please be sure that the manage.py file has:
#!/usr/bin/env python
as its first line. And make sure it is executable: chmod +x manage.py

There are two things you should look at:
First, is the manage.py script set to be executable? If not, try
chmod u+x manage.py
Secondly, does manage.py have a valid hashbang line? If not, you could try adding one that points to the correct python interpreter for your system.

On the mac the manage.py command has to be executable to just run it without the python command. You can do this with:
chmod 755 manage.py
If you are in the same directory as manage.py, to run it you type:
./manage.py somecommand
Otherwise you want to specify the path:
/path/to/my/project/manage.py somecommand

Related

Manage.py unknown command

I am a student and my profesor needs me to install Django on PyCharm.
I made a big folder called PyCharmProjects and it includes like everything I have done in Python.
The problem is that I made a new folder inside this PyCharmProjects called Elementar, and I need to have the Django folders in there but it's not downloading.
I type in the PyCharm terminal django-admin manage.py startproject taskmanager1 (this is how my profesor needs me to name it)
After I run the code it says:
No Django settings specified.
Unknown command: 'manage.py'
Type 'django-admin help' for usage.
I also tried to install it through the MacOS terminal but I don't even have acces the folder named Elementar (cd: no such file or directory: Elementar) although it is created and it is seen in the PyCharm.
Manage.py its python file after you start your project, you cant call this file until this command:
django-admin startproject mysite
Then run:
python manage.py runserver
And if you want apps in your project run:
python manage.py startapp my_app
First of all, you can't create a project using manage.py because the manage.py file doesn't exist yet. It will be created automatically in the folder taskmanager1 if you run the command below.
You can create a project with the command
django-admin startproject taskmanager1
After that you can change the directory to the taskmanager1 folder with the cd taskmanager/ command.
When you changed the directory you can use the python manage.py commando for example if you want to run your migrations or creating an app.
python manage.py migrate

How do I run an existing Django file?

I downloaded a folder with multiple python files in the form of a Django project for a course and I am unable to run it. Usually, when you create a Django project it is created within the terminal with the line django-admin startproject web_project . and then python manage.py migrate is used and then the line python manage.py runserver to start a server.
How do I start a server to run my project?
When I use django-admin startproject web_project ., it creates a new project which I don't want. When I use python manage.py migrate or python manage.py runserver it says:
Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3
But even when I used python3 manage.py migrate or python3 manage.py runserver instead and it returned:
python3: can't open file 'manage.py': [Errno 2] No such file or directory
At first make sure you opened command prompt or terminal in right directory(in projects directory) when you are running the runserver command.
If you are on windows open the project folder(the folder that contains files like manage.py), then right click and press open PowerShell window here.
first of all I recommend creating a virtual environment using the following command
python3 -m venv .env
then activate it
source .env/bin/activate
"django-admin statproject" its for initalite a new project for running an excited Django app you should just install Django - if there is now "requirements.txt" file- , migrate and run.
pip install Django
python manage.py migrate
python manage.py runserver

Django manage.py runserver invalid syntax

I am developing a web in Ubuntu using django. Everything works normal. Now, I want to change my computer which use Windows. When I try to runserver, it gives:
E:\DEGNet>py manage.py runserver
File "manage.py", line 14
) from exc
^
SyntaxError: invalid syntax
E:\DEGNet>py
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)]
on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
As shown above, I have installed Python 3.6.3. I have installed django and other necessary library using pip3 too.
Edit: manage.py file, it is a default manage.py that I get when generating the project.
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
I faced same problem but now solved with this cmd:
python3 manage.py runserver
Edit your manage.py file as given below:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DEGNet.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
)
execute_from_command_line(sys.argv)
Note that from exc is removed from the file. It is not required in the manage.py file.
Make sure that your virtualenv is activated. Suppose the name of your virtualenv is pythonpy, then run these commands:
virtualenv pythonpy
workon pythonpy #After running these command, you should see something like this but your file path may be different: "(pythonpy) C:\Users\ MyDjangoProject \
Then go to the project folder which contains manage.py
(pythonpy) C:\Users\ MyDjangoProject \ #Same path as above
Then simple run the server:
python manage.py runserver #This will give you the project path to the localhost. Copy and paste the URL in the browser and should work.
Try (from command line):
python3 manage.py runserver
If I used this (no python3) instead:
python manage.py runserver
the error persisted. This method allows you to not have to alter manage.py (so you can keep "from exc").
You just forgot to activate the virtual environment , do it like that :
source /home/adel/Dev/Python/DjangoProjects/myproject/venv/bin/activate
And than you can run the server :
python manage.py runserver
i've also got this error but solve it by installing pipenv first,
try run this first
pipenv shell django==2.1
then you should be able to run
python3 manage.py runserver
Ensure you're running the app from virtualenv i.e if you've created a virtualenv for your project, then activate the venv first.
me#debian:~/Desktop/webapp$source venv/bin/activate
(venv) me#debian:~/Desktop/webapp$python manage.py runserver
What's happening is that the wrong version of python is being used, which may not have all the dependencies in your virtualenv. I get this error when using sudo manage.py: using sudo changes the version of python which is being used to /usr/bin/python.
The problem is solved by specifying which version of python to use when using sudo:
sudo /path/to/my/env/bin/python manage.py makemigrations
I have met the same problem, and I find it strange because I have activated the virtualenvironment and set the python3, however, I meet this problem when I use this statement "python manage.py runserver".I use this statement often but I meet this only once, and I restart my virtualenvironment and it runs, hope you wil,too.
I was facing the same issue.
Activated venv & ran python manage.py runserver wasn't working.
I could see the venv was activated but still it wasn't working. Then tried python3 manage.py runserver which got rid of exc issue but now it wasnt detecting my installed libraries.
The change I made that broke it was renaming the base folder ( kind of same situation like OP base folder location was different now).
But wait, how does this impact anything?
A variable that stores a full path to venv inside venv/bin/activate is
now no longer valid.
Resolution:
deactivate venv if already running
open venv/bin/activate
search for VIRTUAL_ENV variable and rename as per the new folder changes made.
activate venv source venv/bin/activate
run python manage.py runserver
Tada, everything is back to normal and we can happily enjoy our lemonade.
I've got also the same issue with Python 3.4.4 and Django 2.0. I tried the last solution, nothing works (no need to delete that: from exc on the line 14).
Just run your server with:
python manage.py runserver
Instead of:
./manage.py runserver #or '.\manage.py runserver' for Windows
I have no problem running this way:
sudo ./**(your path)**/bin/python manage.py runserver
i had the same problem. I solved it by simply specifying the python version i.e type python3 manage.py runserver instead of python manage.py runserver
i re installed the v env
virtualenv venv --python=python3.7
i insttalled django
it worked
I had come out of my virtial environment.
I re-ran pipenv shell
Do this first on your command line where your Env folder is found:
source Env/bin/activate
Then now navigate to your project directory and do:
python manage.py runserver
It works!!
Just use python3 manage.py runserver instead python manage.py runserver

cannot open manage.py after installing django

I have a problem in setting up django.
My situation: I have Anaconda Python 2.7 in my Windows 8 computer. On the Anaconda command prompt window, I type: pip install django. This is successful.
Then I create a folder named "newproject". On the command prompt I went to the folder "newproject". Then django-admin.py startproject newproject. This is successful.
Then I run python manage.py runserver. It tells me
"...can't open file 'manage.py': [Errno 2] No such file or directory"
I checked out udemy django installation guide and other guides on the net. I have even set up a virtual environment. But the main problem is always: can't open file 'manage.py'
You're not in the proper directory...In the case you described, you should have:
mkdir newproject (not sure why you're doing this...but we'll continue with it)
cd newproject
django-admin.py startproject newproject
cd newproject ← A key missing part from what you did. You need to change into the directory where manage.py resides. Verify it by using ls at the command prompt after switching into the directory.
python manage.py runserver
Use ls often, if you need, to double check where you are in the directory tree.
You are not in the correct directory. You need to do cd newproject and the execute your runserver command.
For me it was because the manage.py didn't get created and the problem was that:
In windows instead of typing "django-admin.py" just type "django-admin" and it will create the manage.py file
you need to change your directory to the directory of the project.
open your environment by typing activate environment_name
change the directory to the folder in which you want to create the
project by cd Folder_Name
then create your project by django-admin startproject
Project_Name
then change the directory to your project folder
if you also want to create an app then typepython manage.py
startapp App_name
to confirm if it works type python manage.py runserver, then
you should get a link in the terminal. copy and paste that link in
the browser and you should see a successful page.
The reason of this problem is because you're in the first project directory "parent directory", and you have to go to your project "newproject" that has the manage.py file.
The way to this is simple:
cd "name of your project", E.g. cd newproject
python manage.py runserver
You need to include a . after you create the project.
Eg: django-admin.py startproject newproject .
This will allow you to run python manage.py runserver from where you are.
That can happen because manage.py doesn't have permissions.
Check the existence of manage.py throughout newproject, If exist you can fix it with:
$cd newproject
$chmod +x manage.py
Finally, try to execute python manage.py runserver
I was having the same problem, when Windows 10 changed credentials, Docker lost the access to drives. The fix for this issue, I reset the credentials in docker-desktop > shared Drives > reset credentials at bottom.
can't open file 'manage.py': [Errno 2] No such file or directory"
here is the solution
step 1: command -> django-admin startproject project_name
step 2: command -> cd project_name
step 3: command -> python manage.py startapp app_name
try it may fix your problem
You should navigate to the inner directory where your manage.py resides. For example:
If you have created projectname as
Django-admin startproject loginapp
Step 1: Go to loginapp
Step 2: Click on it
Step 3: You will find another loginapp and manage.py folder inside that
Step 4: Use this rootpath commandprompt
Step 5: Enter py manage.py runserver
Now it will work
I ran into the same error seller. But this is what I discovered
1: open your terminal in your project environment
2: install Django using: pip install django (if you are on windows)
3: then create your Django project using : django-admin startproject newproject . (django-admin start-project followed by project name give a space and add period (.) )
So it goes like this
django-admin startproject new project .
The period/fullstop (.) At the end tells djando the area in which the file should be created which is inside the project file its self you are working on. After this is done you are free to go. You can now run your server and enjoy
Run server using: python manage.py runserver
Make sure to put a period at the end of your command e.g
Django-admin startproject new project .
The period at the end (.) will solve the problem
There could be invalid interpreter selected .Try selecting the proper interpreter

Differences in the ways to running Python scripts

I have a very simple Python question, with examples using Django. When running a Python script, do I always have to precede the script filename with the python command?
In the Django tutorial I am following, some commands are as follows:
django-admin.py startproject mysite
However, other are like this:
python manage.py runserver
Why does the top one not need the python command?
Alternatively, if my system knows that all Python scripts are to be executed by my python interpreter, why does the bottom one need the python command at all?
The answer lies in a combination of two things:
The shebang, the first line of a file.
The permissions on the file, namely whether the executable flag is set or not.
Consider the first line of django-manage.py. On my system it is at /usr/local/bin/django-admin.py and the first line is:
#!/usr/bin/python2.7
And the permissions on the file:
-rwxr-xr-x 1 root root 127 Jan 9 13:38 /usr/local/bin/django-admin.py
The shebang tells my OS that I want to execute the file with the interpreter at /usr/bin/python2.7. The x characters in the permissions say that it is an executable file. I don't need to specify python beforehand, because the OS can figure out that stuff automatically from the above pieces of information.
Now for a freshly created manage.py, which I made by running django-admin.py startproject mysite, it looks like this:
#!/usr/bin/env python
And the permissions:
-rw-r--r-- 1 wim wim 249 Feb 17 17:33 manage.py
The shebang indicates to use whatever python interpreter my env is pointing at, so that part is in place already. But the file lacks executable permissions, so just running ./manage.py won't work (yet).
I can make it behave the same as django-manage.py by explicitly setting the executable flag using chmod +x manage.py. After that, you should see the x flags set when you do ls -l in the directory, and you should be able to run the file without specifying python beforehand.
Note: you do still have to use ./manage.py not just manage.py, this discrepancy is because django-admin.py lives in /usr/local/bin/ which is contained in PATH whereas the manage.py file likely doesn't. So we explicitly specify the path at the shell, where . refers to the current directory.
You can make the script executable by using chmod +x script.py, but you need the shebang line, usually #!/usr/bin/python
If you are using a script heavily on a unix-based maching (mac,linux) making an alias can be useful:
alias command="python path/to/script.py"

Categories

Resources