I sincerely apologize for the noobish dribble thats about to come out here:
Okay so I am following along with a youtube tutorial using terminal/django/komodo edit to make a simple website. This is my first time really using terminal, I am having issues. I have read up on the terminal and searched this site for my question but to no avail. Im hoping someone will take the time to answer this for me as it is most infuriating. This is my first time working with virtual env's as well.
So my question is, How do I uhmm, I suppose "save" my virtual env settings?
So I have set up a new virualenv. Downloaded django and started up my server so I can see things such as the admin page, log in page, from the internet page. Things go as they should along with the tutorial until it comes time to eventually turn off my computer.
When I reload the virtualenv I cannot run the server, it gives me: Import error, no module named django.core.management.
I use pip freeze and it shows that django is no longer installed.
If trying to reinstall django it gives a long block of error messages.
All the work done within the virtualenv file is still visible for the komodo edit pages however, but it seems the terminal does not want to work properly. My only option thus far has been to completely remake a virualenv, re-set it all up with the proper imports, files, django and restart the project.
so my questions are:
how do I save my terminal and/or virtualenv settings?
What do I need to do before logging off to ensure I will be able to continue with my project?
Lets say I am going to continue with my project, How do I start up the project again via terminal? Is that where I am going wrong? I've assumed up until now that I must go into terminal, start the server again and then from komodo edit continue with my project, but inside the terminal everything goes wrong.
Im not even explicitly saying I cannot continue with my project, I am more saying the terminal is not recognizing I had django installed within my virtualenv, and it is not letting me start the server again.
I have tried doing the research on my own, I am not one to sit back and wait for an answer but being completely new, this is baffling. I am sorry for the noob questions, feel free to link another answered question or website that has the answer.
Thank you all!!
Let's start from the beginning:
You are in your project folder eg /home/me/myproject
You create a new virtualenv, eg virtualenv /home/me/virtualenvs/myprojectenv
You activate the new virtualenv:
source /home/me/virtualenvs/myprojectenv/bin/activate
...this means that python and pip commands now point to the versions installed in your virtualenv
You install your project dependencies pip install django
You can ./manage.py runserver successfully
Now, the virtualenv has only been activated in your current terminal session. If you cd outside your project directory the virtualenv is still active. But if you open a new terminal window (or turn off the computer and come back later) the virtualenv is not activated.
If the virtualenv is not activated then the python and pip commands point to the system-installed copies (if they exist) and Django has not been installed there.
All you need to do when you open a new terminal is step 3. above:
source /home/me/virtualenvs/myprojectenv/bin/activate
Possibly the tutorial you followed got you to install virtualenvwrapper which is an additional convenience layer around the virtualenv commands above. In that case the steps would look like this:
You are in your project folder eg /home/me/myproject
You create a new virtualenv, eg mkvirtualenv myprojectenv
...virtualenv has already been activated for you now!
You install your project dependencies pip install django
You can ./manage.py runserver successfully
and whenever you start a new shell session you need to:
workon myprojectenv
in order to re-activate the virtualenv
Related
It's been a while since I posted anything here, and now my problems have become more advanced. For reference, I'm running on Win10.
I'm trying to use the Django framework. Python's been installed on my computer forever; it's the latest version, and it's installed on environment variables/added to system paths. I think those are enabled by default, but I did make sure that they were checked when I reinstalled it.
pip's been updated too. Django installed successfully. I managed to get all the way to installing my new project with django-admin startproject PROJECTNAME, but when I try to run python3 manage.py runserver, I'm getting this error:
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
This "Python was not found" error is the main problem that I'm trying to figure out. Nothing I do as a fix seems to get rid of it.
...Interesting. If I type in python --version, I get a response (Python 3.11.0). Here's what I get if I do pip freeze:
asgiref==3.6.0
Django==4.1.6
sqlparse==0.4.3
tzdata==2022.7
Python IDLE runs fine; all .py files run fine. Alright. So, with that in mind and in accordance to a lot of popular advice I've seen floating around, I added the Path to my Environment Variables. For space-saving reasons, my Python is installed on my D drive rather than C. I've made sure that there weren't any leftover Python files that conflicted.
I've uninstalled and reinstalled, double checked my installations, and Django is in D:\ProgramData\Python311\Scripts with pip and everything else I need. I did also disable the shortcut from Settings > Manage App Execution Aliases, but when I did that, I got this error:
python3 : The term 'python3' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
Based on some other things I've seen, I've also tried:
python manage.py runserver
python3 ./manage.py runserver
I ended up having to enable the options again just to use Python correctly. And I have been closing and reopening the command line whenever I make changes. Though it updates automatically to fit what I've been doing, I wanted to make sure I didn't overlook any possibility.
I even uninstalled Django for a third time, installed the virtual environment (which I realised I skipped), and did it all again. And then, after a long series of figuring out how to activate the virtual environment and enabling scripts to run, I'm finally in (venv) D:\PROJECTNAME, and I tried... to run it again.
I'm still getting the Python not found error. orz
I've been at it for two hours or so, and I feel like I'm running into a dead end. I think I've finally met my match here.
This is my first time using frameworks like this, and it's been a while since I've installed anything through command line on Windows. I'm not very great at this, so I know that I'm making some amateurish mistakes somewhere. Please let me know if I'm doing anything wrong or missed something obvious.
Please.... I just want to use Django....
Thanks in advance!
This question really is not proper for a PowerShell Q&A, but let see if the below helps.
If you are trying to run django from PS, then:
pip install django
django-admin startproject YourProjectNamehere
lastly, hit your dev server
HTTP://YourDevServerName:YourDevServerPortNumber
Also, be sure that you've activated your virtual environment, never assume it is until you test it. Of course, be sure to change directly to the root of your project.
Again, if you are using python, directly, then, it's more prudent to stay in your pristine python shell vs executing from a PowerShell consoler/terminal, thus eliminating any PowerShell particulars.
first download manually python on your computer , therefore install django from official website , www.python.org/downloads/, https://www.djangoproject.com/download/,
if you're on linux, pip install Django==4.1.6
after installing that use ,
django-admin
(Beginner) I am working with a virtualenv for a django project using my Macos terminal and Visual Studio Code.
Recently, my terminal disappeared where I was running all of my code and I do not know how to reenter my virtualenv or my project that I still have on Visual Studio Code. I have tried everything including:
source virtualenv/bin/activate
Or,
virtualenv/source/activate
Or,
cd my_project_name
and then trying to enter my virtualenv following this step (^) with the commands above.
Nothing is working and when I run "python manage.py runserver" it says there is no such file or directory. I am struggling and do not know how to continue my current project on django before getting back to my project and virtualenv.
Any help is greatly appreciated!
Visual studio code actually has a convenient way of keeping track of virtual environments. At the very bottom in the blue footer bar, click next to the refresh icon, and you should see whether it's available.
If it's really gone, don't sweat it. Virtual environments are completely expendable – it'll turn up. For now, just make another, and be sure you create it in your project's root directory this time.
You actually usually don't need to activate a venv with vscode. Just cd into the project root directory and open vscode with code .. The activated envionment will appear in that same spot in the footer.
Also, look into using a requirements.txt file, it'll save a lot of time if you keep misplacing the virtualenv.
And finally, it's best not to name your venv "virtualenv", name it something unique – otherwise it's guaranteed to confuse!
I have a big problem wih Django3: basically, whenever I type a command for manage.py, the prompt gets stuck and never carries on the command without even crashing.
I tried several times the commands "runserver" and "startapp" and waited beetween 10 minutes and an hour, but I never managed to run the server or create an app because I launch the command and the prompt gets stuck.
Strangely enough, to test things out, I created a project, an app and run the server immediately after I installed Django3 and everything worked fine, I even have that project on github. Then I shut down the computer and now nothing works, except my test project.
Also, I noticed in the Windows resource monitor that whenever I try to lauch a command suddenly Python processes start to appear and disappear uncontrollably, and since I never had an issue like this I'm absolutely clueless about everything. So... What's happening? Did someone have the same problem?
I have the lastest Windows update, the latest Python3 (I got it via Windows Store) and the Latest Django3 (I got it via pip). I'd include a stacktrace or some sort of log but, since nothing crashes, I suppose no log gets done: if I'm wrong please correct me, please!
EDIT: I tried uninstalling and installing Python but the problem is the same: I created a test project and I successfully ran the server and started a new app. Once again, I shut down the computer and now everything is broken again.
Ok, thanks to ansuman I discovered virtual environments and now everything works again.
If you're a noob like me and you want to know more go check the virtual environments documentation, it's extremely helpufl if you're experiencing (supposed) module conflicts like me. Also, it's such a cool feature: start using venv now, you won't regret it.
Here's what I did step-by-step:
opened the command prompt (duh);
installed venv via pip;
created a folder for the virtual environment with python -m venv site-venv;
went into the newly created directory;
created a new Django project with django-admin startproject site;
went into the subdirectory Scripts of site-venv and typed activate;
with the virtual environment activated, installed Django via pip;
started doing all the manage.py stuff.
Hope this help someone! Thanks again to ansuman.
I'm new to django and currently going through the main tutorial. Even though it was working earlier, when I do python manage.py runserver OR python manage.py -h OR with any other command, the shell doesn't output anything. Wondering what I'm doing wrong.
The problem is that the first line in manage.py breaks the file on windows.
The first line should look like this:
#!/usr/bin/env python
Removing it will fix the issue.
First, check if python is fully installed by typing "python" in a shell.
Then you should try python manage.py runserver inside your django project. If you don't have any django project, try creating one by typing django-admin.py startproject mysite. If nothing is displayed in your shell, you must have installed Django the wrong way.
Please refer to Django Documentation at https://docs.djangoproject.com/en/1.4/intro/install/
If you had your server running till one point and a certain action/change broke it, try going back to the previous state.
In my case there was an email trigger which would put the system in an invalid state if email doesn't go through. Doing git stash followed by selectively popping the stash and trying the runserver helps narrow down the problem to a particular file in your project.
Please try this.
Uninstall Python.
Go inside C drive and search Django. You will get many Django related files.
Delete every Django file. 😁 don't delete your Django files.
Install Python.
It's worked for me.
if you created a virtual environment then activate it. you can try this command(in virtual environment directory) if you're using windows os:
.\Scripts\activate
On Ubuntu works for my by running manage.py as script:
./manage.py runserver
Just stuck with the same problem. Found a solution that works, but tedious.
You need to know the location of the python.exe file in your computer. It is usually
C:/Users/USERNAME/AppData/Local/Programs/Python//python.exe
Modify as required and run the following in CMD,
C:/Users/USER1/AppData/Local/Programs/Python/Python38-32/python.exe
F:/mysite/manage.py runserver
Hope this works :)
if you are using Redis Server on Windows, check it out if Redis Server is running, I had same problem and realized my Redis Server was not running, I ran it and now my manage.py commands work fine.
The same happened with me also, but this issue is a minor one as it happens if you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
SO you should start from the beginning, uninstall Django first then,
create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
for e.g:
python3 -m venv tutorial-env
//This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it
Once you’ve created a virtual environment, you may activate it.
On Windows, run:
tutorial-env\Scripts\activate.bat
On Unix or MacOS, run:
source tutorial-env/bin/activate
Now,
In the command prompt, ensure your virtual environment is active, and execute the following command:
...> py -m pip install Django
NOTE:
If django-admin only displays the help text no matter what arguments it is given, there is probably a problem with the file association in Windows. Check if there is more than one environment variable set for running Python scripts in PATH. This usually occurs when there is more than one Python version installed.
Another solution, if you can, is to upgrade Django
pip install django --upgrade
Oftentimes one will get other unrelated issues to solve that are linked with the upgrade but once all is fixed the server should run just fine.
If you can't upgrade Django, this problem also happens when the code was built using Python 2.x and you're locally using Python 3.x.
The quicker fix in that case is to uninstall Python 3.x from your machine and make sure Python 2.x was added to the path. I've seen some developers setting up alias in PowerShell to have more than one version in the environment too.
I think the problem is in manage.py file (50%), check it with an another file that is correct.
I started working on my first python API hello world example on Friday on my VPS that is running Ubuntu 14.04. I used python3, created folder, virtualenv, activated it and then electricity went out and my connection broke.
I've left it there, and tried to continue where I stopped. Now, I can't find a way to "get into active" mode (to get that command-line view saying something like
(myprojectenv)user#host:~/myproject$
I've searched for "deactivate" somewhere in order to re-activate it again, but can't find anything such.
Any help understanding what is proper way of handling Virtualenv's is appreciated.
You have to activate you virtualenv again. In bash you have to source the activate file:
source myprojectenv/bin/activate
OK, if someone ends up in similar situation as I did.
Just "activate" your virtualenv again:
$source yourenv/bin/activate
That will re-activate it, and then you can "deactivate".