Python virtualenv problem z instalacją gdal

Cześć,
Mam problem z instalacją gdal w pythonie w virtualenv

pip install gdal
Collecting gdal
  Using cached gdal-3.9.3.tar.gz (840 kB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [92 lines of output]
      Using numpy 2.1.2
      running egg_info
      writing gdal-utils/GDAL.egg-info/PKG-INFO
      writing dependency_links to gdal-utils/GDAL.egg-info/dependency_links.txt
      writing entry points to gdal-utils/GDAL.egg-info/entry_points.txt
      writing requirements to gdal-utils/GDAL.egg-info/requires.txt
      writing top-level names to gdal-utils/GDAL.egg-info/top_level.txt
      Traceback (most recent call last):
        File "<string>", line 91, in fetch_config
        File "/usr/lib/python3.11/subprocess.py", line 1026, in __init__
          self._execute_child(args, executable, preexec_fn, close_fds,
        File "/usr/lib/python3.11/subprocess.py", line 1955, in _execute_child
          raise child_exception_type(errno_num, err_msg, err_filename)
      FileNotFoundError: [Errno 2] No such file or directory: 'gdal-config'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 231, in get_gdal_config
        File "<string>", line 94, in fetch_config
      gdal_config_error: [Errno 2] No such file or directory: 'gdal-config'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "/home/przemek/Dokumenty/studia/semestr-VII/python/projekt-pycharm/pythonProject/.venv2/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/home/przemek/Dokumenty/studia/semestr-VII/python/projekt-pycharm/pythonProject/.venv2/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
                                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/przemek/Dokumenty/studia/semestr-VII/python/projekt-pycharm/pythonProject/.venv2/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
                 ^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 332, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=[])
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 302, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 318, in run_setup
          exec(code, locals())
        File "<string>", line 429, in <module>
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/__init__.py", line 117, in setup
          return distutils.core.setup(**attrs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 183, in setup
          return run_commands(dist)
                 ^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/core.py", line 199, in run_commands
          dist.run_commands()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 954, in run_commands
          self.run_command(cmd)
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/dist.py", line 991, in run_command
          super().run_command(command)
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/dist.py", line 973, in run_command
          cmd_obj.run()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/command/egg_info.py", line 315, in run
          self.find_sources()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/command/egg_info.py", line 323, in find_sources
          mm.run()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/command/egg_info.py", line 544, in run
          self.add_defaults()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/command/egg_info.py", line 582, in add_defaults
          sdist.add_defaults(self)
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/command/sdist.py", line 108, in add_defaults
          super().add_defaults()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/command/sdist.py", line 238, in add_defaults
          self._add_defaults_ext()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/command/sdist.py", line 322, in _add_defaults_ext
          build_ext = self.get_finalized_command('build_ext')
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/cmd.py", line 303, in get_finalized_command
          cmd_obj.ensure_finalized()
        File "/tmp/pip-build-env-pll5v6cd/overlay/lib/python3.11/site-packages/setuptools/_distutils/cmd.py", line 111, in ensure_finalized
          self.finalize_options()
        File "<string>", line 307, in finalize_options
        File "<string>", line 237, in get_gdal_config
      gdal_config_error: Traceback (most recent call last):
        File "<string>", line 91, in fetch_config
        File "/usr/lib/python3.11/subprocess.py", line 1026, in __init__
          self._execute_child(args, executable, preexec_fn, close_fds,
        File "/usr/lib/python3.11/subprocess.py", line 1955, in _execute_child
          raise child_exception_type(errno_num, err_msg, err_filename)
      FileNotFoundError: [Errno 2] No such file or directory: 'gdal-config'
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "<string>", line 231, in get_gdal_config
        File "<string>", line 94, in fetch_config
      gdal_config_error: [Errno 2] No such file or directory: 'gdal-config'
      
      Could not find gdal-config. Make sure you have installed the GDAL native library and development headers.
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

W systemie mam zainstalowaną bibliotekę python-gdal i gdal

  1. gdal z pip musi mieć taką samą wersję jak pakiet gdal, za dokumentacją na pypi najprościej osiągniesz to w ten sposób:
pip install gdal[numpy]=="$(gdal-config --version).*"
  1. Masz już zainstalowany odpowiedni moduł pythona – python-gdal , więc de facto w virtualenv nie musisz go instalować. Jeśli zainicjujesz virtualenv z flagą --system-site-packages to moduły zainstalowane w systemie będą widoczne w środowisku :wink: