django for python set up issues - python

I'm not familiar with using command prompt and having trouble at the start of the django for python set up process which i'm reading here:
https://docs.djangoproject.com/en/2.2/intro/tutorial01/
My problem appears to be the 'python' command doesn't work (please see my command prompt screen shot).
I tried adding it into 'System variables' in the 'Environment Variables' as shown in the other screen shot.
Is there something i have done incorrectly here?
Also although i can get the create the initial project the only place i can create it is in the 'c:\Python\Lib\site-packages\django\bin' folder because i have to be looking at this folder in order to run the django-admin.py file.
I think the second issue is related to the first issue though i.e. if the 'python' command worked i could just run 'python django-admin.py startproject mysite' while my directory path is set to where ever i want the project to be created.
I expect i could simply move the mysite folder to where i want it but i feel i need to understand why this all isn't working as expected before moving on.
Additional:
I've now tried creating a virtual environment as suggested in the comments.
Running 'pip install virtualenvwrapper-win' appears to have worked. Running 'mkvirtualenv myproject' has not worked. It says 'C:\Users\mogwa\Envs is not
a directory although i can view this path so it is actually there.
https://docs.djangoproject.com/en/2.2/howto/windows/

Create Python virtual envoirement (virtual env, venv). Wait a little.
for Linux/GNU:
python -m venv venv
for Windows: (If not python in windows paths)
py -m venv venv
Activate your new python envoirement
for Linux/GNU:
source venv/bin/activate
for Windows: (Option 1)
source venv/Scripts/activate
for Windows: (Option 2)
.\venv\Scripts\activate.bat
Install django over pip (You should see (venv))
pip install Django
Start new Django Project:
django-admin startproject projectname
For Start new Django App:
django-admin startapp appname

Related

Activated Virtualenv in commands but the bracket is not showing in the directory [Python]

I am trying to learn django.
In VS code, I have installed pip and virtualenv.
I created a env folder by using virtualenv command.
$ virtualenv env
To activate virtual environment, I ran the command below.
$ source env/bin/activate
The result I was expecting to see was I see the little brackets in front of directory address as below:
dhkang#dhkang-Lenovo-IdeaPad-S145-15API ~/fastcampus/django
% (env)
However, I do not see any brackets to indicate that the virtual environment is being activated.
dhkang#dhkang-Lenovo-IdeaPad-S145-15API ~/fastcampus/django
% source env/bin/activate
However, I do not see (env) here. I don't know if the virtual environment is activated or not.
Does anyone know how to resolve to show the brackets when virtual environment is activated?
Ok if you are using builtin zsh in vscode, After you activated the virtualenv you should delete the terminal and start new one using the view tab...
Delete terniaml using icon in right top of the terminal. (Use the trash icon)
Start new terminal..
View > Terminal
Now, I resolved my issue.
It was one of zsh settings that I could never imagine as I am fairly new to Linux.
What I did was trying out other shell types like bash. I tried bash as default terminal and the bracket indicating it's on virtual environment was showing.
In the end, I have installed oh-my-zsh and changed the theme 'agnoster'. Now, anywhere in terminal or VScode clearly show the brackets. With regards to oh-my-zsh, refer to the link https://github.com/ohmyzsh/ohmyzsh

Python/Django: How to reactivate pipenv after exiting

Please Everybody. I have a little problem with pipenv.
For instance, I am work on a Django project and I decided to sleep. I had to shutdown my laptop for some reason. Then i woke up navigated to the project and I open it in VScode again. My question is how to I reactivate the pipenv environment again.
I mean something like source bin/activate if you are using virtualenv
I use pipenv shell but i want to be sure that is absolutely right.
pipenv —venv to get the location of the virtualenv
Then activate it like a normal virtualenv.
As a new user of both Django and Pipenv I recently ran into the same issue. Maybe this will help someone.
(for Mac m1 steps maybe different for you)
expose hidden folders inside the home directory. Shift + command + >
you should see a folder called " .local." follow it through...
.local > share, virtualenvs. The virtualenvs is what your looking for. It contains folders for all of pipenvs.
from the integrated terminal inside vscode I cd into whichever pipenv folder coorespondes to the project I'm working on then run:
source bin/activate.
the terminal should return with an indicator in parens ahead of user name confirming the shell is active again:
(Blog) user#user-MBP Blog %
cd back into project folder (the one containing manage.py) and run python manage.py runserver
should be good to go.
pipenv --venv => outputs the virtual environment directory (ie "/Users/td/.local/share/virtualenvs/data-infra-HlhPgy0O")
You can copy the output of the previous command and then run (on zsh):
source [vitrual_environment_directory]/bin/activate
(ie source /Users/td/.local/share/virtualenvs/data-infra-HlhPgy0O/bin/activate)

