[Fixed]-Why can't I run django app outside visual studio?

1👍

Have you ever used virtualenv before? It looks like VS creates an env for you. When you’re running your project outside of VS you’ll need to make sure your environment outside VS matches your VS requirements. This is simple enough to do.

First if you don’t have virtualenv installed yet, run

pip install virtualenv

Then create a new folder and cd into the new folder from the command line. Create a new virtualenv by running

virtualenv env

This creates a new virtual environment called ‘env’. You’ll see a new folder created in your active directory called ‘env’. This stores all of the libraries you install to this environment.

Next you need to activate your environment by running:

env\scripts\activate

You’ll see (env) or whatever you named your env at the beginning of your command prompt like this:

(env) C:\path\new_folder>

Now you’re using a fresh env that should only have python installed. You can run pip freeze to see what is currently installed on the environment. Should be a (mostly) empty list. Now you can install django with

pip install django

Run pip freeze again and you should see the latest version of django installed in your environment. You’ll want to make sure you’re using the same version of django that Visual Studios is using. The easiest way to test this is to go into the VS command line tool and run pip freeze. You’ll see a full list of requirements used by the project. There should be a requirements.txt file somewhere in the project directory, if not create a new one again from the VS command line run:

pip freeze > requirements.txt

This just creates a new txt file with the same list you saw running pip freeze. All Python/Django projects [SHOULD] have them. That way anyone who wants to run a project should be able to create an (as close to) identical environment as you have on your local machine.

The last thing you need to do is install any other requirements you have from your VS project. We already created a fresh requirements.txt file so now from your windows command line (make sure your env is still activated) change directory to the one containing the requirements.txt file and finally run:

pip install -r requirements.txt

This will install all of the packages needed to your new virtual env. If you run pip freeze from your windows command line, you should see the exact same list you see if you run it from VS. Now you should be able to cd to the directory containing manage.py and run your django project without a problem.

This can be a little overwhelming in the beginning, but as your start working with python/django more outside of VS it becomes second nature. It makes sense to create a new virtualenv for every project you work on to keep all requirements seperate. That way you don’t run into issues down the road.

0👍

I can assume that you are using different interpreters, which means that the 2nd one doesn’t have django installed. Kindly check or uncolor the path on your screenshots.

Also you might want to consider PyCharm for Django development, it is more convenient and has more features.

👤Hisagr

0👍

It looks like django is installed within the virtual environment of Visual Studio. To make it work globally you should install django globally on your computer. To do it, you should follow this steps:

  1. Install pip: https://pip.pypa.io/en/stable/installing/
  2. Install django via pip: pip install django

After that django will work without any installed IDEs.

Leave a comment