0

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?

5
  • 1
    Use the full path to your preferred version, or create and activate a virtual environment with your preferred version, or add an alias to your preferred version. Commented Jun 27, 2024 at 16:57
  • I know I can use the full path in the Terminal but don't want to. In past, I had no issues using different interpeters in Terminal but this time I dont know what's going on. About Virtual Env, I already tried that route and I think loaded that but still the issue, as I said. Maybe I did something wrong then but I am new to that. Thank you. Commented Jun 27, 2024 at 16:59
  • 1
    If you type a python command yourself, then the only thing that matters is your PATH. VSCode's interpreter settings only matter when VSCode is running the program. Commented Jun 27, 2024 at 17:09
  • @jasonharper Good info! I still think in past I used different interpreter in VS Code but I could be wrong; it was a corp computer and can pre configured with various Python interpreters. This is my own laptop. Thank you! Commented Jun 27, 2024 at 17:12
  • I just managed to load the new environment which was pointing to the correct interpreter and this time running the Python filename.py does show the correct interpreter so that's progress! Still an error and I am looking into that! Commented Jun 27, 2024 at 17:16

2 Answers 2

1

You can try these points and see if the problem is solved.

  1. Use the shortcut Ctrl+, go to Settings, and input python.defaultInterpreterPath. See if the path is the correct path for ESRI's Python interpreter.
  2. Run $env:Path += ";C:\path\to\your\python\directory" in the terminal.Then input your path, note that it only works in the current terminal.
  3. If you are using a virtual environment, you can try to cd to your directory with the command .venv\Scripts\Activate.
  4. Use the shortcut key Ctrl+Shift+P to open the command panel, and select the command Extensions: Clear Extended Cache in the command panel to clear the extended cache data.
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you. I already have a solution/Answer. But thank you. I think of your suggestion, #3 might have worked and I did think about it but I didn't want to change Windows Env. #1: I had already confirmed that. #3: No needed because my .venv is already active except it needed a little tweak per my answer. #4: Not needed or made a difference, I think. Whatever solution I found was workaround. Works for me. I need to move on. But thanks. I will give you a Rep!
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.