from __future__ import print_function import os import sys import shutil assert sys.getwindowsversion() >= (6,0), "Windows Vista or later required" ################################ # first some ctypes symlink implementation since Python doesn't support # symlinks in windows yet. Borrowed from jaraco.windows project. from ctypes import windll from ctypes import c_uint64 from ctypes.wintypes import ( BOOLEAN, LPWSTR, DWORD, LPVOID, HANDLE, FILETIME, WCHAR, ) CreateSymbolicLink = windll.kernel32.CreateSymbolicLinkW CreateSymbolicLink.argtypes = ( LPWSTR, LPWSTR, DWORD, ) CreateSymbolicLink.restype = BOOLEAN def handle_nonzero_success(result): if result == 0: raise RuntimeException(result) def symlink(link, target, target_is_directory = False): """ An implementation of os.symlink for Windows (Vista and greater) """ target_is_directory = target_is_directory or os.path.isdir(target) handle_nonzero_success(CreateSymbolicLink(link, target, target_is_directory)) # end ctypes symlink implementation ################################ # create a sample package package_name = 'sample' tagged = '{0}-tagged'.format(package_name) os.mkdir(tagged) init_file = os.path.join(tagged, '__init__.py') open(init_file, 'w').close() assert os.path.exists(init_file) # now create a symlink to the tagged package so it can be imported symlink(package_name, tagged) assert os.path.isdir(package_name) assert os.path.isfile(os.path.join(package_name, '__init__.py')) # make sure sample can only be imported from the current directory. sys.path[:] = ['.'] # and try to import the package try: pkg = __import__(package_name) # fails print("Problem does not exist here.") except ImportError as e: print(e) # now cleanup os.rmdir(package_name) shutil.rmtree(tagged)