perl - 在安装 conda 包期间更新 @INC 变量

标签 perl environment-variables conda environment conda-build

我正在尝试安装 Perl 模块的 conda 包。到目前为止,我可以使用 conda-build 创建包。为此,我有一个包含 build.shmeta.yaml 文件的配方。

然后我在新环境中使用 conda-install 安装它,我希望能够运行位于我刚刚安装的 Perl 模块中的一些 Perl 脚本。

所有这些步骤都运行良好,但是当我运行一些脚本时,出现错误:

无法在 @INC 中找到 PMP/util.pm(您可能需要安装 PMP::util 模块)(@INC 包含:/.autofs/home/antoine/anaconda2/envs/testCustomChannel/lib/site_perl/5.26.2/x86_64-linux-thread-multi/.autofs/home/antoine/anaconda2/envs/testCustomChannel/lib/site_perl/5.26.2/.autofs/home/antoine/anaconda2/envs/testCustomChannel/lib/5.26.2/x86_64-linux-thread-multi/.autofs/home/antoine/anaconda2/envs/testCustomChannel/lib/5.26.2 .)

正如你所看到的,当我执行 Perl 时,我的 Perl 模块的某些模块似乎无法识别。我知道要解决此问题,我可以修改 @INC 变量并将 bin/添加到 PATH,将 lib/添加到 PERL5LIB,但我需要在模块安装过程中自动执行此过程。

我真的不知道应该在哪里修改环境变量。例如,在创建包的过程中,在 build.sh 中添加一些内容?或者我应该在安装过程中管理它,如果是的话,我该怎么做?

如有任何建议,我们将不胜感激,

谢谢

编辑:

meta.yaml =>

{% set name = "module_name" %}
{% set version = "0.8.3" %}

package:
  name: "{{ name }}"
  version: "{{ version }}"

source:
  git_url: ssh://git@adress/bspcore/perl_module.git

build:
  number: 0

requirements:
  host:
    - perl
    - perl-extutils-makemaker
  run:
    - perl

about:
  home: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  license: xxx
  license_family: xxx
  summary: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Build.sh =>

#!/bin/bash
if [ -f Build.PL ]; then
    perl Build.PL
    perl ./Build
    # Make sure this goes in site
    perl ./Build install --installdirs site
elif [ -f Makefile.PL ]; then
    # Make sure this goes in site
    perl Makefile.PL INSTALLDIRS=site
    make
    make install
else
    echo 'Unable to find Build.PL or Makefile.PL. You need to modify build.sh.'
    exit 1
fi

chmod u+rwx $PREFIX/bin/*
echo "${PREFIX}"

编辑2:

另一个编辑可以帮助你们更好地了解我的情况。我刚刚意识到,当我构建包时,我的 Perl 模块的 lib 文件夹(其中有 PMP::util)位于 lib/site_perl/5.26.0/Perl_Module 下。我非常确定,如果我能够将其直接安装在 lib/ 文件夹下,它将解决此问题。但是我不确定如何修改 build.sh 文件来修改我们构建 perl 模块的位置。

最佳答案

以下是如何创建一个安装 Perl 模块(依赖于 CPAN 模块)的 conda 包的简单示例,可能会帮助您解决问题:

在 Linux 上安装 miniconda

$ wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
$ bash Miniconda3-latest-Linux-x86_64.sh
# NOTE: I answered "yes" on the question:
#   "Do you wish the installer to initialize Miniconda3 ?" in the
#   previous command. This will modify ~/.bashrc
$ source ~/.bashrc # activates base environment
# Next command: Do not automatically activate conda for every terminal window,
# instead run "conda activate" from a given terminal window to
# activate locally. The following command also creates ~/.condarc
$ conda config --set auto_activate_base False

创建包:

perl-hello/meta.yaml:

package:
  name: perl-hello3
  version: "1.0"

source:
  path: ../src # NOTE: if you had put "src" in same folder as "meta.yaml", 
               # conda-build would have include the src folder in info/recipe in 
               # the generated package. It is not necessary to include  the 
               # source code in the generated package.

requirements:
  build:
    - perl >= 5.22
    - make

  run:
    - perl >= 5.22

about:
  license: Artistic
  summary: Simple perl function

../src/:

$ tree ../src
../src
├── lib
│   └── My
│       └── Module.pm
└── Makefile.PL

../src/Makefile.PL:

use utf8;
use ExtUtils::MakeMaker;

WriteMakefile(
    MIN_PERL_VERSION => 5.022000,
    NAME             => 'My::Module',
    VERSION_FROM     => 'lib/My/Module.pm',
    PREREQ_PM        =>
    {
        'ExtUtils::MakeMaker' => '7.12',
        'Data::Dump'          => 0,
    },
    ABSTRACT_FROM    => 'lib/My/Module.pm',
    AUTHOR           => 'Håkon Hægland <<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f89099939796d690999f9499969cb89f95999194d69b9795" rel="noreferrer noopener nofollow">[email protected]</a>>',
    LICENSE          => 'perl',
);

../src/lib/My/Module.pm:

package My::Module;
our $VERSION = 0.01;
use strict;
use warnings;
use Exporter qw(import);

our @EXPORT = qw(hello);
our @EXPORT_OK = @EXPORT;

use Data::Dump;
sub hello {
    print "Hello world!\n";
    my $str = "Testing Perl module Data::Dump";
    dd $str;
}
1;

build.sh:

# PERL_MM_USE_DEFAULT=1  -> automatically answer "yes" on config questions
PERL_MM_USE_DEFAULT=1 cpan App::cpanminus
perl ${PREFIX}/bin/cpanm Data::Dump
perl Makefile.PL INSTALLDIRS=site
make
make install

请注意,我使用 perl ${PREFIX}/bin/cpanm 运行 cpanm。我无法简单地将其作为 cpanm 运行,请参阅 Can you rely on the shebang of an installed command during build?了解更多信息。

构建包

$ conda-build .

(记下生成的输出,并确定生成的包的路径。在我的例子中,路径名称为:

/home/hakon/miniconda3/conda-bld/linux-64/perl-hello3-1.0-pl526_0.tar.bz2

将包上传到anaconda服务器

  • Anaconda Cloud 注册新用户

  • 安装客户端

    $ conda install anaconda-client
    
  • 登录您的帐户:

    $ anaconda login
    
  • 上传生成的包:

    $ anaconda upload /home/hakon/miniconda3/conda-bld/linux-64/perl-hello3-1.0-pl526_0.tar.bz2
    

测试包(可以在任何 Linux 机器上完成):

  • 创建一个新环境:

     $ conda create --name perltest
     $ conda activate perltest
    
  • 在新环境中安装软件包:

     $ conda install -c hakonhagland perl-hello3 
     # Alternatively: You can test the package locally before uploading with
     #   "conda install --use-local perl-hello3"
    
  • 测试包:

     $ perl -E 'use My::Module; hello'
     Hello world!
     "Testing Perl module Data::Dump"
    

关于perl - 在安装 conda 包期间更新 @INC 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56875121/

相关文章:

perl - 使用 $@ 传递模块中的错误消息

arrays - 计算和处理文本文件中的出现次数 (Perl)

node.js - 以内存为数据源的环回测试

python - 如何列出所有已安装的 Jupyter 内核?

python - 无法使用 conda install 安装 Vpython 和 Anaconda

Perl 字符串拆分和连接

perl - Dancer 中的重定向和恢复 STDERR

unix - 如何在UNIX ....中创建新的环境变量?

makefile - 生成文件中缺少 MACHTYPE

python - pip 安装与 conda 安装