Python Install On Windows – My Opinionated Approach

This is an opinionated approach to installing Python obtained from Python.org on Windows. Essentially, install per-user and keep Python out of the system or PATH environment variable. I’ll show you how to create some simple shortcuts to create Python console windows with your installed Python so you can easily used Python from the command line.

If you install Python on a Windows server used for something like builds or Flask websites and add Python to the PATH, you will quickly get stuck on a stale, aging, old Python version that can’t be updated because you (and everyone else) will be to busy and scared to worry about breaking existing apps on the machine.

Download and Run Setup

From the Python.org official Python site, then select Windows to view all download for Windows. Find the version you want, making sure you select a 64 bit version if that is what you want for a 64 bit machine.

Download the executable or web based installer for the version and bitness you want. For this example, the 64 bit 3.7.1 windows executable installer was used.

Running the install should show the following dialog:

Initial Setup Dialog

Make sure that Install launcher for all users (recommended) and Add Python 3.7 to PATH are both unchecked.

Click Customize Installation to get the next dialog shown below:

Optional Features

Make sure py launcher and for all users (requires elevation) are both unchecked.

Click Next to get the Advanced Options dialog.

Make sure that Install for all users and Customize install location are clear, and additionally, Associate files with Python (requires the py launcher) is unchecked and disabled.

Click Install to run the install.

Create Command Shortcut

I created a batch file, py37_env.cmd with the following contents.

@echo off
TITLE Python.org 3.7
 
REM add Python executable to PATH
set python_base=%LOCALAPPDATA%\Programs\Python\Python37
 
REM add Python executable and tools like pip.exe to PATH
set PATH=%PATH%;%python_base%;%python_base%\Scripts
 
REM switch to Unicode code page
chcp 65001

I then create a shortcut to the batch file with the following for the shortcut properties:

Assuming the batch file is in %USERPROFILE%\Desktop\Toolbox I create a short cut with the following for Target:

%comspec% /K "%USERPROFILE%\Desktop\Toolbox\py37_env.cmd"

And for Start In: someting like

%USERPROFILE%

Posted in Python, Uncategorized | Leave a comment

Multiple Oracle Javas On Windows – No Installs

Whether you want a portable JDK or you just don’t want to further muck up your Windows %PATH%, you can get a older or newer Java on your Windows machine without affecting other versions.

I found a link explaining how to do it here: https://www.whitebyte.info/programming/java/how-to-install-a-portable-jdk-in-windows-without-admin-rights, but in case it ‘goes away’, here are the steps:

So far this works for Java 1.7, should work with Java 1.8.

  1. Make sure you have 7-zip on your Windows. Download the installer from Oracle
  2. Open the executable installer with 7-zip and extract the single tools.zip file inside
  3. Extract the files from the tools.zip to where you want your JDK.
  4. From a Windows command prompt in the folder where you extract the tools.zip files, run the following:

    for /R %f in (.\*.pack) do @"%cd%\bin\unpack200" -r -v -l "" "%f" "%~pf%~nf.jar"

Posted in Java | Leave a comment

WiFi From Linux Rescue CD

Using Finnix 111 rescue CD:

Generate wpa_supplicant.conf configuration file:

# wpa_passphrase ssid passphrase > /etc/wpa_supplicant.conf

If you have any funky characters like ‘@’ in your passphrase, escape with a ‘\’.

Find driver
# wpa_supplicant –help

Using the correct driver:
# wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf -B

My driver listed was ‘nl80211’. Also, do not add a space between the options and their arguments.

Use ifconfig to find the network interface.

# dhclient wlan0

Posted in Uncategorized | Leave a comment

Make Bamboo Shell Task Run Using Bash

Bamboo will call shell tasks on *nix agents using sh as opposed to Bash. For Debian agents, this means dash not bash. So, how do we redirect to bash? I found this snippet:

if [ "$(ps -p "$$" -o comm=)" != "bash" ]; then
# Taken from http://unix-linux.questionfor.info/q_unix-linux-programming_85038.html
echo "RUNNING THIS PUPPY THE RIGHT WAT"
bash "$0" "$@"
exit "$?"
fi

Alas, though, looks like the link I copied and pasted from the post does not work. Here’s the post I snagged this from.

Posted in Bamboo, Bash, Snippets | Leave a comment

Hack Classpath In Gradle

I recently tried this to fix the Classpath in Gradle for compiling and testing. Works for compiling, bombs for the testing though. The Gradle plugin integration using jna-3.2.7 blows up during unit testing when the older API is not loaded. What a mess. At least this snippet should help for other scenarios.

def fixTheCp(fc) {
def jnaGoodJars = fc.filter {
it.name == “jna-4.2.1.jar”
}
return jnaGoodJars.plus(fc.minus(jnaGoodJars))
}

task(“fixCompileCp”) << { sourceSets.main.compileClasspath = fixTheCp(sourceSets.main.compileClasspath) sourceSets.main.runtimeClasspath = fixTheCp(sourceSets.main.runtimeClasspath) } task("fixTestCp") << { sourceSets.test.compileClasspath = fixTheCp(sourceSets.test.compileClasspath) sourceSets.test.runtimeClasspath = fixTheCp(sourceSets.test.runtimeClasspath) } compileJava.dependsOn fixCompileCp testClasses.dependsOn fixTestCp [/code]

Posted in Gradle, Snippets | Leave a comment

Hex Encode/Decode String In C#

The same code is posted all over the news groups and Internet, but here it is, easy as a snippet for myself and for you to use.

I don’t even get credit for this as what I have pretty much matches up to what other people have already posted.

This is much easier in Python.

Encode a byte array

public static string HexEncode(byte[] data)
{
    return BitConverter.ToString(data).Replace("-", string.Empty);
}

Decode a hex necoded string

public static byte[] HexDecode(string hexEncoded)
{
int numChars = hexEncoded.Length;
byte[] retVal = new byte[numChars / 2];
for (int i = 0; i < numChars; i += 2) { retVal[i / 2] = Convert.ToByte(hexEncoded.Substring(i, 2), 16); } return retVal; } [/code]

Posted in C#, Snippets | Tagged | Leave a comment

How To Clean SVN Checkout Directory

I just love

git clean -xfd

but you just don’t get that with Subversion. Instead from your Bash shell (or Cygwin) prompt you can do:

svn status --no-ignore | egrep '^[?I]' | cut -c9- | sudo xargs -d \\n rm -r

I got that last one from http://stackoverflow.com/questions/239340/automatically-remove-subversion-unversioned-files

Oh, and on that same post, for PowerShell users I found

function svnclean{
svn status | foreach { if($_.StartsWith("?")) {Remove-Item $_.substring(8) -Verbose}
}

I could resist saving these two snippets, I know I will need them.

Posted in Bash, PowerShell | Leave a comment

Validate Windows User On Network In C++

Need to validate and/or impersonate a user on a Windows domain? I Googled for this a bit but the API call is fairly simple so here is a quick snippet:

HANDLE htoken = nullptr;
DWORD dwError = ERROR_SUCCESS;
BOOL retVal = LogonUserW(L”JoeUser”, L”JoesDomain”, L”JoesPasswordPlainText”, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &htoken)

if (!retVal)
{
dwError = ::GetLastError();
qDebug() << "Error: GetLastError returned: " << dwError; } else { ::CloseHandle(htoken); qDebug() << "Logon successful."; } [/code]

Posted in Uncategorized | Leave a comment

Refresh Icon Cache In Windows 7

This keeps me from having to reboot:

C:\Windows\system32\ie4uinit.exe -ClearIconCache

Posted in Snippets | Leave a comment

Hash File Using POCO

This is the first time I have had to create MD5 hashes for files in C++. I did find a library called POCO that has some very handy classes for doing this. I didn’t want to copy and paste code from work, so I created this simple example for hashing a single file and outputting the MD5 hash in a hex encoded string.

The hashing uses a chunk size of 1 MiB changes so the whole file is not loaded at once.

I will soon create another post on how to do this in Qt5 and post it then, updating this post with the link.

Check out the open source POCO C++ libraries at pocoproject.org/. There are downloads for Windows. For Linux, you may be able to get packages from the main repositories for your distro.


#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;Poco/DigestStream.h&gt;
#include &lt;Poco/MD5Engine.h&gt;

int main(int argc, char** argv)
{
	Poco::MD5Engine md5;

	std::ifstream ifHash("../DSC_0002.JPG", std::ios::binary);
	if (ifHash.is_open())
	{
		Poco::DigestInputStream isMd5(md5, ifHash);
		size_t bufSize = 0x100000;
		char* buf = new char[bufSize];

		isMd5.read(buf, bufSize);
		while ( (isMd5.rdstate() &amp;&amp; std::ifstream::failbit) == 0 )
		{
			isMd5.read(buf, bufSize);
		}


		if ( (isMd5.rdstate() &amp;&amp; std::ifstream::eofbit) != 0)
		{
			std::string digest;
			digest = Poco::DigestEngine::digestToHex(md5.digest());
			std::cout &lt;&lt; digest.c_str() &lt;&lt; std::endl;
		}

		delete [] buf;
	}
}
Posted in C++, Cryptography | Tagged | Leave a comment