Shutil which seems to be not working in my venv

My program is developed using a virtual environment in VSCode.
Before launching python main.py in VSCode I activate the venv.
Inside the program I use:

if not which(’mysql’) : 
    print('not installed')

to check the mysql server is installled. With mysql installed it prints

“not installed”

Using python in a terminal outside of venv with

>>>from shutil import which
>>>which('mysql')

it returns the path to the installed mysql (in my case

‘C:\Program Files\MariaDB 11.1\bin\mysql.EXE’

Is it a normal behavior for which ?

What result do you get from os.environ['PATH'] in each case?

Also what is os.environ[ PATHEXT’] that is needed on Windows.

This isn’t a reliable way to test if a mysql server is installed. What you’re testing for there is the MySQL CLI client, which is completely independent. The server doesn’t need to be on PATH, since it’s not something an app normally needs to invoke.

One (the terminal in the system) shows

C:\Program Files\MariaDB 11.0\bin;

the other (the terminal in venv) not.

in venv

‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.CPL’

outside venv

‘.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC’

outisde venv

>>> from shutil import which
>>> which('mysql')
'C:\\Program Files\\MariaDB 11.0\\bin\\mysql.EXE'
>>>

which is in fact the client
in venv

>>> from shutil import which
>>> which('mysql')
>>>

Then do you know the reliable way ?

It depends on what you’re trying to achieve. If I want to see if there’s a MySQL server running, I would just try to connect to it; being installed is usually irrelevant.

I wonder is vscode is the problem with the environment.
Maybe it is controlling the env vars that are given to you venv.

What happens if you activate the venv from a CMD terminal instead of vscode?

It’s a relevant question. I just want to inform the user that they forgot to install the server. I am aware I could just try the connection but in this case I would not see what the reason is,server not installed, wrong database name or wrong password…

When the venv activated outside of VSCode it works while it doesn’t work when activated from VSCode

But how is that “reason” useful? And what if the user is connecting to something else?

And again, “mysql” is NOT the server. It is the interactive shell for MySQL commands.

Might be relevant here, the shutil tests are failing for me in vscode’s terminal but work in the regular Mac OS terminal with the same configs and without using any venvs:

e@Edwards-MacBook-Pro cpython % ./python.exe -m test test_shutil                                                     
0:00:00 load avg: 1.46 Run tests sequentially
0:00:00 load avg: 1.46 [1/1] test_shutil
test test_shutil failed -- Traceback (most recent call last):
  File "/Users/e/Github/cpython/Lib/test/test_shutil.py", line 1592, in test_tarfile_vs_tar
    self.assertEqual(self._tarinfo(tarball), self._tarinfo(tarball2))
AssertionError: Tuples differ: ('dist', 'dist/file1', 'dist/file2', 'dist/[31 chars]ub2') != ('._dist', 'dist', 'dist/._file1', 'dist/._[122 chars]ub2')

First differing element 0:
'dist'
'._dist'

Second tuple contains 6 additional elements.
First extra element 6:
'dist/file1'

- ('dist', 'dist/file1', 'dist/file2', 'dist/sub', 'dist/sub/file3', 'dist/sub2')
+ ('._dist',
+  'dist',
+  'dist/._file1',
+  'dist/._file2',
+  'dist/._sub',
+  'dist/._sub2',
+  'dist/file1',
+  'dist/file2',
+  'dist/sub',
+  'dist/sub/._file3',
+  'dist/sub/file3',
+  'dist/sub2')

test_shutil failed (1 failure)

== Tests result: FAILURE ==

1 test failed:
    test_shutil

Total duration: 871 ms