2

I have a python script that downloads projects out of git. This script runs inside a virtual enviroment ( for example: d:\robot_fw\venv\cloner\Scripts\python.exe) After cloning the repo I itend to create a new virtual enviroment, with all depencies needed for the cloned project.

From the python script I used

subprocess.run([sys.executable, "-m", "venv", self.virtual_dir])
# sys.executable is d:\\robot_fw\\venv\\cloner\\Scripts\\python.exe
# self.virtual_dir is 'd:\\itxvenv\\venv\\myvenv'

The call was successfull. But when I check the directory I find: d:\itxvenv\venv\myvenv\Scripts and in this dir only python.exe and pythonw.exe The venv could not be activated

If I do the same call from inside a terminal ( terminal insode pycharm)

(cloner) PS D:\Development\Dev\cloner> d:\\robot_fw\\venv\\cloner\\Scripts\\python.exe -m venv d:\\itxvenv\\venv\\myvenv

I found

enter code here


ode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---          21.02.2023    21:16           2066 activate
-a---          21.02.2023    21:16            995 activate.bat
-a---          21.02.2023    21:16          24167 Activate.ps1
-a---          21.02.2023    21:16            393 deactivate.bat
-a---          21.02.2023    21:16         107888 pip.exe
-a---          21.02.2023    21:16         107888 pip3.10.exe
-a---          21.02.2023    21:16         107888 pip3.exe
-a---          21.02.2023    21:16         266616 python.exe
-a---          21.02.2023    21:16         254840 pythonw.exe

This could be actiavted.

So whats going wrong here?

sinoroc
  • 18,409
  • 2
  • 39
  • 70
gthorsten
  • 21
  • 4
  • just for the record - I tested it as is under Linux, it just works. I think it is possible that the venv creation gets caught, even if indirectly, by Windows "a file can only be open by one process at a time" default policy when trying this. – jsbueno Feb 21 '23 at 20:32

1 Answers1

4

You don't need to use subprocess. Create the virtual environment directly from the venv module:

import venv

venv.create(self.virtual_dir)
Corralien
  • 109,409
  • 8
  • 28
  • 52
  • ok, did not now that this exists. Will try this in the morning – gthorsten Feb 21 '23 at 20:34
  • if I use this venv.create(self.virtual_dir, with_pip=True) I received raceback (most recent call last): File "C:\Users\thorsten.guddack\AppData\Local\Programs\Python\Python310\lib\subprocess.py", line 526, in run raise CalledProcessError(retcode, process.args, subprocess.CalledProcessError: Command '['d:\\itxvenv\\venv\\myvenv\\Scripts\\python.exe', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1 – gthorsten Feb 21 '23 at 20:58
  • Does `python3-setuptools` is installed in your current environnment? Check https://stackoverflow.com/questions/69594088/error-when-creating-venv-error-command-im-ensurepip-upgrade-def – Corralien Feb 21 '23 at 21:13
  • Switches off the PC now. But of i did IT from the cmd line, python - m venv... IT works. Does this means hat the mentioned packages were installed? – gthorsten Feb 21 '23 at 21:27
  • yes setup tools is installed. – gthorsten Feb 22 '23 at 06:19