[英]How can I copy files from a Python package (site-packages) to a directory?
The situation is this: I have a Python library that serves to communicate with some other hardware using a custom protocol. 情况是这样的:我有一个Python库,该库用于使用自定义协议与其他一些硬件进行通信。 Whoever uses this solution needs a library in C code to implement the other end of the communications.
任何使用此解决方案的人都需要C代码库来实现通信的另一端。
I figured that a easy way to do this is to put the files in the Python package and provide a command that copies the files to a directory of choice. 我认为一种简单的方法是将文件放入Python包中,并提供一个将文件复制到所选目录的命令。 The files are bundled correctly, but I can't figure how to access them.
这些文件已正确捆绑,但我不知道如何访问它们。 I was hoping it would be this easy:
我希望这会很容易:
# 'pkgname' is a placeholder for package in site-packages.
shutil.copy('pkgname' + os.sep + 'filename', os.getcwd())
But then I get: 但是然后我得到:
FileNotFoundError: [Errno 2] No such file or directory: 'pkgname\\filename'
Any suggestion on how to fix the copy issue? 关于如何解决复印问题的任何建议? Or the problem itself?
还是问题本身?
Found a working solution, the main thing I was looking for was the first line: 找到了一个可行的解决方案,我主要要寻找的是第一行:
pkgdir = sys.modules['<mypkg>'].__path__[0]
fullpath = os.path.join(pkgdir, <myfile>)
shutil.copy(fullpath, os.getcwd())
Also did a silly error of not import the module in question; 还做了一个愚蠢的错误:没有导入相关模块; guess the obvious bugs are the hardest to find.
猜想最明显的错误是最难发现的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.