I want to set up Visual Studio command line build environment with python so that I can use
subprocess.call(['msbuild', 'myapp.sln /p:Configuration=Release'])
to build my project with automation.
I have some other dependencies need to use a portable scripting language, so I chose python.
Is there any ways to do so? Thanks in advance.
Trial 1 (failure):
I tried to call vcvarsall.bat in python. However, it seems that it did not actually setup the environment for me.
import os
import subprocess
# Backup current environment vars
envvar_backup_list = [
'CMAKE_PREFIX_PATH',
'PATH'
]
envvar_backup_val_list = []
for i in envvar_backup_list:
envvar_backup_val_list.append(os.environ.get(i, None))
# Set CMAKE environment vars
os.environ['CMAKE_PREFIX_PATH'] = 'C:\\Qt\\Qt5.3.1\\5.3\\msvc2013_64\\lib\\cmake\\'
# Set MSVC environment path
current_path = os.getcwd()
os.chdir('C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\')
ret = subprocess.call(['vcvarsall.bat', 'amd64'], shell=True)
os.chdir(current_path)
subprocess.call(['msbuild'], shell=True)
subprocess.call(['cl.exe'], shell=True)
The command line instance that vcvarsall.bat and other commands run are not the same since each time you call subprocess.call() a new process is spawned, so that first call probably is initializing the build environment but then being killed. What you need to do is something like,
build = subprocess.Popen(['vcvarsall.bat', 'x86', '&&', 'msbuild', myProject.sln, '/p:Configuration=Release'])
build.wait()
if build.returncode != 0:
sys.exit("Build failed.")
PS: Reading vcvarsall.bat path from registry would be more portable, see the key SOFTWARE\\Microsoft\\VisualStudio\\<version>\\ShellFolder.
Related
I am working in Windows 11, Python 3.10, Mu 1.1.0. I followed the instructions on ATBS to create a batch file to run a script from the WIN + R launcher. However in CMD the error A(below) pops up. I tried following instructions from the documentation on python.org, and in Appendix B of ATBS to edit environment variables. I added the following to PATH env variables: C:\Users\19139\MyPythonScript; C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk; and C:\Users\19139\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Python 3.10\Python 3.10 (64-bit).lnk\Scripts. Is there some issue with the fact that the PATH includes start menu? All I am able to call from CMD is py --version. When I try and directly call a python script I get Error B. I disabled the "App Installer"(s) under App Aliases for Python.exe and Python3.exe but no change in error. I can call py --version, but I am beyond the scope of my ability to figure out what to do. I am unsure of next possible step to problem solve, suggestions welcome.
Error A:
`enter code here`Python path configuration:
PYTHONHOME = (not set)
PYTHONPATH = (not set)
program name = 'C:\Users\19139\AppData\Local\Programs\Python\Python310\python.exe'
isolated = 0
environment = 1
user site = 1
import site = 1
sys._base_executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.base_prefix = ''
sys.base_exec_prefix = ''
sys.platlibdir = 'lib'
sys.executable = 'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python.exe'
sys.prefix = ''
sys.exec_prefix = ''
sys.path = [
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\python310.zip',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\Lib\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310\\DLLs\\',
'C:\\Users\\19139\\AppData\\Local\\Programs\\Python\\Python310',
]
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
Traceback (most recent call last):
File "C:\Users\19139\AppData\Local\Programs\Python\Python310\Lib\encodings\__init__.py", line 31, in <module>
ModuleNotFoundError: No module named 'codecs'
Error B
C:\Users\19139>python "C:\Users\19139\MyPythonScript\mClip.py"
Python was not found; run without arguments to install from the Microsoft Store, or
disable this shortcut from Settings > Manage App Execution Aliases.
First of all, unlike ubuntu/ linux Windows doesn't detect python on its own. That's why we need to specify the path manually, to make it aware of python.
Is there some issue with the fact that the PATH includes start menu?
Yes, there is issue there. PATH for python doesn't include Start Menu generally.
You are looking at the wrong place or have installed python package at the wrong location. Find the correct python package location.
Python PATH looks something like this:
C:\Users\user_name\python_installerpkg_name
For example, if you have installed python using a python installer pkg like anaconda/ mini-conda, you can use its command prompt to find the location via where python. Then use the location to set the PATH in environment variables. the PATH would look like: C:\Users\user_name\anaconda
Error B
Until you configure the correct path in the environment variables you won't be able to use the command python filename.py from the command line.
Created an executable using pyinstaller on ubuntu 16.04 and trying to run it on SuSe 12 SP4 gives error at a certain portion of the code.
The code works like this:
Its a flask app that receives the input from user over web
Process those inputs and creates a .sh script and run that shell script
Reads the output from the shell script and present it to the web as a return render
The executable was successfully created on the ubuntu machine and works successfully and no issues seens but when I use this executable on SuSe12 SP4, it starts but when it reaches the code where it runs the bash script, it throws the following error:
sh: /tmp/_MEI369vhy/libreadline.so.6: no version information available (required by sh)
I am really tired of looking for solutions and have done the following so far:
Tried both --onefile and --onedir, no difference
Tried creating the executable on SuSe12 sp4 itself but it throws a different error regarding subprocess not being found
Tried finding the libreadline.so on Suse with no luck
Tried creating an env on ubuntu 14 but too many dependencies errors
I'm finally out of suggestions and could use some help here. If you can please assist.
Environment
Python 2.7.12
Ubuntu 16.04
SuSe12 SP4
Pyinstaller 3.6
P.S. The code as a raw python code works flawlessly on SuSe 12 SP4 if i create proper build environment
So, I finally solved it with some assistance from Rokm. The warning message above was not causing any issues but it was due to the environment variable not being passed to subprocess.
In order to solve this issue, I simply did the following:
###Add the following code to your existing code
env = dict(os.environ) # make a copy of the environment
lp_key = 'LD_LIBRARY_PATH' # for GNU/Linux and *BSD.
lp_orig = env.get(lp_key + '_ORIG')
if lp_orig is not None:
env[lp_key] = lp_orig # restore the original, unmodified value
else:
# This happens when LD_LIBRARY_PATH was not set.
# Remove the env var as a last resort:
env.pop(lp_key, None)
Next, add the env variable to subprocess Popen command. Here is the full code for reference. This code will give you the output of the command and also return code aka exit code of the command. Also, you don't have to use any shelix or any other things to run it, simple .strip() command will do it for you. Hope you guys find it useful, enjoy. !!
from subprocess import Popen,PIPE,STDOUT
env = dict(os.environ) # make a copy of the environment
lp_key = 'LD_LIBRARY_PATH' # for GNU/Linux and *BSD.
lp_orig = env.get(lp_key + '_ORIG')
if lp_orig is not None:
env[lp_key] = lp_orig # restore the original, unmodified value
else:
# This happens when LD_LIBRARY_PATH was not set.
# Remove the env var as a last resort:
env.pop(lp_key, None)
cmd = raw_input('Enter your command:')
out = Popen(cmd.split(),stderr=STDOUT,stdout=PIPE, env=env)
t, y = out.communicate()[0],out.returncode
print('output : ' + str(t))
print ('Return Code : ' + str(y))
P.S. Unfortunately, cmd.split() will fail in some cases, ie when an argument has spaces, like cmd='/usr/bin/ls "/home/user/my directory"' will fail with cmd.split(). In that case, cmd = shlex.split(cmd, posix=True) will work better. But shlex.split() will fail when stdout is captured, hence there is no allrounder solution IMHO
I have a python script for a C++ project that uses CMake as the build system. I want to use the CMake Ninja generator instead of the Visual Studio generator. However, the Ninja generator expects the environment to be set up as performed by the vcvarsall.bat batch file provided with the Visual Studio installation.
It's easy if you do everything manually because you can just call the vcvarsall.bat file, get a command prompt with the correct environment set up and then you can fire the cmake -G Ninja command from there.
Things become more difficult when trying to use a python script that has not been started in such an environment. How do I call the vcvarsall.bat file programmatically from python, followed by a cmake call in that environment?
I assume that the only way is to just fire one subprocess from python. But I cannot come up with a parameter list that would do the job.
I'm lucky enough to have found an answer to my own question.
Here is the right python call to be used:
src_dir = ... # set your source dir here
build_dir = ... # set your build dir here
# retrieve visual studio installation path using vswhere.
# See: https://github.com/Microsoft/vswhere
vswhere = os.path.join(os.environ['ProgramFiles(x86)'],
"Microsoft Visual Studio",
"Installer",
"vswhere.exe")
# get installation path of Visual Studio 2017
vspath = subprocess.Popen([vswhere, "-property", "installationPath", "-version", "[15,16)"],
stdout=subprocess.PIPE).communicate()[0].rstrip()
# build path to vcvarsall.bat file
vcvarsall = os.path.join(vspath, "VC", "Auxiliary", "Build", "vcvarsall.bat")
# vcvarsall.bat changes the current directory to the one specified
# by the environment variable %VSCMD_START_DIR%
my_env = os.environ
my_env["VSCMD_START_DIR"] = build_dir
# set up the environment and then call cmake with Ninja generator
subprocess.call('call "' + vcvarsall + '" x64 && cmake -G Ninja "' + src_dir + '"', shell=True, env=my_env)
I'm following this tutorial here to make a snakegame in pygame. Here is my setup.py code:
import cx_Freeze
executables = [cx_Freeze.Executable("snake.py")]
cx_Freeze.setup(
name="Snake",
options={"build_exe":{"packages":["pygame"], "include_files":["apple.png","Aenemy.png","bomb.png","cherry.png","enemy.png","fire.png","iceimg.png","snakebod(2).png","snakebod.png","Explosion.wav","Explosion2.wav","jump.wav","Pickup_Coin.wav","Powerup.wav","openingsong.mp3","highscores.txt",]}},
description = "Snake Game made in python with pygame.",
executables = executables
)
When I try to build that in the command prompt I get this error
C:\Users\Accounts\Documents\snake>C:/Python35/python setup.py build running build running build_exe File "C:\Python35\lib\site-packages\cx_Freeze\hooks.py", line 597, in load_tkinter tclSourceDir = os.environ["TCL_LIBRARY"]
File "C:\Python35\lib\os.py", line 681, in getitem raise KeyError(key) from None KeyError: 'TCL_LIBRARY'KeyError: 'TCL_LIBRARY'
and it doesn't build. Does anyone know how to fix this? Thanks
I was getting a similar error and solved it successfully this morning!
Add following lines to your setup.py code
import os
os.environ['TCL_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tcl8.6"
os.environ['TK_LIBRARY'] = "C:\\Program Files\\Python35\\tcl\\tk8.6"
you may need to replace C:\Program Files\Python35\tcl\tcl8.6 and C:\Program Files\Python35\tcl\tk8.6 with the exact path of tcl8.6 and tk8.6 on your system respectively.
Assuming you are using windows operating system:
Add 2 environment variable named TCL_Library and TK_Library in your machines user and system variable
TK_Library
TCL_Library
Env variable
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.