Updating the @INC variable during installation of a conda package

时光总嘲笑我的痴心妄想 提交于 2021-02-08 05:03:43

问题


I am trying to install a conda package of a Perl module. So far I'm able to create the package using conda-build. For that I have a recipe containing a build.sh and a meta.yaml files.

I then install it using conda-install in a new environment, I'd like to be able able to run some Perl scripts located in the Perl module I just installed.

All those steps work well but when I'm running some scripts I have an error saying :

Can't locate PMP/util.pm in @INC (you may need to install the PMP::util module) (@INC contains: /.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 .)

As you can see it seems that some module of my Perl module are not recognized when I execute Perl. I know that to fix this issue I can modify the @INC variable and add the bin/ to the PATH and the lib/ to the PERL5LIB but I need to automatize this process during the installation of the module.

I don't really know where I should modify the environment variable. During the creation of the package by adding something in the build.sh for example ? Or should I manage that during the installation and if so, how could I do that ?

Any suggestions would be greatly appreciated,

Thanks

Edit :

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}"

Edit 2 :

Another edit that could help you guys understand better my situation. I just realized that when I build the package the lib folder of my perl module in which I have PMP::util lives under lib/site_perl/5.26.0/Perl_Module. I'm pretty sure that if I'm able to install it directly under the lib/ folder it will resolve this issue. However I'm not sure how to modify the build.sh file to modify the place where we build the perl module.


回答1:


Here is a simple example of how to create a conda package that installs a Perl module (which depends on a CPAN module) that might help you solve your issue:

Installing miniconda on Linux

$ 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

Creating the package:

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 <hakon.hagland@gmail.com>',
    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

Note that I ran cpanm using perl ${PREFIX}/bin/cpanm. I was not able to simply run it as cpanm, see Can you rely on the shebang of an installed command during build? for more information.

Build the package

$ conda-build .

(Take a note of the generated output, and determine the path of the generated package. In my case the path name was:

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

Uploading the package to the anaconda server

  • Register a new user at Anaconda Cloud

  • Install the client

    $ conda install anaconda-client
    
  • Login to your account:

    $ anaconda login
    
  • Upload the generated package:

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

Testing the package (can be done from any linux machine):

  • Create a new enviroment:

     $ conda create --name perltest
     $ conda activate perltest
    
  • Install the package in the new environment:

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

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


来源:https://stackoverflow.com/questions/56875121/updating-the-inc-variable-during-installation-of-a-conda-package

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!