How to install Flask on Windows?

I have a project to do for after create a webpage that display the latest weather from my CSV file.
I would like some details how to do it (don't really get the http://flask.pocoo.org/docs/installation/#installation installation setup)
Can anyone mind explain me how to do it simply?
Thanks.
I'm running on Windows 7, with the Windows Powershell.
Install pip as described here: How do I install pip on Windows?
Then do
pip install flask
That installation tutorial is a bit misleading, it refers to actually running it in a production environment.
First install flask using pip,
pip install Flask
* If pip is not installed then install pip
Then copy below program (hello.py)
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run()
Now, run the program
python hello.py
Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Just copy paste the above address line in your browser.
Reference: http://flask.pocoo.org/
Assuming you are a PyCharm User, its pretty easy to install Flask
This will help users without shell pip access also.
Open Settings(Ctrl+Alt+s) >>
Goto Project Interpreter>>
Double click pip>> Search for flask
Select and click Install Package ( Check Install to site users if intending to use Flask for this project alone
Done!!!
Cases in which flask is not shown in pip:
Open Manage Repository>>
Add(+) >> Add this following url
https://www.palletsprojects.com/p/flask/
Now back to pip, it will show related packages of flask,
select flask>>
install package>>
Voila!!!
https://www.youtube.com/watch?v=QjtW-wnXlUY&t=38s
Follow as in the url
This is how i do :
1) create an app.py in Sublime Text or Pycharm, or whatever the ide, and in that app.py have this code
from flask import Flask
app = Flask(__name__)
#app.route('/')
def helloWorld():
return'<h1>Hello!</h1>'
This is a very basic program to printout a hello , to test flask is working.I would advise to create app.py in a new folder, then locate where the folder is on command prompt
enter image description here
type in these line of codes on command prompt
>py -m venv env
>env\Scripts\activate
>pip install flask
Then
>set FLASK_APP=app.py
>flask run
Then press enter all will work
The name of my file is app.py, give the relevant name as per your file in code line
set FLASK_APP=app.py
Also if your python path is not set, in windows python is in AppData folder its hidden, so first have to view it and set the correct path under environment variables. This is how you reach environment variables
Control panel ->> system and security ->> system ->> advanced system setting
Then in system properties you get environment variables
On Windows, installation of easy_install is a little bit trickier, but still quite easy. The easiest way to do it is to download the distribute_setup.py file and run it. The easiest way to run the file is to open your downloads folder and double-click on the file.
Next, add the easy_install command and other Python scripts to the command search path, by adding your Python installation’s Scripts folder to the PATH environment variable. To do that, right-click on the “Computer” icon on the Desktop or in the Start menu, and choose “Properties”. Then click on “Advanced System settings” (in Windows XP, click on the “Advanced” tab instead). Then click on the “Environment variables” button. Finally, double-click on the “Path” variable in the “System variables” section, and add the path of your Python interpreter’s Scripts folder. Be sure to delimit it from existing values with a semicolon. Assuming you are using Python 2.7 on the default path, add the following value:
;C:\Python27\Scripts
And you are done! To check that it worked, open the Command Prompt and execute easy_install. If you have User Account Control enabled on Windows Vista or Windows 7, it should prompt you for administrator privileges.
Now that you have easy_install, you can use it to install pip:
easy_install pip
First: I assumed you already have Python 2.7 or 3.4 installed.
1: In the Control Panel, open the System option (alternately, you can right-click on My Computer and select Properties). Select the “Advanced system settings” link.
In the System Properties dialog, click “Environment Variables”.
In the Environment Variables dialog, click the New button underneath the “System variables” section.
if someone is there that above is not working, then kindly append to your PATH with the C:\Python27 then it should surely work. C:\Python27\Scripts
Run this command (Windows cmd terminal): pip install virtualenv
If you already have pip, you can upgrade them by running:
pip install --upgrade pip setuptools
Create your project. Then, run virtualenv flask
heres a step by step procedure (assuming you've already installed python):
first install chocolatey:
open terminal (Run as Administrator) and type in the command line:
C:/> #powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin
it will take some time to get chocolatey installed on your machine. sit back n relax...
now install pip. type in terminal
cinst easy.install pip
now type in terminal:
pip install flask
YOU'RE DONE !!!
Tested on Win 8.1 with Python 2.7
I have windows 10 and pythonv3.5. #uku answer is correct. however, problem I was facing is that where are python scripts which are to be added in environment variable. So I found out that we need to add
C:\Users\\AppData\Local\Programs\Python\Python35\Scripts
above location as environment variable. If it still doesnot work search for python in C Drive then find out script locations.
If You are using windows then go to python installation path like.
D:\Python37\Scripts>pip install Flask
it take some movement to download the package.
you are a PyCharm User, its good easy to install Flask
First open the pycharm press
Open Settings(Ctrl+Alt+s)
Goto Project Interpreter
Double click pip>>
search bar (top of page) you search the flask and click install package
such Cases in which flask is not shown in pip: Open Manage Repository>> Add(+) >> Add this following url
https://www.palletsprojects.com/p/flask/
Now back to pip, it will show related packages of flask,
select flask>>
install package

python manage.py startapp polls

I'm following along with the djangoproject.com tutorial. I used pyCharm to setup a dummy project.
on my terminal command line I cd into the folder that has manage.py i then run the line.
python manage.py startapp polls
I get this error:
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management
What is this and why can't I create another app called polls?
In my case, I already have installed Django, but I type "runserver" command from virtualenv and it showed the same error that Chapsterj
I resolved typing this:
(venv)$ pip install Django
Before, To enter in virtualenv, type this:
$ virtualenv venv --distribute
$ source venv/bin/activate
(venv)$
I've never heard of PyCharm, but as the comments suggest, this means the python interpreter cannot find django.
One thing to try - open up a terminal (anywhere) and type:
python
then run
import django
If you have django installed, this should work; otherwise it will not.
In my case, I run all of my django installs via virtualenv (basically, localised versions of the python package index so different projects can have different versions of packages installed) so doing this from any old terminal for me doesn't work.
In my case, I then need to make an adjustment to the PYTHONPATH environment variable. Well, actually, I'm lying slightly, virtualenv is a little more complicated than that, but that's another way to make it work.
So, I would suggest:
Making sure you have django installed in your python site-packages directory (where most third party packages live). Usually, something like yum install python-django or pip install Django will do this for you.
If this is not where you've installed Django, edit your PYTHONPATH first in a shell session (to make it easy to test and get right) and then in your IDE so python can find it.
If the IDE doesn't provide you with a way to edit the pythonpath variable, you should be able to manage this by editing manage.py like so:
import sys
sys.path.append('/opt/Django')
# the rest of manage.py ...
import django
To properly install django you must use the "superuser", try
sudo pip install django
Hope it helps
On Rhel6, umask prevented the django directory from being accessed, so I had to run:
cd /usr/lib/python2.6/site-packages/django/
find django -type d | xargs chmod 775
find django -type f | xargs chmod 664
and add /usr/lib/python2.6/site-packages/django/ to my path in .bash_profile.
I see no accepted response to this and since it is not so much a Django question as it is a PyCharm question, I'll add my 2c in case others are having the same problem getting started.
I am also running through the "Writing your first Django" tutorial using PyCharm and I am not having these problems. I WAS having all sorts of problems until I realized that you let PyCharm create everything and the problems go away ;-)
So far, I am reading both the PyCharm AND the Django tuts together. PyCharm tut is here: https://www.jetbrains.com/pycharm/help/creating-django-application-in-a-project.html
YOUR problem may be answered with this:
To add a new django application to an existing project
On the main menu, choose Tools | Run manage.py task
In the Enter manage.py task name dialog box, type startapp. Note suggestion list that appears under the dialog box after entering the first letter, and shrinks as you type to show the exact match only.
In the dialog box that opens, type the name of the new Django application.
I had a same problem with PyCharm, what I did was:
Go to File -> Default Setting ->
Project Interpreter (Pick python 3...) if thats what you want to use
It will show all packages available
Double click on Django
Install Package.
Then in terminal instead of
python manage.py migrate
tape
python3 manage.py migrate

django-admin.py startproject mysite not working well on windows 7

I'm learning django and I did successfully start a site on Windows XP by following the tutorial.
However, on Windows 7 when I issued:
django-admin.py startproject mysite
python.exe was started and a window appeared to ask me to choose either python.exe or other program to open a file....
Did I do anything wrong or there are more tricks for windows 7?
That's already been reported to the django, see http://bugs.python.org/issue7936
And these's an solution:
open regedit and find python.exe like this
"C:\Python26\python.exe" "%1"
change it to
"C:\Python26\python.exe" "%1" %*
This is what worked for me:
I already had c:\python27 and c:\python27\Scripts in PATH.
Copy django-admin.py and django-admin-script.py from c:\python27\Scripts to your desired folder
with cmd go to that folder and run > django-admin startproject mysite
Note that if I doesn't type python windows just opens the django-admin.py in editor.
I had the same error when I ran django-admin.py on the command prompt. I couldn't create a projects.
I tried to run Regedit by using Find to Search python.exe and then adding %% but it was not sufficient.
I then added the necessary Path to the System PATH as shown below.
Open:
Control Panel -> System -> Advanced -> Environment Variables -> PATH.
Add to the PATH:
C:\Python27\python.exe;C:\Python27\Scripts;C:\Python27\Lib\site-packages\django\bin
It Work! Now I can Create Django Projects and applications Using Command on Windows 7.
In latest django(1.11.7 for now) , simple django-admin would work, instead of django-admin.py .
I had the same problem i followed django-admin.py startproject mysite not working well on windows 7
and found that the python files still associated with my text editor.
Changing the necessary registry keys fix the issue.
I also had the same problem but don't worry its simple to solve this problem, just add "C:\Users\Nik's\myenv\Scripts" to the enviroment veriables and its done.
The address may be differnt in your pc as it depends where you install it.
Change the first line of django-admin.py #!/usr/bin/env python to for example #!D:\Program Files\Python\python.exe (Where you install your python.exe,that's my directory); it works.
The solution is simple in Windows:
1-Go to C: \ Python34 \ Scripts
2-Right click on django-admin.py
3-Select open with
4-Select default program
5-Select Laucher Python for Windows (Console)
6- Run the command in CMD Windows python django-admin.py startproject mysite
In Windows 8.1, I faced the same problem. In my case when I tried to create my project, it would open the contents of the page in Internet Explorer window rather than creating the project folder. I became curious of what exactly was this django-manage.py. After searching, I got to django-admin.py file in the path C:\Python34\Scripts and I right clicked on it chose to open with default program of Launcher Python for Windows(Console).
Again I got back to the CMD and in my project directory I entered:
django-admin.py startproject mysite
And worked just fine.!!!
I have faced the same issue and when I ran the Python installer again and clicked on the repair option, it magically fixed everything for me. Now, I can run django-admin straight from CMD.
first of all make sure that python/Scripts is in your path
run "django-admin startproject mysite" instead of "django-admin.py startproject mysite"
I get the same problem for Windows 10. Here is how I resolve it:
I run the terminal CMD with administrator right. How to do that ? Right click on the terminal and click on Run as administrator.
I install django by doing: python -m pip install django. You can check that django is really installed by doing: python -m pip freeze. If you see django in the list, then django is installed.
Finally, I create my django project by doing: django-admin startproject project-name.
NB: For step 3, you don't need the terminal with administrator right.

Categories

Resources