1

i have this weird problem with python venvs. If i activate the venv and try to install packages via pip install -r requirements.txt the packages are getting installed to the global python and not in the venv.

i am using python 3.11.2 on Debian Bookworm.

I've googled now 2 hours and tried everything including everything in pip installing in global site-packages instead of virtualenv and nothing helped. The path are correctly set,which pip gives me the path to the venv pip, there are no aliases, i created configfiles but nothing helped.

Then i tried to install a package without the -r requirements.txt e.g. pip install numpy that worked and the packages were installed inside the venv. So what's the problem of pip not installing packages with the -r parameter to the venv?

BeRational
  • 46
  • 5
  • 1
    Can you post the contents of the requirements.txt? – spo Jul 13 '23 at 19:40
  • Do this first `pip freeze > requirements.txt` then do this second `pip install -r requirements.txt` – toyota Supra Jul 13 '23 at 20:16
  • I don't used ven. here is link: https://note.nkmk.me/en/python-pip-list-freeze/ – toyota Supra Jul 13 '23 at 20:21
  • @toyotaSupra, if you do `pip freeze > requirements.txt` in the root of the source directory, then it will overwrite the requirements.txt of the source with the current `pip freeze` result . Because of `>`. But OP wants to install the requirements.txt of the source. – Constantin Hong Jul 13 '23 at 20:23
  • @spo The content of the requirements.txt is: `--extra-index-url https://download.pytorch.org/whl/cu118 numpy==1.23.5 etc.` – BeRational Jul 13 '23 at 20:29
  • Please post the full content in your question. – Constantin Hong Jul 13 '23 at 20:33
  • 2
    What does `which pip` output? Are you sure you running the `pip` installed in the virtual environment? (If you are sure `python` is the correct interpreter, you can also use `python -m pip install -r requirements.txt` to install into the venv.) – chepner Jul 13 '23 at 21:02
  • 1
    after activating your env do `python -m pip install -r requirements.txt` – JonSG Jul 13 '23 at 21:08
  • 1
    You need to include exact and complete steps to replicate your scenario. Enough that *anyone* could follow them and be *guaranteed* to get the same error. (Added to the question, not as comments.) [mre] – MatBailie Jul 13 '23 at 21:19

1 Answers1

1

This works for me:

python -m pip install -r requirements.txt

I think you error is occurring because pip is still referring to the global pip. This command refers to python's (the venv python) pip instead of the global python's pip.

Ritvik S
  • 7
  • 2
  • may be pip is not installed in virtualenv. try `python -m ensurepip` to install `pip` in virtualenv. – Talha Junaid Jul 13 '23 at 21:16
  • No, my pip is referring to the pip in the venv. I've run `which pip` and it gave me the path to the venv. I also referred via absolute path to the venv pip - which gave me the same result – BeRational Jul 13 '23 at 21:17
  • Still only ever use `python -m` inside venv. It's one of those golden rules that avoids a million subtle problems like this. – Mike 'Pomax' Kamermans Jul 13 '23 at 21:23