I recently encountered an elusive bug, which eventually I tracked down to the fact that environment variables are not replaced in a certain string even though it is set by calling os.path.expandvars(original string).
Turns out, the problem is that expandvars() doesn’t expand variables who are enclosed within a string literal. I couldn’t find it documented anywhere but facts are, this is the case.
>>> import os
>>> os.environ['VAR1'] = 'Value1'
>>> s1 = "this is '%VAR1%'"
"this is '%VAR1%'"
>>> s2 = "this is %VAR1%"
'this is Value1'
It may be a good idea in some cases, but for me it was quite an annoyance.
So, how to overcome this?
Here’s a short explanation how to execute python unit tests during VSTS/TFS builds, without the need of plug-ins. The basic idea is to create a JUnit XML from your test results, and then have it published to the TFS build dashboard.
First, on the relevant build agent(s), install the pytest module. You can use the following command, or your favorite CM tool.
python -m pip install pytest
Next, edit your build definition in TFS, and add two build steps: one (Batch Script) for running py.test to generate a junit XML, and one (Publish Test Results) for publishing the xml to the test results page.
The full command line to run the tests is as follows (in this example, the unittest code is in test_MyApp.py):
python -m py.test --junitxml test-results.xml test_MyApp.py
Here is a sample build definition which includes only those two steps:
Batch Script Settings
Finally, queue the build, and you will able to see the test results in the build page:
Python, like many other popular OSS (Git anyone?), does not support symbolic links on Windows platforms , although they have been around since Windows Vista. Note, I am referring to actual symbolic links, not NTFS directory junctions or shortcuts.
I’m not really sure why is that. Maybe because they do not distinguish between different Windows revisions?
My current approach is to use the OS commands to create/delete symlinks. It’s not very elegant but it works without compatibility issues, unlike other solutions (calling win32 api through DLLs, manipulating file attributes, and other stuff you find in StackOverflow or tech blogs)
For example, to create a symbolic link of directories, one can use:
child = subprocess.Popen(['MKLINK', '/D', link, target], stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)
streamdata = child.communicate()
And check child.returncode for the result (and the output – stdout and stderr combined – available in the streamdata variable)
To remove a symbolic link to a directory, use the windows RMDIR command (os.rmdir or os.unlink won’t work)
When I first heard that Visual Studio 2015 is going to support developing with Python, I wasn’t sure how to react. Microsoft? Python? it just doesn’t seem related. I was very skeptical about working in Python within Visual Studio.
Recently I started a new pet project in GitHub to play around with VS integration to GitHub. So I figured, let’s include some Python code just to see how it feels working with Python in VS.
And it feels great, actually! Visual Studio users would feel right at home with IntelliSense-like auto completion and tooltips, debugging capabilities, unit testing with Test Explorer, advanced searching and editing, and more. I would dare comparing it to some of the best Python IDEs out there like PyCharm. GitHub integration also work seamlessly.
This Python support (a.k.a Python Tools for Visual Studio) is included in the free Visual Studio 2015 Community edition, and even available as open source on GitHub.
Microsoft! on GitHub! Times are changing, indeed.
Remember my previous posts about Microsoft’s bizarre issues? Well, this one sets a new record.
I was quite baffled at first.
cmd /c "C:\Program Files\7-Zip\7z.exe" C:\Temp\MyFile.7z
But this fails:
cmd /c "C:\Program Files\7-Zip\7z.exe" "C:\Temp\My Other File.7z"
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Turns out it’s an issue as old as Windows 2000.
The solution is to add another pair of quotes around the entire command:
cmd /c ""C:\Program Files\7-Zip\7z.exe" "C:\Temp\My Other File.7z""
Thank you, Microsoft.