1

I have a python application that I installed in WSL ubuntu and I an trying to link my VSCode ( from windows to it). I am having some trouble accomplishing that thou and I hope I can be assisted.

SITUATION

In running a script I need ( in VSCode terminal) I get the following error. 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? (Please note I am in a virtual enviroment).

So then I return to Ubuntu and I run python3 -m django --version which returns 3.2.8.

I go back to VSCode virtual environment and run the same script that shows me /mnt/h/Documents/Projects/React/Myplace/venv/bin/python: No module named django

So I think ok maybe I can install it and I run sudo pip install Django==3.2.8 which returns

Requirement already satisfied: Django==3.2.8 in /usr/local/lib/python3.8/dist-packages (3.2.8) Requirement already satisfied: sqlparse>=0.2.2 in /usr/local/lib/python3.8/dist-packages (from Django==3.2.8) (0.4.2) Requirement already satisfied: asgiref<4,>=3.3.2 in /usr/local/lib/python3.8/dist-packages (from Django==3.2.8) (3.4.1) Requirement already satisfied: pytz in /usr/local/lib/python3.8/dist-packages (from Django==3.2.8) (2021.3)

Given this, I am currently unsure how to proceed. I think I should also mentioned I created my virtual enviroment using sudo pip3 install virtualenv

virtualenv venv

source venv/bin/activate

Any assistance would be appreciated.

Regards

UPDATE

Django not installing in virtualenv I followed this question advice and did the django install in the bin of the virtual enviroment. but it just keeps telling me packages are missing.

Niana
  • 1,057
  • 2
  • 14
  • 42

1 Answers1

0

You almost got it. The problem is that your packages are not installed in your virtual environment path. Take a look at this message:

Requirement already satisfied: Django==3.2.8 in /usr/local/lib/python3.8/dist-packages (3.2.8)

As you can see, /usr/local/lib/python3.8/dist-packages is not /mnt/h/Documents/Projects/React/Myplace/venv/bin/python/dist-packages

To solve the problem, you need to:

  1. Create a venv (you did this already)
  2. Activate your venv: source <the name of your venv>/bin/activate (you did this too)
  3. Install your dependencies: pip install -r <requirements file path> or pip install <packages names>

Make sure your packages are installed in /mnt/h/Documents/Projects/React/Myplace/venv/bin/python/dist-packages by reading the output of the installing command. After confirming that, you should be able to run your code.

pazitos10
  • 1,641
  • 16
  • 25
  • But that's the problem, when I run step 3( Inside the Virtual enviroment) it doesnt try to install it inside of `/venv/bin/python/dist-packages/` it says it is found in `/usr/local/lib/python3.8/dist-packages` . Can I somehow alter the install path? – Niana Oct 09 '21 at 18:06
  • That's weird. To remove VSCode from the equation, try to run those commands using cmd, powershell or the wsl shell. – pazitos10 Oct 09 '21 at 18:13
  • Same thing, I deleted the virtual envi, went in the WSL shell, but once im in a venv, it doesnt know anything about Django – Niana Oct 09 '21 at 18:21
  • Make sure you're using the `pip` that's inside your `venv`. Check `pip --version`. It should tell you if `pip` is running from inside your `venv` or from your system. – pazitos10 Oct 09 '21 at 18:24
  • pip 21.2.4 from /mnt/h/Documents/Projects/React/Myplace/venv/lib/python3.8/site-packages/pip (python 3.8) – Niana Oct 09 '21 at 18:27
  • If I do ` sudo venv/bin/python -m pip install django` it installs, but then it says other things are missing. `ModuleNotFoundError: No module named 'graphene_django'` – Niana Oct 09 '21 at 18:30
  • That's the right one, when you run `pip install` it should install the packages inside the right directory according to the [docs](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#activating-a-virtual-environment) without using `sudo`. – pazitos10 Oct 09 '21 at 18:32
  • Have you tried creating the venv directly on windows instead of using wsl? – pazitos10 Oct 09 '21 at 18:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237991/discussion-between-niana-and-pazitos10). – Niana Oct 09 '21 at 18:34