Temporary file association for single cmd.exe session

前端 未结 4 1778
挽巷
挽巷 2020-11-30 08:13

I need to set association for .py files to be executed with specific python version. But I need to make this association only for single cmd.exe session (parallel sessions s

相关标签:
4条回答
  • 2020-11-30 08:36

    In Windows you can change file associations from the command line using the assoc and ftype commands (You can currently download a Windows Command Reference PDF file from here).

    You can see what file type is currently associated with .py files using the assoc command:

    > assoc .py
    .py=Python.File
    

    With that information you can then check to see what program is currently associated with the Python.File file type using the ftype command:

    > ftype Python.File
    Python.File="C:\Python2.6\python.exe" "%1" %*
    

    You can also use ftype to change the associated program:

    > ftype Python.File="C:\Python2.7\python.exe" "%1" %*
    Python.File="C:\Python2.7\python.exe" "%1" %*
    

    Associations set this way are persistent because they're stored in the Windows Registry. That means you will need to set or restore it to what you want before terminating the cmd.exe session. I'd suggest using one or more batch files for this purpose.

    cmd.exe itself accepts a /k parameter, which you could use to have it execute a batch file at start up that sets up the file association you want initially. You could then also provide a custom quit.bat that would restore it before exiting the cmd session.

    0 讨论(0)
  • Your best bet is probably going to be to set the PATH variable in the script and invoke python by writing python script.py. File associations are global and shared between processes. Environment variables are local to a process and that's why I suggest this solution.

    0 讨论(0)
  • 2020-11-30 08:45

    You can use the windows set command to set temp associations, set will last as long as the shell does unless attached to the system enviromental variables.

    0 讨论(0)
  • 2020-11-30 09:01

    Sure you can. You were very close to answer in your comment to Jakob's answer -

    If it is possible to change file association with environment variables - it will help, but it doesn't seem possible.

    It is possible. All you have to do is to use REG_EXPAND_SZ type of registry key and environment variable in the key's value. For example putting
    %python_home%\python %1 %*
    as the (Default) value of
    HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Python.File\shell\open\command
    key and setting its value to REG_EXPAND_SZ makes it possible to define what Python your Python files will be opened with. You decide by setting python_home environment variable and you can do this per command line session of course. Take a look at my answer to the question where in the registry does Windows store, with which program to open certain file types?

    Having said that there is now special tool for solving exactly this problem which I highly recommend. It's called pylauncher. From the docs:

    PEP 397 compatible launcher for Python under Windows. See http://www.python.org/dev/peps/pep-0397/ for PEP, http://www.red-dove.com/screencasts/launcher/launcher.html for screencast

    You can even skip the .py extension if you add it to the PATHEXT environment variable. See the question What environment variables will be used when calling an EXE from command line?

    0 讨论(0)
提交回复
热议问题