How to install pywin32 in virtualenv with python 2.7 64 bit?

隐身守侯 提交于 2020-01-22 17:53:28

问题


I wonder right way to install on virtualenv with 64 bit python 2.7 pywin32 module (Operating system is Windows 7 64 bit).
I've found exe installer on Sourceforge.

When i run easy_installer on command line first everything goes fine but soon i see strange behavior: while pywin32-216.win-amd64-py2.7 installs it runs pywin32-216-py2.7-win32.egg and it fails with error:

No local packages or download links found for pywin32==216
Best match: None

And here is my full log:

(sandbox_virtenv) D:\test\py2exe_test>easy_install http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.win-amd64-py2.7.exe
Downloading http://sourceforge.net/projects/pywin32/files/pywin32/Build216/pywin32-216.win-amd64-py2.7.exe
Processing pywin32-216.win-amd64-py2.7.exe
pywintypes: module references __file__
win32evtlogutil: module references __file__
win32serviceutil: module references __file__
win32timezone: module references __file__
Demos.win32rcparser_demo: module references __file__
Demos.security.sspi.validate_password: module references __file__
isapi.install: module references __file__
isapi.samples.advanced: module references __file__
isapi.samples.test: module references __file__
isapi.test.setup: module references __file__
pywin.framework.scriptutils: module references __file__
pywin.framework.startup: module references __path__
pywin.scintilla.config: module references __path__
pywin.scintilla.control: module references __file__
pywin.scintilla.IDLEenvironment: module references __file__
scripts.regsetup: module references __file__
scripts.setup_d: module references __file__
test.testall: module references __file__
test.test_clipboard: module references __file__
test.test_win32api: module references __file__
test.test_win32rcparser: module references __file__
D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\bdist_egg.py:422: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
  symbols = dict.fromkeys(iter_symbols(code))
test.test_win32trace: module references __file__
win32com.__init__: module references __path__
win32com.server.register: module references __file__
win32com.server.register: module references __path__
win32com.test.GenTestScripts: module references __path__
win32com.test.pippo_server: module references __file__
win32com.test.testall: module references __file__
win32com.test.testAXScript: module references __path__
win32com.test.testDictionary: module references __file__
win32com.test.testPippo: module references __file__
win32com.test.testPyComTest: module references __file__
win32com.test.testServers: module references __file__
win32com.test.util: module references __file__
win32comext.adsi.__init__: module references __path__
win32comext.authorization.__init__: module references __path__
win32comext.axcontrol.__init__: module references __path__
win32comext.axdebug.codecontainer: module references __file__
win32comext.axdebug.debugger: module references __file__
win32comext.axdebug.__init__: module references __path__
win32comext.axscript.__init__: module references __path__
win32comext.bits.__init__: module references __path__
win32comext.directsound.__init__: module references __path__
win32comext.directsound.test.ds_test: module references __file__
win32comext.internet.__init__: module references __path__
win32comext.mapi.__init__: module references __path__
win32comext.shell.__init__: module references __path__
win32comext.shell.demos.servers.empty_volume_cache: module references __file__
win32comext.shell.demos.servers.folder_view: module references __file__
win32comext.taskscheduler.__init__: module references __path__
creating 'c:\users\antp~1.kyi\appdata\local\temp\easy_install-kclmxk\pywin32-216-py2.7-win32.egg' and adding 'c:\users\antp~1.kyi\appdata\local\temp\easy_install-kclmxk\pywin32-216-py2.7-win32.egg.tmp' to it
creating d:\test\sandbox_virtenv\lib\site-packages\pywin32-216-py2.7-win32.egg
Extracting pywin32-216-py2.7-win32.egg to d:\test\sandbox_virtenv\lib\site-packages
Adding pywin32 216 to easy-install.pth file
Installing pywin32_postinstall.py script to D:\test\sandbox_virtenv\Scripts
Installing pywin32_postinstall.pyc script to D:\test\sandbox_virtenv\Scripts
Installing pywin32_testall.py script to D:\test\sandbox_virtenv\Scripts
Installing pywin32_testall.pyc script to D:\test\sandbox_virtenv\Scripts

Installed d:\test\sandbox_virtenv\lib\site-packages\pywin32-216-py2.7-win32.egg
Processing dependencies for pywin32==216
Searching for pywin32==216
Reading http://pypi.python.org/simple/pywin32/
Reading http://sf.net/projects/pywin32
Reading https://sourceforge.net/projects/pywin32/files/pywin32/
Reading http://sourceforge.net/project/showfiles.php?group_id=78018
No local packages or download links found for pywin32==216
Best match: None
Traceback (most recent call last):
  File "D:\test\sandbox_virtenv\Scripts\easy_install-script.py", line 8, in <module>
    load_entry_point('setuptools==0.6c11', 'console_scripts', 'easy_install')()
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 1712, in main
    with_ei_usage(lambda:
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 1700, in with_ei_usage
    return f()
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 1716, in <lambda>
    distclass=DistributionWithoutHelpCommands, **kw
  File "c:\Python27\Lib\distutils\core.py", line 152, in setup
    dist.run_commands()
  File "c:\Python27\Lib\distutils\dist.py", line 953, in run_commands
    self.run_command(cmd)
  File "c:\Python27\Lib\distutils\dist.py", line 972, in run_command
    cmd_obj.run()
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 211, in run
    self.easy_install(spec, not self.no_deps)
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 422, in easy_install
    return self.install_item(None, download, tmpdir, deps, True)
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 478, in install_item
    self.process_distribution(spec, dist, deps)
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 519, in process_distribution
    [requirement], self.local_index, self.easy_install
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py", line 563, in resolve
    dist = best[req.key] = env.best_match(req, self, installer)
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py", line 799, in best_match
    return self.obtain(req, installer) # try and download/install
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\pkg_resources.py", line 811, in obtain
    return installer(requirement)
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\command\easy_install.py", line 434, in easy_install
    self.local_index
  File "D:\test\sandbox_virtenv\lib\site-packages\setuptools-0.6c11-py2.7.egg\setuptools\package_index.py", line 475, in fetch_distribution
    return dist.clone(location=self.download(dist.location, tmpdir))
AttributeError: 'NoneType' object has no attribute 'clone'

(sandbox_virtenv) D:\test\py2exe_test>

Now i can import from python win32api and it's quite confusing

(sandbox_virtenv) D:\test\py2exe_test>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>>

If you find here something unclear ask me. Thanks for yours advices.


回答1:


I figured this out, but the process isn't pleasant.

There are 3 problems with the PyWin32 installer:

  1. It reads your python install location out of the registry.
  2. Is assumes that your lib directory is a child of the directory containing python.exe
  3. It dumps 3 dlls to %WINDIR%\system32

So if you're running 64 bit python 2.7 on 64 bit windows or 32 on 32 and your virtual environment is in c:\scratch\env1\ the process looks like this:

  1. Copy python.exe and pythonw.exe from c:\scratch\env1\scripts to c:\scratch\env1
  2. Update the value "(default)" in the registry key HKLM\SOFTWARE\Python\PythonCore\2.7\InstallPath\ to "c:\scratch\env1\" (make sure you record the old value.)
  3. From the command line, where you've activated the virtual environment, install PyWin32 by running the installer.exe directly. Do not use easy_install.
  4. Set the InstallPath registry key back to the original value
  5. Delete python.exe and pythonw.exe from c:\scratch\env1\
  6. move pythoncom27.dll, pywintypes27.dll and pythoncomloader27.dll from %WINDIR%\system32\ to c:\scratch\env1\ (do not move python27.dll or you'll regret it)

This works well enough to run django-mssql in the virtual environment without having pywin32 installed outside of the virtual environment.




回答2:


With the latest virtualenv-1.7.1.2, try creating the virtualenv with the --distribute flag. I managed to solve the exact same problem you had by adding the flag.

On a related note, on another machine (same OS, Windows Server 2008 R2), I had to create the virtualenv without the --distribute flag, otherwise whenever I run easy_install a new command prompt will pop open.

Virtualenv on Windows is like a dark art.




回答3:


Unfortunately, support for 64bit Python on Windows is relatively poor than the 32bit version. It's not the fault of the Python community but the relative difficulty of getting free compilers for 64bit Windows.

If you installed the 64bit version of Python, you also need to use packages built for 64bit. Some simple Python packages (those written in pure Python and not requiring external/C libraries) will install as normal using setuptools/easy_install/virtualenv. A lot of 64bit packages however might break when installed in virtualenv and will require an installer.

Fortunately, you can find 64bit pre-compiled installers for popular packages here. Try the package there first. The installer will target the global/main installation of Python and thus is not isolated by virtualenv. Note however that these are unofficial builds: provided for free for your convenience so please don't sue the provider if things fail (or expect for help).




回答4:


This is an old question, but come across this problem recently and I want to share what I did to make this work. I was looking for python kerberos implementation on windows, and I found kerbeors_sspi that uses pywin32. all of this is installed using pip, but pywin32 can't be installed using pip inside the virtualenv.

So i patched the kerberos_sspi library to use pypiwin32, which is a wheel packaged by Glyph and el (twisted creator BTW). And now all of this works wonderfully, still if you want access the COM stuff you have to manually install pywin32 because it needs to be registered with the system.

Here is the link to the pypiwin32 index

Cheers,



来源:https://stackoverflow.com/questions/8078216/how-to-install-pywin32-in-virtualenv-with-python-2-7-64-bit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!