英文:
In which one of the "~/pythonX.X/site-packages/" should I put my self-written package when I have anaconda and different environments?
问题
我已经编写了一个包,想在不同目录中的不同脚本中导入和使用它。
我发现最好的方法是将其放在“site-packages”目录中(比更改PYTHONPATH和sys.path.insert和sys.path.append更好,因为我在这里读到的许多问题的答案很多)。
现在,由于我有Anaconda和Anaconda中的不同环境,我不知道应该把包放在哪里?例如,应该放在以下哪个目录中:
- ~/.local/lib/pythonX.X/site-packages/
- ~/anaconda3/lib/pythonX.X/site-packages/
- ~/anaconda3/envs/MY_ENV/lib/pythonX.X/site-packages/
我认为,如果我把它放在第一个或第二个目录中,无论在哪个环境中,我都可以导入它。如果我把它放在第三个目录中,只有在激活“MY_ENV”环境时,我才能导入它。这是真的吗?
附言:我应该安装它还是只需剪切并粘贴文件夹?
英文:
I have written a package and I want to import and use it in different scripts from different directories.
I found out that the best way is to put it in the "site-packages" directory (better than changing the PYTHONPATH and sys.path.insert and sys.path.append, due to many answers to many questions I read here).
Now since I have anaconda and different environments within anaconda, I do not know where I should put the package? for example in which of the following directories:
-
~/.local/lib/pythonX.X/site-packages/
-
~/anaconda3/lib/pythonX.X/site-packages/
-
~/anaconda3/envs/MY_ENV/lib/pythonX.X/site-packages/
What I think is that if I put it in the first one or the second one, I can import it in any script regardless which environment and if I put it in the third directory I can import it only when I am using a script when the "MY_ENV" environment is activated. Is it true?
p.s. should I install it or just cut and paste the folder in there?
答案1
得分: 1
是的,您对此有正确的理解。您应该放置软件包的位置取决于您的用例和要求。
~/.local/lib/python3.9/site-packages/:通常用于使用特定用户的Python安装程序安装的软件包。如果您希望您的软件包在系统上对所有用户和环境都可用,您可以将其放在这里。在此位置的软件包可以从任何Python环境中导入。
~/anaconda3/lib/python3.9/site-packages/:这个目录特定于默认的Anaconda Python安装。如果您希望您的软件包在所有Anaconda环境中全局可用,您可以将其放在这里。在此位置的软件包可以从任何Anaconda环境中导入。
~/anaconda3/envs/MY_ENV/lib/python3.9/site-packages/:这个目录特定于名为“MY_ENV”的特定Anaconda环境。如果您希望软件包仅在“MY_ENV”环境被激活时可用,您可以将其放在这里。在此位置的软件包只能在特定环境处于活动状态时导入。
根据您的要求,您可以选择合适的目录来放置您的软件包。如果您希望该软件包可以从任何Python环境中访问,而不管是否使用Anaconda,您可以考虑使用第一个或第二个目录。但是,如果您明确希望该软件包仅在特定Anaconda环境内可用,您可以使用第三个目录。
请记住确保您有写入所选目录所需的权限,特别是如果它是系统范围的位置。
英文:
Yes, you are correct in your understanding. The location where you should put your package depends on your use case and requirements.
~/.local/lib/python3.9/site-packages/: This directory is typically used for packages installed using the user-specific Python installation. If you want your package to be available globally for all users and environments on your system, you can place it here. Packages in this location can be imported from any Python environment.
~/anaconda3/lib/python3.9/site-packages/: This directory is specific to the default Anaconda Python installation. If you want your package to be available globally for all Anaconda environments, you can place it here. Packages in this location can be imported from any Anaconda environment.
~/anaconda3/envs/MY_ENV/lib/python3.9/site-packages/: This directory is specific to a particular Anaconda environment named "MY_ENV." If you want your package to be available only when the "MY_ENV" environment is activated, you can place it here. Packages in this location will only be importable when that specific environment is active.
Based on your requirement, you can choose the appropriate directory to place your package. If you want the package to be accessible from any Python environment, regardless of Anaconda, you can consider using the first or second directory. However, if you specifically want the package to be available only within a certain Anaconda environment, you can use the third directory.
Remember to ensure that you have the necessary permissions to write to the chosen directory, especially if it is a system-wide location.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论