11

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:\users\d4wt0\appdata\local\packages\pythonsoftwarefoundation.python.3.9_qbz5n2kfra8p0\localcache\local-packages\python39\site-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:\Users\d4wt0\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\d4wt0\AppData\Local\Temp\pip-record-x2n0fvt3\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\d4wt0\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Include\pythonnet'
         cwd: C:\Users\d4wt0\AppData\Local\Temp\pip-install-jlv07504\pythonnet_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:\Users\d4wt0\AppData\Local\Microsoft\WindowsApps\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\python.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('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record 'C:\Users\d4wt0\AppData\Local\Temp\pip-record-x2n0fvt3\install-record.txt' --single-version-externally-managed --user --prefix= --compile --install-headers 'C:\Users\d4wt0\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\Include\pythonnet' 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?

3
  • Hey can you mention your python version please!! May 6, 2021 at 13:00
  • Sure, it's 3.9.
    – Dawid_K
    May 6, 2021 at 13:02
  • pip install pythonnet==3.0.0a2 Jan 4, 2022 at 15:29

3 Answers 3

17

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:\Users\User\Downloads\pythonnet‑2.5.2‑cp39‑cp39‑win_amd64.whl
1
  • Please see @Stephen Simmons's answer which is better for 3.9
    – ishahak
    Jul 21, 2022 at 15:24
16

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.)

2

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) details regarding custom .whl installation.
The only test I did is import clr (successful - no crash or error) in the interpreter console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.