Skip to content

Instantly share code, notes, and snippets.

@JesseBuesking
Last active January 3, 2016 04:09
Show Gist options
  • Save JesseBuesking/8407154 to your computer and use it in GitHub Desktop.
Save JesseBuesking/8407154 to your computer and use it in GitHub Desktop.
Creating a virtualenv and updating so that the compiler is set to mingw32 (so that things like cython can be installed via pip).

Execute create-package <package name> from a command prompt to use.

Assumptions:

  • ming32 is installed
  • the path to the mingw32 compiler is on the PATH
  • these files are in the root of you virtualenv directories ex C:\envpy\ ; virtual environments will be created on paths that look like C:\envpy\<package name>

See https://zignar.net/2012/06/17/install-python-on-windows/ for more information.

virtualenv --distribute --no-site-packages %1
python update-mingw-compiler.py %1
import os
import sys
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Please supply the name of the virtualenv environment.")
sys.exit(1)
name = sys.argv[1]
print("Updating the compiler for environment [{}]".format(name))
directory = os.path.dirname(os.path.abspath(__file__))
path = os.path.normpath(
'{}/{}/Lib/distutils/distutils.cfg'.format(directory, name)
)
found_compiler = False
found_build = False
lines = ''
# handle finding a compiler= line
with open(path, 'r') as distutils:
for line in distutils:
if 'compiler=' in line:
found_compiler = True
line = 'compiler=mingw32\n'
if '[build]' in line:
found_build = True
lines += line
# handle case where [build] was found, but no compiler= line
if not found_compiler and found_build:
with open(path, 'r') as distutils:
lines = ''
for line in distutils:
lines += line
if '[build]' in line:
lines += 'compiler=mingw32\n'
found_compiler = True
# handle case where no [build] or compiler= lines were found
if not found_compiler:
lines += '[build]\n'
lines += 'compiler=mingw32\n'
# modify the file
with open(path, 'w') as distutils:
distutils.write(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment