##// END OF EJS Templates
Merge pull request #11524 from jakevdp/pip-conda-magic...
Matthias Bussonnier -
r24888:a8165da9 merge
parent child Browse files
Show More
@@ -0,0 +1,103 b''
1 """Implementation of packaging-related magic functions.
2 """
3 #-----------------------------------------------------------------------------
4 # Copyright (c) 2018 The IPython Development Team.
5 #
6 # Distributed under the terms of the Modified BSD License.
7 #
8 # The full license is in the file COPYING.txt, distributed with this software.
9 #-----------------------------------------------------------------------------
10
11 import os
12 import re
13 import shlex
14 import sys
15 from subprocess import Popen, PIPE
16
17 from IPython.core.magic import Magics, magics_class, line_magic
18
19
20 def _is_conda_environment():
21 """Return True if the current Python executable is in a conda env"""
22 # TODO: does this need to change on windows?
23 conda_history = os.path.join(sys.prefix, 'conda-meta', 'history')
24 return os.path.exists(conda_history)
25
26
27 def _get_conda_executable():
28 """Find the path to the conda executable"""
29 # Check if there is a conda executable in the same directory as the Python executable.
30 # This is the case within conda's root environment.
31 conda = os.path.join(os.path.dirname(sys.executable), 'conda')
32 if os.path.isfile(conda):
33 return conda
34
35 # Otherwise, attempt to extract the executable from conda history.
36 # This applies in any conda environment.
37 R = re.compile(r"^#\s*cmd:\s*(?P<command>.*conda)\s[create|install]")
38 for line in open(os.path.join(sys.prefix, 'conda-meta', 'history')):
39 match = R.match(line)
40 if match:
41 return match.groupdict()['command']
42
43 # Fallback: assume conda is available on the system path.
44 return "conda"
45
46
47 CONDA_COMMANDS_REQUIRING_PREFIX = {
48 'install', 'list', 'remove', 'uninstall', 'update', 'upgrade',
49 }
50 CONDA_COMMANDS_REQUIRING_YES = {
51 'install', 'remove', 'uninstall', 'update', 'upgrade',
52 }
53 CONDA_ENV_FLAGS = {'-p', '--prefix', '-n', '--name'}
54 CONDA_YES_FLAGS = {'-y', '--y'}
55
56
57 @magics_class
58 class PackagingMagics(Magics):
59 """Magics related to packaging & installation"""
60
61 @line_magic
62 def pip(self, line):
63 """Run the pip package manager within the current kernel.
64
65 Usage:
66 %pip install [pkgs]
67 """
68 self.shell.system(' '.join([sys.executable, '-m', 'pip', line]))
69 print("Note: you may need to restart the kernel to use updated packages.")
70
71 @line_magic
72 def conda(self, line):
73 """Run the conda package manager within the current kernel.
74
75 Usage:
76 %conda install [pkgs]
77 """
78 if not _is_conda_environment():
79 raise ValueError("The python kernel does not appear to be a conda environment. "
80 "Please use ``%pip install`` instead.")
81
82 conda = _get_conda_executable()
83 args = shlex.split(line)
84 command = args[0]
85 args = args[1:]
86 extra_args = []
87
88 # When the subprocess does not allow us to respond "yes" during the installation,
89 # we need to insert --yes in the argument list for some commands
90 stdin_disabled = getattr(self.shell, 'kernel', None) is not None
91 needs_yes = command in CONDA_COMMANDS_REQUIRING_YES
92 has_yes = set(args).intersection(CONDA_YES_FLAGS)
93 if stdin_disabled and needs_yes and not has_yes:
94 extra_args.append("--yes")
95
96 # Add --prefix to point conda installation to the current environment
97 needs_prefix = command in CONDA_COMMANDS_REQUIRING_PREFIX
98 has_prefix = set(args).intersection(CONDA_ENV_FLAGS)
99 if needs_prefix and not has_prefix:
100 extra_args.extend(["--prefix", sys.prefix])
101
102 self.shell.system(' '.join([conda, command] + extra_args + args))
103 print("\nNote: you may need to restart the kernel to use updated packages.") No newline at end of file
@@ -2220,7 +2220,8 b' class InteractiveShell(SingletonConfigurable):'
2220 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2220 self.register_magics(m.AutoMagics, m.BasicMagics, m.CodeMagics,
2221 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2221 m.ConfigMagics, m.DisplayMagics, m.ExecutionMagics,
2222 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2222 m.ExtensionMagics, m.HistoryMagics, m.LoggingMagics,
2223 m.NamespaceMagics, m.OSMagics, m.PylabMagics, m.ScriptMagics,
2223 m.NamespaceMagics, m.OSMagics, m.PackagingMagics,
2224 m.PylabMagics, m.ScriptMagics,
2224 )
2225 )
2225 if sys.version_info >(3,5):
2226 if sys.version_info >(3,5):
2226 self.register_magics(m.AsyncMagics)
2227 self.register_magics(m.AsyncMagics)
@@ -24,6 +24,7 b' from .history import HistoryMagics'
24 from .logging import LoggingMagics
24 from .logging import LoggingMagics
25 from .namespace import NamespaceMagics
25 from .namespace import NamespaceMagics
26 from .osm import OSMagics
26 from .osm import OSMagics
27 from .packaging import PackagingMagics
27 from .pylab import PylabMagics
28 from .pylab import PylabMagics
28 from .script import ScriptMagics
29 from .script import ScriptMagics
29
30
@@ -179,7 +179,7 b' class BasicMagics(Magics):'
179 @line_magic
179 @line_magic
180 def lsmagic(self, parameter_s=''):
180 def lsmagic(self, parameter_s=''):
181 """List currently available magic functions."""
181 """List currently available magic functions."""
182 return MagicsDisplay(self.shell.magics_manager, ignore=[self.pip])
182 return MagicsDisplay(self.shell.magics_manager, ignore=[])
183
183
184 def _magic_docs(self, brief=False, rest=False):
184 def _magic_docs(self, brief=False, rest=False):
185 """Return docstrings from magic functions."""
185 """Return docstrings from magic functions."""
@@ -379,25 +379,6 b' Currently the magic system has the following functions:""",'
379 except:
379 except:
380 xmode_switch_err('user')
380 xmode_switch_err('user')
381
381
382
383
384 @line_magic
385 def pip(self, args=''):
386 """
387 Intercept usage of ``pip`` in IPython and direct user to run command outside of IPython.
388 """
389 print(textwrap.dedent('''
390 The following command must be run outside of the IPython shell:
391
392 $ pip {args}
393
394 The Python package manager (pip) can only be used from outside of IPython.
395 Please reissue the `pip` command in a separate terminal or command prompt.
396
397 See the Python documentation for more information on how to install packages:
398
399 https://docs.python.org/3/installing/'''.format(args=args)))
400
401 @line_magic
382 @line_magic
402 def quickref(self, arg):
383 def quickref(self, arg):
403 """ Show a quick reference sheet """
384 """ Show a quick reference sheet """
General Comments 0
You need to be logged in to leave comments. Login now