如何通过conda安装我自己的python模块(包)并观察它的变化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49474575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to install my own python module (package) via conda and watch its changes
提问by Michael
I have a file mysql.py
, which I use in almost all of my projects. Since I do not want to copy and paste the same file into each of these projects I wrote a module - possibly a package in the future.
我有一个文件mysql.py
,我几乎在所有项目中都使用它。由于我不想将相同的文件复制并粘贴到每个项目中,因此我编写了一个模块 - 将来可能是一个包。
Question
题
How do I add a local module to my conda environment, and automatically update or watch it when I change something in mysql.py
? How to do the same for a package?
如何将本地模块添加到我的 conda 环境中,并在我更改某些内容时自动更新或监视它mysql.py
?如何对包做同样的事情?
I would like to avoid to set up a local channel/repository and just reference to the folder with mysql.py
.
我想避免设置本地频道/存储库,而只是引用带有mysql.py
.
回答by Michael
While the previous answers are doing what I need, I just want to show what I will be using instead. Since it was my plan to learn about conda packages anyway...
虽然以前的答案正在做我需要的事情,但我只想展示我将使用的内容。因为无论如何我都计划学习 conda 包......
0. Good sources
0. 好货源
- Michael Sarahan - Making packages and packaging "just work" | YouTube
- GitHub - audreyr/cookiecutter: A command-line utility that creates projects from cookiecutters (project templates)and use one of these templates:
- Michael Sarahan - 制作包装和包装“恰到好处” | YouTube
- cookiecutter,从 cookiecutter (项目模板) 创建项目并使用以下模板之一的 命令行实用程序,下载cookiecutter的源码_GitHub_酷徒
1. Create a python package template for conda using cookiecutter
1.使用cookiecutter为conda创建python包模板
conda install -c conda-forge cookiecutter
Now change to the directory where you want to initialize your package, then do:
现在切换到要初始化包的目录,然后执行以下操作:
cookiecutter https://github.com/conda/cookiecutter-conda-python.git
This will ask for some basic information about the package that you want to create. Then change into your repo
这将询问有关您要创建的包的一些基本信息。然后换成你的回购
cd myrepo
2. Build your package
2. 构建你的包
make sure conda-build
is installed, if not run
确保conda-build
已安装,如果没有运行
conda install conda-build
Make sure to set the CONDA_BLD_PATH
as mentioned in anaconda - using a different conda-build root directory - Stack Overflow. This will be the directory where you can find your packages, then run:
确保CONDA_BLD_PATH
按照anaconda 中所述进行设置- 使用不同的 conda-build 根目录 - 堆栈内存溢出。这将是您可以找到包的目录,然后运行:
conda build conda.recipe
to build your package and clean up after you with
构建你的包并在你之后清理
conda build purge
3. Set up your own local channel (no uploading to anaconda.org)
3.建立自己的本地频道(不上传到anaconda.org)
Read
读
- Creating custom channels — Conda documentation
- python - add local channel to .condarc on windows - Stack Overflow
for help.
求助。
Index each platform. Maybe someone can confirm that this step is not needed, as my builds already contain the repodata.json
. Otherwise:
索引每个平台。也许有人可以确认不需要这一步,因为我的构建已经包含repodata.json
. 除此以外:
conda index D:\CODE\condamychannel\win-64
Test if the package can be found with
测试是否可以找到包
conda search -c file:///D:\CODE\condamychannel --override-channels mypackage
or add the channel to the config directly (per environment)
或直接将频道添加到配置中(每个环境)
conda config --add channels file:///D:\CODE\condamychannel
4. Install (and update) the package
4.安装(和更新)包
activate myenv
and
和
conda install mypackage
Once I change mypackage
, I give it a new version number in meta.yaml
and setup.py
and build the package with conda build conda.recipe
.
Updating is simply
一旦我改变mypackage
,我给它一个新的版本号,meta.yaml
并setup.py
用conda build conda.recipe
. 更新很简单
conda update mypackage
See if your package works:
看看你的包是否有效:
python
>>> import cli from mypackage
>>> cli.cli()
CLI template
This may not be the optimal way, but I could not find a tutorial that contains all the steps I outlined above.
这可能不是最佳方式,但我找不到包含我上面概述的所有步骤的教程。
回答by darthbith
If you install the conda build package (and you have a package, not just a script), you can install in "editable" mode:
如果你安装了 conda 构建包(并且你有一个包,而不仅仅是一个脚本),你可以在“可编辑”模式下安装:
conda develop .
(running from the directory with your script). This is very similar to the "editable" mode from pip
(从带有脚本的目录运行)。这与 pip 的“可编辑”模式非常相似
pip install -e .
Either approach lets you uninstall packages with either
这两种方法都可以让您卸载软件包
conda develop -u .
or
或者
pip uninstall .
If you just have a script (not a package), you can edit or set the PYTHONPATH
environment variable to include the directory with the script.
如果您只有一个脚本(不是包),您可以编辑或设置PYTHONPATH
环境变量以将目录包含在脚本中。
回答by amanb
I had a wrapper function that I had to call multiple times on different scripts.So, I copied that file wrappers.py
to the Anaconda site-packagesfolder. On my computer this was at: C:\ProgramData\Anaconda3\Lib\site-packages. Then, whenever I needed wrappers.py
, I'd just import it in my scripts, like this:
我有一个包装函数,我必须在不同的脚本上多次调用它。所以,我将该文件复制wrappers.py
到了 Anaconda站点包文件夹。在我的电脑上,这是在:C:\ProgramData\Anaconda3\Lib\site-packages。然后,每当我需要时wrappers.py
,我都会将它导入到我的脚本中,如下所示:
import wrappers
If you want to make sure that the import was successful, you could either select Anaconda as your dev environment in your IDE and then invoke the Intellisense after import: from wrappers import (intellisense suggestions)
. Or you could also use IDLE:
如果你想确保导入成功,您既可以选择如蟒蛇在IDE的开发环境,然后调用智能感知导入后:from wrappers import (intellisense suggestions)
。或者你也可以使用 IDLE:
>>> import wrappers
>>>(if you get this prompt, this means the import was successful).
Conda integration is only possible if you create custom channels. This is because conda searches for packages in the following locations(based on your OS) and to tell conda to search for your module at certain location(s), you must install conda-build
to create a custom channel:
Conda 集成只有在您创建自定义渠道时才可能。这是因为 conda 在以下位置搜索包(基于您的操作系统)并告诉 conda 在特定位置搜索您的模块,您必须安装conda-build
以创建自定义通道:
- https://repo.continuum.io/pkgs/main/win-64
- https://repo.continuum.io/pkgs/main/noarch
- https://repo.continuum.io/pkgs/free/win-64
- https://repo.continuum.io/pkgs/free/noarch
- https://repo.continuum.io/pkgs/r/win-64
- https://repo.continuum.io/pkgs/r/noarch
- https://repo.continuum.io/pkgs/pro/win-64
- https://repo.continuum.io/pkgs/pro/noarch
- https://repo.continuum.io/pkgs/msys2/win-64
- https://repo.continuum.io/pkgs/msys2/noarch
If you want to avoid creating a repo/channel, then a simple import as above should do the job. In addition, if you make any changes to your module and save it, you will always have the latest import
in your scripts.
如果你想避免创建一个 repo/channel,那么上面的简单导入应该可以完成这项工作。此外,如果您对模块进行任何更改并保存它,您将始终拥有import
脚本中的最新版本。