Virtualenv for django in VS Code not working, What am I doing wrong?

Viewed 1464

I've been following a tutorial on how to start using django and creating a virtual env on VS Code, but it doesn't work.. For what it shows in the tutorial, it's supposed to create a folder called ".vscode" with a json file inside called "settings.json" that contains the python path to the python interpreter.. But in my case, none of those files appear.. I THINK there might be sth wrong with the path where it creates the virtual env, but since I'm pretty new at this, I can hardly say.. This are the steps I followed:

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON>cd DJANGO

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO>mkdir storefront

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO>cd storefront

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv install django

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>code .

C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv shell

(storefront-vT5YbUlq) C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>django-admin startproject storefront .

(storefront-vT5YbUlq) C:\Users\Usuario\Desktop\Andres\Programación\5. Prácticas\3. PYTHON\DJANGO\storefront>pipenv --venv

** So the command prompt returns me this:

C:\Users\Usuario\.virtualenvs\storefront-vT5YbUlq

I'm supposed to copy that line to "Enter interpreter path" in VSCode, and after that it should create those vscode folder and json file.. but that doesn't happen, so I can't use the VS terminal to run the server

I'm going insane with this, I just can't understand where's the problem

I'd really appreciate if someone could help me with that

Thanks!

2 Answers
  • using terminal, mkdir to make a directory for your project

  • cd to your project folder/dir

  • type code . to open up vs code in this directory

  • Now you can use the integrated vs code terminal, please select the terminal, not the powershell

  • type pip3 freeze, it will show up all the installed packages and dependencies on global scope/system

  • but we gonna have a venv where we will install our necessary packages and dependencies

  • type python3 -m venv ./venv to create venv inside your current project folder, please ensure you are inside the folder before running this command [if you are not using python 3, then the command will be python -m venv ./venv]

  • to actiavte environment, on mac, run source ./venv/bin/activate on windows, run .\venv\Scripts\activate.bat [if it doesn't work, try to put your absolute path]

  • you can check what is installed inside venv using pip freeze, you will see nothing inside the venv

  • Now you can install django inside venv for your project

  • inside vs code, press shift+cmd+p and search for Python: select interpreter and choose venv [enter interpreter path>find>project folder>venv>Scripts>python.exe]

  • then you can notice the settings.json file inside .vscode folder in your project

  • to deactivate the environment, just type deactivate

Still, if you don't see the .vscode folder inside your project folder, you can create it manually.

  • Create a folder and name it .vscode

  • inside the folder, create setting.json file and paste the following code

    { "python.analysis.extraPaths": [ "./YOUR_PROJECT_FOLDER_NAME", ] }

for instance, system search for custom modules here in this extraPath if the module is not available in your system or user environment path.

You can try to follow this tutorial on the VSCode official docs.

You don't need to create the settings.json file, it was created by the VSCode, you can refer to here for the details.

And the interpreter-related information you can refer to here.

Related