简体   繁体   English

如何将文件从Python程序包(站点程序包)复制到目录?

[英]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.

相关问题 我可以将软件包从旧的Python 3.6.3复制并粘贴到新安装的Python 3.7.4的/ Lib / site-packages目录中吗? - Can I copy and paste software packages from my old Python 3.6.3 to the /Lib/site-packages directory of a newly installed Python 3.7.4? 将文件复制到网站程序包 - Copy files to site-packages 将python包安装到相对于site-packages的目录中 - Install python package into directory relative to site-packages 从site-packages文件夹在本地目录下开发Python软件包,还是有更好的选择? - Developing a Python package under a local directory, from site-packages folder or is there a better option? 假设无法访问site-packages目录,使用Python .pth文件 - Using Python .pth files assuming no access to site-packages directory 如何在不将所有内容复制到lib / site-packages的情况下安装python包? - How to install python package without copy everything into lib/site-packages? 在python模块安装过程中会发生什么? 我可以将模块源复制到site-packages吗? - What happens during a python module install? Can I just copy the module source to site-packages? 我可以拥有我的Python OS X“ site-packages”目录的所有权(以避免“ sudo”)吗? - Can I take ownership of my Python OS X 'site-packages' directory (to avoid 'sudo')? 如何为本地Python 2.7安装设置site-packages目录 - How to set site-packages directory for local Python 2.7 install 无法从site-packages目录加载通过pip安装的Python模块 - Can't load Python modules installed via pip from site-packages directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM