Skip to content

Commit

Permalink
Merge pull request #322 from AlbertDeFusco/empty-init
Browse files Browse the repository at this point in the history
Empty environment on init
  • Loading branch information
AlbertDeFusco committed May 14, 2021
2 parents 731e91e + 409a856 commit a82a020
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 70 deletions.
14 changes: 14 additions & 0 deletions anaconda_project/env_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,3 +634,17 @@ def _anaconda_default_env_spec(shared_base_spec):
description="Default environment spec for running commands",
inherit_from_names=(),
inherit_from=inherit_from)


def _empty_default_env_spec(shared_base_spec):
if shared_base_spec is None:
inherit_from = ()
else:
inherit_from = (shared_base_spec, )
return EnvSpec(name="default",
conda_packages=[],
channels=[],
platforms=conda_api.default_platforms_with_current(),
description="Default environment spec for running commands",
inherit_from_names=(),
inherit_from=inherit_from)
8 changes: 4 additions & 4 deletions anaconda_project/internal/cli/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from anaconda_project.internal.cli.console_utils import (print_project_problems, console_ask_yes_or_no)


def init_command(project_dir, assume_yes, empty_environment):
def init_command(project_dir, assume_yes, with_anaconda_package):
"""Initialize a new project.
Returns:
Expand All @@ -24,7 +24,7 @@ def init_command(project_dir, assume_yes, empty_environment):
# --yes or we go with the default in project_ops.create
# (depends on whether project file already exists).
assert assume_yes is None or assume_yes is True
assert empty_environment is None or empty_environment is True
assert with_anaconda_package is None or with_anaconda_package is True

if not os.path.exists(project_dir):
if assume_yes:
Expand All @@ -37,7 +37,7 @@ def init_command(project_dir, assume_yes, empty_environment):
project = project_ops.create(project_dir,
make_directory=make_directory,
fix_problems=assume_yes,
empty_environment=empty_environment)
with_anaconda_package=with_anaconda_package)
if print_project_problems(project):
return 1
else:
Expand All @@ -47,4 +47,4 @@ def init_command(project_dir, assume_yes, empty_environment):

def main(args):
"""Start the init command and return exit status code."""
return init_command(args.directory, args.yes, args.empty_environment)
return init_command(args.directory, args.yes, args.with_anaconda_package)
6 changes: 5 additions & 1 deletion anaconda_project/internal/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,13 @@ def add_env_spec_name_arg(preset, required):

preset = subparsers.add_parser('init', help="Initialize a directory with default project configuration")
add_directory_arg(preset)
preset.add_argument('--with-anaconda-package',
action='store_true',
help="Add the 'anaconda' metapackage to the packages list.",
default=None)
preset.add_argument('--empty-environment',
action='store_true',
help="Do not add the default package set to the environment.",
help="[DEPRECATED] Do not add the default package set to the environment.",
default=None)
preset.add_argument('-y', '--yes', action='store_true', help="Assume yes to all confirmation prompts", default=None)
preset.set_defaults(main=init.main)
Expand Down
4 changes: 2 additions & 2 deletions anaconda_project/internal/cli/test/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def check(dirname):
with_directory_contents(dict(), check)


def test_init_empty_environment(capsys, monkeypatch):
def test_init_with_anaconda_package(capsys, monkeypatch):
def check(dirname):
_monkeypatch_pwd(monkeypatch, dirname)

code = _parse_args_and_run_subcommand(['anaconda-project', 'init', '--empty-environment'])
code = _parse_args_and_run_subcommand(['anaconda-project', 'init', '--with-anaconda-package'])
assert code == 0

assert os.path.isfile(os.path.join(dirname, DEFAULT_PROJECT_FILENAME))
Expand Down
3 changes: 0 additions & 3 deletions anaconda_project/internal/conda_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,6 @@ def _get_root_prefix():

def create(prefix, pkgs=None, channels=(), stdout_callback=None, stderr_callback=None):
"""Create an environment either by name or path with a specified set of packages."""
if not pkgs or not isinstance(pkgs, (list, tuple)):
raise TypeError('must specify a list of one or more packages to install into new environment')

if os.path.exists(prefix):
raise CondaEnvExistsError('Conda environment [%s] already exists' % prefix)

Expand Down
3 changes: 0 additions & 3 deletions anaconda_project/internal/default_conda_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,9 +435,6 @@ def fix_environment_deviations(self, prefix, spec, deviations=None, create=True)
# Create environment from scratch

command_line_packages = set(spec.conda_packages_for_create)
# conda won't let us create a completely empty environment
if len(command_line_packages) == 0:
command_line_packages = set(['python'])

try:
conda_api.create(prefix=prefix,
Expand Down
4 changes: 1 addition & 3 deletions anaconda_project/internal/test/test_conda_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ def test_conda_create_no_packages():
def do_test(dirname):
envdir = os.path.join(dirname, "myenv")

with pytest.raises(TypeError) as excinfo:
conda_api.create(prefix=envdir, pkgs=[])
assert 'must specify a list' in repr(excinfo.value)
conda_api.create(prefix=envdir, pkgs=[])

with_directory_contents(dict(), do_test)

Expand Down
4 changes: 2 additions & 2 deletions anaconda_project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from os.path import join

from anaconda_project.env_spec import (EnvSpec, _anaconda_default_env_spec, _find_importable_spec,
_find_out_of_sync_importable_spec)
_find_out_of_sync_importable_spec, _empty_default_env_spec)
from anaconda_project.requirements_registry.registry import RequirementsRegistry
from anaconda_project.requirements_registry.requirement import EnvVarRequirement
from anaconda_project.requirements_registry.requirements.conda_env import CondaEnvRequirement
Expand Down Expand Up @@ -1183,7 +1183,7 @@ def load_default_specs():
if importable_spec is not None:
return [importable_spec]
else:
return [_anaconda_default_env_spec(shared_base_spec=None)]
return [_empty_default_env_spec(shared_base_spec=None)]

self._project_file = ProjectFile.load_for_directory(directory_path,
default_env_specs_func=load_default_specs,
Expand Down
8 changes: 4 additions & 4 deletions anaconda_project/project_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

def create(directory_path,
make_directory=False,
empty_environment=False,
with_anaconda_package=False,
name=None,
icon=None,
description=None,
Expand All @@ -60,7 +60,7 @@ def create(directory_path,
Args:
directory_path (str): directory to contain anaconda-project.yml
make_directory (bool): True to create the directory if it doesn't exist
empty_environment (bool): True to create an empty base environment
with_anaconda_package (bool): True to add the 'anaconda' package
name (str): Name of the new project or None to leave unset (uses directory name)
icon (str): Icon for the new project or None to leave unset (uses no icon)
description (str): Description for the new project or None to leave unset
Expand All @@ -78,8 +78,8 @@ def create(directory_path,

project = Project(directory_path, scan_parents=False)

if empty_environment:
project.project_file.set_value('packages', [])
if with_anaconda_package:
project.project_file.set_value('packages', ['anaconda'])
if name is not None:
project.project_file.set_value('name', name)
if icon is not None:
Expand Down
14 changes: 9 additions & 5 deletions anaconda_project/test/test_project_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ def mock_codecs_open(*args, **kwargs):

assert sorted(list(project.env_specs.keys())) == sorted(['default'])
spec = project.env_specs['default']
assert spec.conda_packages == ('anaconda', )
assert spec.conda_packages == ()
assert spec.pip_packages == ()
assert spec.channels == ()

# Test the --empty-environment flag
project = project_ops.create(subdir, make_directory=True, empty_environment=True)
# Test the --with-anaconda-package flag
project = project_ops.create(subdir, make_directory=True, with_anaconda_package=True)
spec = project.env_specs['default']
assert spec.conda_packages == ()
assert spec.conda_packages == ('anaconda', )
assert spec.pip_packages == ()

with_directory_contents(dict(), check_create)
Expand Down Expand Up @@ -232,7 +232,11 @@ def check_create(dirname):

def test_create_imports_notebook():
def check_create(dirname):
project = project_ops.create(dirname, make_directory=False, name='hello', description="Hello World")
project = project_ops.create(dirname,
make_directory=False,
name='hello',
description="Hello World",
with_anaconda_package=True)
assert [] == project.problems
assert [] == project.suggestions
assert os.path.isfile(os.path.join(dirname, DEFAULT_PROJECT_FILENAME))
Expand Down
42 changes: 0 additions & 42 deletions docs/source/user-guide/tasks/create-project.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,45 +24,3 @@ Creating a project
see what the file looks like for an empty project. As you work
with your project, the ``anaconda-project`` commands you use
will modify this file.

By default the ``init`` command will add the ``anaconda`` metapackage
to the default environment. This metapackage will install over 200 of the
most commonly used data science and scientific computing packages.

The ``anaconda-project.yml`` file will include the following sections

.. code-block:: yaml
name: iris
packages:
- anaconda
channels: []
env_specs:
default:
description: Default environment spec for running commands
packages: []
channels: []
platforms: []
To create an ``anaconda-project.yml`` file with no default packages run::

anaconda-project init --empty-environment --directory iris

*******************
Prepare environment
*******************

Once the project directory and ``anaconda-project.yml`` file have been created
``cd`` into the new directory and install the packages::

anaconda-project prepare

This will create a new Conda environment in a subdirectory of your project
directory called ``envs/default``.

For more information about adding and removing packages and environments (``env_specs``)
see :ref:`Packages`.

See :ref:`Configuration` to change the default location of the Conda environments.
2 changes: 1 addition & 1 deletion docs/source/user-guide/tasks/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Tasks
:maxdepth: 1

create-project
work-with-packages
download-data
work-with-commands
work-with-packages
work-with-variables
run-project
clean-project
Expand Down

0 comments on commit a82a020

Please sign in to comment.