Can’t import libarchive-c in Python: FileNotFoundError

I am trying to selectively extract files from many 7zip files automatically, the usual patool and pyunpack and the like don’t allow selection of files to be extracted. py7zr seems to provide the functionality however I find it extremely inefficient to extract individual files.

I found libarchive-c after crafting this Google query manually:

https://www.google.com/search?q=%22python%22+%227z%22+-patool+-pyunpack+-py7zr&tbs=li:1

And sifting through the trash it still returns.

I installed this version of libarchive, I downloaded the binary for Windows 10 x64 here.

I extracted the files from the libarchive package to D:Programslibarchive.

At first I can’t even import libarchive:

In [20]: import libarchive
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [20], in <cell line: 1>()
----> 1 import libarchive

File C:Program FilesPython310libsite-packageslibarchive__init__.py:1, in <module>
----> 1 from .entry import ArchiveEntry
      2 from .exception import ArchiveError
      3 from .extract import extract_fd, extract_file, extract_memory

File C:Program FilesPython310libsite-packageslibarchiveentry.py:6, in <module>
      3 from enum import IntEnum
      4 import math
----> 6 from . import ffi
      9 class FileType(IntEnum):
     10     NAMED_PIPE     = AE_IFIFO  = 0o010000  # noqa: E221

File C:Program FilesPython310libsite-packageslibarchiveffi.py:26, in <module>
     23 page_size = mmap.PAGESIZE
     25 libarchive_path = os.environ.get('LIBARCHIVE') or find_library('archive')
---> 26 libarchive = ctypes.cdll.LoadLibrary(libarchive_path)
     29 # Constants
     31 ARCHIVE_EOF = 1       # Found end of archive.

File C:Program FilesPython310libctypes__init__.py:452, in LibraryLoader.LoadLibrary(self, name)
    451 def LoadLibrary(self, name):
--> 452     return self._dlltype(name)

File C:Program FilesPython310libctypes__init__.py:364, in CDLL.__init__(self, name, mode, handle, use_errno, use_last_error, winmode)
    362 import nt
    363 mode = nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
--> 364 if '/' in name or '\' in name:
    365     self._name = nt._getfullpathname(self._name)
    366     mode |= nt._LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR

TypeError: argument of type 'NoneType' is not iterable

It is extremely easy to figure out what is wrong, at least for me. It is because of this line:

libarchive_path = os.environ.get('LIBARCHIVE') or find_library('archive')

It searches for the environment variable to file the path of libarchive library, but I downloaded a .zip package not an installer which doesn’t set the environment variables, so the first command returns None. Of course it means the second command also failed.

So I had to manually supply the correct path. It is not well-documented, at least I haven’t found it, but the line immediately below it is:

libarchive = ctypes.cdll.LoadLibrary(libarchive_path)

It means the path is expected to be a .dll path, so I changed it to this:

libarchive_path = "D:/Programs/libarchive/bin/archive.dll"

But I still can’t import libarchive:

FileNotFoundError: Could not find module 'D:Programslibarchivebinarchive.dll' (or one of its dependencies). Try using the full path with constructor syntax.

I have searched this error and have found a lot of irrelevant information. I have found many posts similar to this: Can’t import dll module in Python

But they are all unhelpful, as I have supplied the absolute path while they are all dealing about relative path.

So the first part of the message is False, that must not be the case, the file must have been indeed found, or there is a serious bug in the library code, or the Python implementation…

Could it be there are other .dll files in that directory that this .dll depends on?

I have tried the following commands and they don’t solve the issue:

os.chdir("D:/Programs/libarchive/bin")
os.add_dll_directory("D:/Programs/libarchive/bin")

And no, there is only one .dll in that directory:

PS D:Programslibarchivebin> (gci).fullname
D:Programslibarchivebinarchive.dll
D:Programslibarchivebinbsdcat.exe
D:Programslibarchivebinbsdcpio.exe
D:Programslibarchivebinbsdtar.exe
PS D:Programslibarchivebin>

So how can I fix this?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật