How to fix error during pythonnet installation

Question:

When I try to use following command:

pip install pythonnet

I see error that you can see below:

Collecting pythonnet
  Using cached pythonnet-2.5.2.tar.gz (1.9 MB)
Requirement already satisfied: pycparser in c:usersd4wt0appdatalocalpackagespythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0localcachelocal-packagespython39site-packages (from pythonnet) (2.20)
Using legacy 'setup.py install' for pythonnet, since package 'wheel' is not installed.
Installing collected packages: pythonnet
    Running setup.py install for pythonnet ... error
    ERROR: Command errored out with exit status 1:
     command: 'C:Usersd4wt0AppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\d4wt0\AppData\Local\Temp\pip-install-jlv07504\pythonnet_0928bad80975481c9950abc5017a28b4\setup.py'"'"'; __file__='"'"'C:\Users\d4wt0\AppData\Local\Temp\pip-install-jlv07504\pythonnet_0928bad80975481c9950abc5017a28b4\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:Usersd4wt0AppDataLocalTemppip-record-x2n0fvt3install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:Usersd4wt0AppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39Includepythonnet'
         cwd: C:Usersd4wt0AppDataLocalTemppip-install-jlv07504pythonnet_0928bad80975481c9950abc5017a28b4
    Complete output (6 lines):
    usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: setup.py --help [cmd1 cmd2 ...]
       or: setup.py --help-commands
       or: setup.py cmd --help

    error: option --single-version-externally-managed not recognized
    ----------------------------------------
ERROR: Command errored out with exit status 1: 'C:Usersd4wt0AppDataLocalMicrosoftWindowsAppsPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0python.exe' -u -c 'import io, os, sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\Users\d4wt0\AppData\Local\Temp\pip-install-jlv07504\pythonnet_0928bad80975481c9950abc5017a28b4\setup.py'"'"'; __file__='"'"'C:\Users\d4wt0\AppData\Local\Temp\pip-install-jlv07504\pythonnet_0928bad80975481c9950abc5017a28b4\setup.py'"'"';f = getattr(tokenize, '"'"'open'"'"', open)(__file__) if os.path.exists(__file__) else io.StringIO('"'"'from setuptools import setup; setup()'"'"');code = f.read().replace('"'"'rn'"'"', '"'"'n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:Usersd4wt0AppDataLocalTemppip-record-x2n0fvt3install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:Usersd4wt0AppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39Includepythonnet' Check the logs for full command output.

How can I fix it? I tried to install that package using PyCharm, but there is the same error. Any ideas?

Asked By: Dawid_K

||

Answers:

From the Python.NET docs I can see it doesn’t support python 3.9. You can download wheel file from
here.

As your python version is 3.9, download pythonnet‑2.5.2‑cp39‑cp39‑win32.whl (for 32-bit python version) or pythonnet‑2.5.2‑cp39‑cp39‑win_amd64.whl (for 64-bit python version).

Then specify a full path to wheel file for installation.

Example:

pip install C:UsersUserDownloadspythonnet‑2.5.2‑cp39‑cp39‑win_amd64.whl
Answered By: Bhavya Parikh

The current pre-release version supports Python 3.9. It can be installed with pip using:

pip install --pre pythonnet

(This answer came from https://github.com/pythonnet/pythonnet/issues/1262.)

Answered By: Stephen Simmons

Split this out of [SO]: pywebview installing Error Python windows 10 (Python 3.10.5) "pip install pywebview" (@CristiFati’s answer).

You’re using Python 3.9.
According to [PyPI]: pythonnet – Release history, latest released version (at answer time) is v2.5.2. Unfortunately, highest Python version that the .whls are built for is v3.8. Since there is no .whl for your version, it tries to build it from source, and that’s where it fails.

To get past this, you could either:

  1. Use an older Python version (v3.8) that has all the packages built for it. Everything should work OOTB.
    The drawback is obvious, older versions are only receiving security updates and will go out of support sooner, and some packages (newer versions) might no longer support them.

  2. Install / build it manually (python -m pip install pythonnet)

    1. Install v3.0.0rc6 (!!! PRE RELEASE !!!) available at original answer time (check the end of this section). Pass –pre flag to PIP, as instructed in [GitHub]: pythonnet/pythonnet – Troubleshooting on Windows, Linux, and OSX:

      pip install pythonnet [--pre] -U (–pre gets the latest development build uploaded to PyPI)

      So, the command line should be: python -m pip install --pre pythonnet.
      Or specify the version directly: pip install pythonnet==3.0.0rc6.

      On 220929, PythonNET 3.0.0 was released!.
      You can use that one from now on. It’s one .whl only (doesn’t seem to be tied to Python versions).

    2. Find a 3rd-party repository that has prebuilt .whls for newer Python versions:

    3. Build v2.5.2. But the process is shitty (it has been much improved in v3), as it requires lots of stuff:

      • MSBuild – installed by VStudio (but I think it can also be installed standalone)

      • CLang – can be installed standalone, but also by other software like:

        • Android Studio

        • Nix emulators (MinGW, MSYS2, Cygwin, …)

      • Some environment variables being set

      • A file needs to be patched

      I did all of the above (and published the binaries in the GitHub URL mentioned in the answer).


Update #0

Built .whls and published them on [GitHub]: CristiFati/Prebuilt-Binaries – (master) Prebuilt-Binaries/PythonNET/v2.5.2 (win_amd64 and win32 for Python: v3.11, v3.10, v3.9). Check [SO]: Installing pygraphviz on Windows 10 64-bit, Python 3.6 (@CristiFati’s answer) (Shortcut section – at the end) for how to install the appropriate one.
The only test I did is import clr (successful – no crash or error) in the interpreter console.

Answered By: CristiFati
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.