Currently taking CS50 Web Programming with Python and Javascript. I'm on the Week 3 Django lecture and trying to follow along but I'm running into trouble while trying to run python manage.py run server.
I'm getting the "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?" error.
I'm using Windows, Django IS installed and I've reinstalled it multiple times. I've found a workaround by following the steps from https://www.youtube.com/watch?v=eJdfsrvnhTE&t=296s to set up a virtual env and can proceed after that, but in the lecture Brian doesn't need to setup a virtual env? It just loads straight through for him?
Yes I have scoured through reddit, stackoverflow, youtube, and other articles online before asking this here. It's not too much trouble to do so but I'm just wondeirng why he didn't need to make a virtualenv and if I'm actually going to have to make a virtual env for every Django project going forward? Is it because things have changed with python/pip/Django?
I would just find it more convenient if I could just run the run server command without having to run the extra 4 commands to setup the virtual env before being able to runserver.
Any info or guidance on this would be much appreciated. Thank you.
I have a workaround. I'm just wondering why in the lecture he didn't need to create a virtual env for it to work.
CodePudding user response:
No. You do not. In fact, you don't HAVE to use one at all, but it's a good practice to set up a virtual environment for each of your projects. Creating the virtual environment is generally the first step. The second step will then be to activate that virtual environment. You'll use pip to install modules like django into that environment. This has the effect of essentially creating your own little custom version of python under the directory you chose for your virtual environment which you will then use the modules from and add modules to (with pip) to build your project.
To launch the django server is a one line command from the terminal, usually something like ' python manage.py runserver '. I use Pycharm as my IDE which automates and simplifies this whole process and allows me to launch the server and open the browser with a single click. I would recommend downloading it and taking a look at it. I'm sure it would help get you a little further along.
CodePudding user response:
Run this command to check that Django is correctly installed and accessible from your current directory
python -m django --version
If everything is working correctly should see the version of your installation
Run this command from the outer "projectName" directory
python manage.py runserver
It would be more helpful to read the official documentation from Django.
Official Django ~5min tutorial
https://docs.djangoproject.com/en/4.1/intro/tutorial01/
Django Installation
https://docs.djangoproject.com/en/4.1/topics/install/
Please remember these instructions are not system, editor, or IDE specific you can run the commands from your terminal and use any system, editor, or IDE