pythonvisual-studio-codepythoninterpreter

Visual Studio Code Terminal is Not Using Correct Python Interpreter


I have Visual Studio Code (v 1.9) running on a Windows 10 machine. I want to use ESRI's Python interpreter and I selected that per this image:

Compiler Selection

After that, if I use the Run (right arrow) toward the top right of a python file then correct interpreter is selected and no issues. However, if I run the the python file using commands like in this screen:

Terminal Run of the Script

then some Python interpreter from Windows is selected. Please note: The error you see in this image of file not found is a fake error because the file really doesn't exist but it confirms that the Terminal is using wrong interpreter unlike the Run button.

I have tried to create a new Environment and selected the correct interpreter but still the Terminal is using the wrong interpreter . The Terminal does show the correct interpreter as in this image:

Terminal View of the Compiler

I prefer to run the Python script using the Terminal using keyboard short cuts instead of the Run button in the IDE. What can I do to fix this problem?


Solution

  • Here is how I ended up figuring out a solution:

    1. Created a Virtual environment in Visual Studio Code
    2. Pointed the environment in VS Code to the ESRI Python interpreter in Program Files location.
    3. Tried 'Python myfilename.py' in the terminal but it could still not find the required ESRI Module as referenced in the .py file in step #2. That told me something wasn't right.
    4. Opened 'pyvenv.cfg' file in the VS Code's Workspace and changed include-system-site-packages = false to true and saved the file.
    5. Tried 'Python myfilename.py' and this time the ESRI module was found. However, I get a warning like arcpy expects to run within a Conda environment but I think I can learn to live with this for now!

    UPDATE: Got rid of the warning by:

    Opening C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\__init__.py as Administrator in Nodepad++ and commented out the two lines:

    #print(msg.format(install_dir))
    
    #warnings.warn("arcpy expects to run within a Conda environment", RuntimeWarning)
    

    HTH someone!