Problem using VSCode for OSGEO4W and Python development - python

I want to use VSCode as editor for my OSGEO4W development. I can write and debug and run my programm, but the "outline"-window (which is pretty necessary) on the left side is not working.
When looking at the Python-Output there is the following error-message:
InvalidPythonEnvironment: Could not get version information for 'C:\\OSGEO4~1\\bin\\python.exe': InternalError(u'The subprocess C:\\OSGEO4~1\\bin\\python.exe has crashed (EOFError(), stderr=).',)
I am using this batchfile for starting VSCode:
rem #echo off
set OSGWO4W_ROOT=C:\OSGeo4W64
REM Setting HOME is necessary for VSCode
REM in order to find the global .gitignore-file
SET HOME=H:\
call "%~dp0\o4w_env.bat"
#echo off
path %PATH%;%OSGEO4W_ROOT%\apps\qgis\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Qt4\bin
path %PATH%;%OSGEO4W_ROOT%\apps\Python27\Scripts
SET PYTHONPATH=%OSGEO4W_ROOT%\apps\python27\lib;%OSGEO4W_ROOT%\apps\python27\lib\site-packages;%OSGEO4W_ROOT%\bin;%PYSC_DIR%\Lib;C:\Users\<username>\AppData\Roaming\python\Python27\site-packages;
path %PATH%;C:\Program Files\Git\cmd
path %PATH%;C:\Program Files\Microsoft VS Code\bin
path %PATH%;C:\Program Files\dotnet\;C:\Windows\system32;c:\Windows;c\OSGeo4W64\bin;%OSGEO4W_ROOT%\
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT:\=/%/apps/qgis
set GDAL_FILENAME_IS_UTF8=YES
rem Set VSI cache to be used as buffer, see #6448
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%\apps\qt5\plugins
path %PATH%;%OSGEO4W_ROOT%
start /d "C:\Program Files\Microsoft VS Code\" Code.exe
When using VSCode with the standard-python-installation everything works fine.
VSCode-Version: 1.47.2
Python: 2.7.16 64bit

Related

Is it possible to change default directory for WinPython Command Prompt.exe?

When I run "WinPython Command Prompt.exe", the working directory defaults to the scripts directory of the installation.
Creating a shortcut to run the exe with a specific working directory does not seem to have an effect.
Is it possible to have the directory after running "WinPython Command Prompt.exe" be something other than the scripts directory?
The exe runs cmd.bat in the scripts directory.
It doesn't seem like the exe is passing "start in" or any parameters along.
To change the default start directory I modified cmd.bat instead:
#echo off
call "%~dp0env_for_icons.bat"
cmd.exe /k cd /d d:\python
Update:
second possibility is to pass all arguments in cmd.bat:
#echo off
call "%~dp0env_for_icons.bat"
cmd.exe /k %*
After that you can create a customized shortcut with the Target:
"C:\WPy64-3870\WinPython Command Prompt.exe" cd /d d:\python
on latest version, drag&drop your directory over the icon, it should make your day.

Batch file to execute python script

I'm trying to write a batch file to execute a python script and not having much luck. I tried the following:
#echo off
SET path C:\"Program Files\python37\python.exe"
C:\"projects\systemcheck.py -c systems.csv"
but get the following error:
C:\projects>nexus-script.bat Environment variable path C:\"Program
Files\python37\python.exe" not defined 'C:\"projects\systemcheck.py -c
systems.csv"' is not recognized as an internal or external command,
operable program or batch file.
It may be easiest to specify the full path to the Python executable. That way you don't have to worry about the PATH environment variable.
#echo off
"C:\Program Files\python37\python.exe" C:\projects\systemcheck.py -c systems.csv
If you absolutely need to set the PATH environment variable, you would do it like this:
#echo off
SET "PATH=C:\Program Files\python37;%PATH%"
python C:\projects\systemcheck.py -c systems.csv
Note that the path to the Python folder goes before the previous contents of PATH; this ensures that that is the Python that gets run if you have multiple Python installations on you computer.
You need to have your FULL path in quotes. Example: SET path to "C:\Program Files\python37\python.exe"
You don't need to SET the path, you can do what Jack mentioned in a past comment.

How do i append to the Maya PYTHONPATH?

I am working through Practical Maya Programming, and trying to set a 'development root' on my PC, I have followed the instructions (below) exactly but it is not working - At the point where I type 'mayapy.exe' I get the warning "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file."
From the book:
Let's decide where we will do our coding. We'll call this location the development root for the rest of the book. To be concise, I'll choose C:\mayapybook\pylib to house all of our Python code.
Create the development root folder, and inside of it create an empty file named minspect.py.
Now, we need to get C:\mayapybook\pylib onto Python's sys.path so it can be imported. The easiest way to do this is to use the PYTHONPATH environment variable. From a Windows command line you can run the following to add the path, and ensure it worked:
> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
> mayapy.exe
>>> import sys
>>> 'C:\\mayapybook\\pylib' in sys.path
True
>>> import minspect
>>> minspect
<module 'minspect' from '...\minspect.py'>
EDIT
This is how it is working for me at the moment:
PS C:\Users\Me> set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
C:\mayapybook\pylib : The term 'C:\mayapybook\pylib' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:29
+ set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (C:\mayapybook\pylib:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
So the code from the book is not working but the code from the post by DrHaze seems to:
PS C:\Users\Me> setx PATH "%PATH%C:\mayapybook\pylib\"
SUCCESS: Specified value was saved.
But when I run the Maya Python Interpreter and check if C:\mayapybook\pylib\ is in sys path it returns false:
>>> 'C:\\mayapybook\\pylib' in sys.path
False
This error "'mayapy.exe' is not recognized as an internal or external command, operable program or batch file." means that the path where mayapy.exe is located is not included in the PATH environment variable. Your system tries to look in all the folders included in the PATH variable but can't find an executable called mayapy.exe.
The executable mayapy.exe is generally located here:
C:\Program Files\Autodesk\Maya(VERSION)\bin\mayapy.exe
on my computer it's located here: C:\Program Files\Autodesk\Maya2014\bin\mayapy.exe
To add the mayapy.exe location to your path, use one of the following commands:
setx PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" if you
want to change it permanently
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\" only works for the current instance of the cmd session.
EDIT
The error you show in your edit is the source of the problem. Windows failed to set the environment variable PYTHONPATH. Hence, when you execute 'C:\\mayapybook\\pylib' in sys.path it returns False. sys.path is in fact containing the value of PYTHONPATH. That's why it returns False.
Now, why did it fail to set this environment variable?
First I can see that you are using Windows Powershell, keep this in mind.
The command I gave you is:
set PATH "%PATH%;C:\Program Files\Autodesk\Maya2014\bin\"
You wrote:
set PYTHONPATH=%PYTHONPATH%;C:\mayapybook\pylib
What it should be:
set PYTHONPATH "%PYTHONPATH%;C:\mayapybook\pylib\"
The syntax is a bit different and this last command should work.
As an explaination, your book gives you some commands to type in the vintage/old-style windows terminal: cmd.exe
As you are using Windows Powershell, some commands might have a different syntax.
Now what you can do is:
Use cmd.exe (Right click on the title bar -> Properties to custom it)
Use Powershell but keep in mind that the syntax might be a bit different than in your book
If you are using Powershell, there are different commands and strategies for managing environment variables.
You can set a variable permanently with SetEnvironmentVariable
You can set for the current shell session with: $env:VARNAME =
VARVALUE
You can put the commands to set variables in a powershell profile
file.
I would go with the third option. All three are detailed below:
Option 1. To append the directory "C:\mayapybook\pylib\" to the existing
PYTHONPATH permanently for your account:
[Environment]::SetEnvironmentVariable("PYTHONPATH", $env:PYTHONPATH +";C:\mayapybook\pylib\", "User")
Option 2. To append the Maya bin folder to your PATH for only the current
shell session:
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
Option 3. Create a powershell profile and set your env vars there.
First you'll need to make sure powershell scripts can run locally:
Hit the windows button, start typing powershell, right click and open as administrator. Enter:
Get-ExecutionPolicy
If it is says Restricted or AllSigned, set it to RemoteSigned like so:
Set-ExecutionPolicy RemoteSigned
Close that shell. Now in another powershell (not admin) type:
cd ~\Documents
md WindowsPowerShell
cd WindowsPowerShell
New-Item -path "profile.ps1" -type file
notepad.exe profile.ps1
Paste into the file any commands you want to run whenever a new powershell is opened:
Write-Host "Hello From Your Profile"
$env:PATH += ";C:\Program Files\Autodesk\Maya2016\bin\"
$env:PYTHONPATH += ";C:\mayapybook\pylib\"
Now whenever you open a powershell, you'll get a silly message and those paths will be set. You can test by typing:
Write-Host $env:PATH
or to list all env vars:
Get-ChildItem Env:
You should now be able to run commands from the maya bin directory. For example, type: maya to start maya.
Some other useful powershell env var commands here.

Start Python from Windows PowerShell

I know this looks like a question answered thousands of time, but none of the traditional answers concerning the environment PATH are working.
I want to run the following in Windows 7, in Powershell:
python mycode.py
However, Powershell returns an error, stating that python not recognized as an applet, function, application,...
These are my path variables:
Users variables:
PATH
C:\Python27\Lib\site-packages\PyQt4;
C:\Python27;
C:\Python27\DLLs;
C:\Python27\Scripts;
C:\Python27\Lib\site-packages\vtk;
C:\Python27\gnuplot\binary;
C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
C:\Program Files (x86)\pythonxy\console;
C:\MinGW32-xy\bin;
C:\Program Files (x86)\pythonxy\swig;
C:\Program Files (x86)\pythonxy\gettext\bin
PATHEXT
.PY;.PYW
PYTHON_INCLUDE
C:\Python27\include
PYTHON_LIB
C:\Python27\libs\python27.lib
and System variables:
PATH
C:\Python27\Lib\site-packages\PyQt4;
C:\Python27;
C:\Python27\DLLs;
C:\Python27\Scripts;
C:\Python27\Lib\site-packages\vtk;
C:\Python27\gnuplot\binary;
C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
C:\Program Files (x86)\pythonxy\console;
C:\MinGW32-xy\bin;
C:\Program Files (x86)\pythonxy\swig;
C:\Program Files (x86)\pythonxy\gettext\bin;
C:\WINDOWS\system32;
C:\WINDOWS
I tried the following:
$env:Path = $env:Path + ";C:\Python27\"
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")
without success. (I tried starting a new Powershell session, of course, and even tried to reboot my PC)
Could it be that PowerShell doesn't properly read the path variable, or I don't have some permission? I am lost, especially knowing that this work fine on another Windows 7 install. I note that typing:
python.exe
...opens a Python terminal as expected.
Edit : Ok I tried the following test.py code :
# -*- coding: utf-8 -*-
print "Hello"
input()
python.exe test.py
open a new terminal with "Hello" in it and wait for my input
but I don't want that, I expect the normal behaviour, with "Hello" printed in PowerShell, error message in PowerShell and so on.
Edit2 : I noticed that the "Path" variable given in PowerShell by:
Get-ChildItem Env
Is not equal to the one in the W7 options "System -> Advanced System settings -> Environment variable" . it was only :
;C:\Python27
Like if my previous command line
[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27")
just wrote in it. Fixing this didn't solve my problem either.
Running any executable using Powershell (not just Python.exe) is possible using multiple approaches, but what has worked best for me is iex.
The basic steps I follow are:
Find the path to executable
Construct command string
Run iex on it. Use & to account for spaces in file paths.
To find the executable, I generally use get-command. This searches PATH:
if (get-command curl.exe) {
$exePath = "curl.exe"
}
$Cmd = '"'+ $exePath + '"' + ' args'
iex "& $curlCmd"
Hope this helps.
Adding "C\Python27" in "System -> Advanced System settings -> Environment variable" to the system variables solved my issue. For me it was only in user variables.

Unable to launch a python script through my batch file

I have a batch file that's supposed to set PATH/PYTHON path variables and then invoke my python script (myscript.py), that's designed as an interactive console. I tried with the following:
#echo off
setlocal
if not defined PYTHONHOME (echo warning: PYTHONHOME environment variable is not defined. Using C:\Python24 by default.
SET PYTHONHOME=C:\Python24
if not exist "C:\Python24" ( echo warning: C:\Python24 does not exists. Please specify PYTHONHOME variable manually.))
color 1e
set PYTHONSTARTUP=%~dp0%myscript.py
set PYTHONPATH=%~dp0;%PYTHONPATH%
path %PYTHONHOME%;%PATH%
set PATHEXT=%PATHEXT%;.PY
cd %~dp0%
cmd.exe /k title Interactive Python Console 1.0
cls
%~dp0%myscript.py"
:done
endlocal
Before setting the colorpair (1e)for the console, I have appended directory containing myscript to path, python path and python24 is set as python home.
My problem is:
I am able to change the default font/background color of console, set the current window's title, but then neither cls (clearscreen) works, nor does my script is invoked. At the console, my pwd is script's directory. But when i enter 'python' at the prompt, myscript is invoked and I can see interactive console of my script.
Anything missing from batch, that would automatically clear off the console after setting color/title, and invoke myscript.py?
This doesn't really have anything to do with Python. cmd /k doesn't "set the window title", it starts a new command shell and leaves you in it, thus stopping your script midway through. Why don't you just do title My New Title? There's no need to use cmd.

Categories