简体   繁体   中英

How to create a Relocatable Conda Environment? Is it doable?

I would like to make a relocatable environment. So I need to use relative paths in the package installations. For this I just create a Conda Environment like this:

conda create --prefix env python=3.6.5
activate .\env

And then I have installed the needed packages as usual with

pip install package_name

The problem comes when I want to install my own package. I have created a structure like this and I have followed this tutorial :

some_root_dir/
|-- setup.py
|-- python_files
|-- |-- runall.py
|-- |-- test0.py

And the content of the setup.py is this:

from setuptools import setup

setup(
    name='my_app',
    version='0.1',
    description='My app',
    keywords="app csv some other word",
    url='https://www.my_domain.com/',
    author='My name',
    author_email='email@email_domain.com',
    license='MIT',
    packages=['my_package'],
    zip_safe=False,
)

But after the installation with:

cd some_root_dir
pip install .

and moving it to another location, the paths that are appearing in the application are the ones where I did the pip install .

I have been looking for information here , but I did not find anything useful.

Main Steps I want to do

  1. Create a conda environment and install some packages with pip or conda, my own python package included
  2. Copy the environment folder to another computer
  3. Run the application in this computer where conda and python are not installed. If I use the python.exe included in the folder python should know where the packages are installed and how to import them.

Questions

  • How can I use relative paths in the environment packages?
  • Is this doable? Or am I doing anything wrong?
  • Which are the best practices to achieve what I want?
  • Are the relocatable environments possible?

Note : I am using Windows 10 and Miniconda 3.

Virtualenv

The equivalent on virtualenv would be this :

virtualenv --relocatable env_folder

But it is an experimental feature

Update (August 7, 2018)

Actually what I want is what @interfect says in his comment, the issue is here . So relocatable environments on conda are not possible yet

I think that relocatable environments depend on the installed packages. They should be implemented with relative paths and avoiding hardcoded paths. All the paths that are used in the source code of the package should be inside the own package. So if you install well done package you won´t have any problem to relocate the environment in other folder or computer.

As you will need to add all the folders inside the package you will need to modify the arguments of the setup . Add these two parameters in order to add folders to the final package. If you don´t do this the folders won´t copied to the site-packages folder within the environment (the final destination when you install the package with pip ):

packages=[
    'main_folder',
    'main_folder.folder_with_python_files',
    'main_folder.other_folder_with_python_files',
],
package_data={
    'main_folder': [
        'static/css/*.*',
        'templates/*.*',
    ],
},

Environments, Package Manager and Paths

I have tried to build the environment on Windows with Virtualenv , but I had some problems building a basic environment:

  • There was a dll library missing: VCRUNTIME140.dll
  • The runpy module was missing as well. This is used to run commands with the -m parameter: python -m ...
  • Other packages dependencies were not installed when I used pip such as zipfile

So I came back to Conda Environments again, but I have built the environment with package manager pip , instead of conda , because the packages were much lighter in my case.

Therefore, my recommendation is to install the packages with pip . If any of them are giving problems after the relocation, we should check if there is any harcoded path and change it directly. Though the best solution would be to modify the original source code and install the customised package.

Some python scripts in the environment had the absolute path on the header with #! .

#!C:\absolute\path\to\python.exe

I just removed them because if I call any script with the python.exe that is currently inside the environment those headers are ignored

Update

Also conda-pack can be useful. I haven´t tried it yet

conda-pack is a command line tool for creating relocatable conda environments. This is useful for deploying code in a consistent environment, potentially in a location where python/conda isn't already installed.

If you turn your package into a conda-package (trivial if you are using pip already), you just conda install your packages on the new machine and everything will be relocated at install time.

That includes any compiled libraries let alone the paths in scripts. Conda will modify everything so it just works no matter where you install it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM