1

So when I try to find the path from the cmd with where azuredatastudio, I get the path. When I go in Python and do print(os.environ), I get many defined paths, but not this from the upper command in cmd.

How to get in this example azuredatastudio path from Python and where is it stored?

finefoot
  • 9,914
  • 7
  • 59
  • 102
waltexwq
  • 53
  • 2
  • 10
  • The path to `azuredatastudio` is not "stored" anywhere. Your question doesn't make much sense and the two answers as I write this comment are incredibly inefficient, OS specific, and prone to unexpected failures that the answers don't correctly handle. Why do you need to do this? I ask because in practice it is extremely rare. One of the better answers is https://stackoverflow.com/questions/377017/test-if-executable-exists-in-python. – Kurtis Rader Mar 01 '20 at 03:56
  • Thank you for your answer. My application is an extension to the `azuredatastudio`, so i need to find that path because i need to write some data relative to that directory. – waltexwq Mar 01 '20 at 22:45
  • 1
    What you want to do should be provided by an `azuredatastudio` API. You should not be creating files "relative to that directory"; i.e. the location of the `azuredatastudio` program. I'm guessing you are using the tool described by https://searchsqlserver.techtarget.com/definition/SQL-Operations-Studio. If that app does not provide such an API then presumably your extension should be stored relative to the user rather than the location of the `azuredatastudio`. Which could be different for each user. – Kurtis Rader Mar 02 '20 at 04:09
  • 1
    @KurtisRader If OP is currently already using `where` anyway, there is no need to write the Python code platform-independent, right? Also, the "best" solution for this question depends very much on the usecase - a "quick and dirty" solution via `check_output` is better than a hardcoded path, right? This might even be for a script running only one time. I also don't assume an XY problem every time I'm answering a question on here. However, I have edited my answer to include the link from your comment to show an example implementation how to get the paths from `os.environ['PATH']`. Thanks :) – finefoot Mar 02 '20 at 13:44

2 Answers2

0

A easy way to do this is:

import os

os.system("where azuredatastudio")

or if you want to save it in a variable.

import subprocess
process = subprocess.Popen("where azuredatastudio",stdout=subprocess.PIPE)
print(process.stdout.readline())
jizhihaoSAMA
  • 12,336
  • 9
  • 27
  • 49
0

The WHERE command is roughly equivalent to the UNIX 'which' command. By default, the search is done in the current directory and in the PATH.

Source: https://ss64.com/nt/where.html

So you'll have to explicitly look at the paths in the PATH environment variable: os.environ['PATH']. You'll find an implementation in this question here for example: Test if executable exists in Python?

Also, you should be able to just run the command from Python:

from subprocess import check_output
path = check_output(["where", "azuredatastudio"])
print(path)
finefoot
  • 9,914
  • 7
  • 59
  • 102