Class: RubyPython::PythonExec

Inherits:
Object
  • Object
show all
Defined in:
lib/rubypython/pythonexec.rb

Overview

A class that represents a Python executable.

End users may get the instance that represents the current running Python interpreter (from RubyPython.python), but should not directly instantiate this class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(python_executable) ⇒ PythonExec

Based on the name of or path to the Python executable provided, will determine:

  • The full path to the Python executable.

  • The version of Python being run.

  • The system prefix.

  • The main loadable Python library for this version.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rubypython/pythonexec.rb', line 14

def initialize(python_executable)
  @python = python_executable || "python"
  @python = %x(#{@python} -c "import sys; print sys.executable").chomp

  @version = run_command 'import sys; print "%d.%d" % sys.version_info[:2]'

  @realname = @python.dup
  if @realname !~ /#{@version}$/
    @realname = "#{@python}#{@version}"
  end
  @basename = File.basename(@realname)

  @sys_prefix = run_command 'import sys; print sys.prefix'
  @library = find_python_lib
end

Instance Attribute Details

#libraryObject (readonly)

The Python library.



98
99
100
# File 'lib/rubypython/pythonexec.rb', line 98

def library
  @library
end

#pythonObject (readonly)

The python executable to use.



92
93
94
# File 'lib/rubypython/pythonexec.rb', line 92

def python
  @python
end

#realnameObject (readonly)

The real name of the python executable (with version).



94
95
96
# File 'lib/rubypython/pythonexec.rb', line 94

def realname
  @realname
end

#sys_prefixObject (readonly)

The sys.prefix for Python.



96
97
98
# File 'lib/rubypython/pythonexec.rb', line 96

def sys_prefix
  @sys_prefix
end

#versionObject (readonly)

The version



100
101
102
# File 'lib/rubypython/pythonexec.rb', line 100

def version
  @version
end

Instance Method Details

#inspectObject



111
112
113
114
115
116
117
# File 'lib/rubypython/pythonexec.rb', line 111

def inspect
  if @python
    "#<#{realname} #{sys_prefix}>"
  else
    "#<invalid interpreter>"
  end
end

#run_command(command) ⇒ Object

Run a Python command-line command.



103
104
105
# File 'lib/rubypython/pythonexec.rb', line 103

def run_command(command)
  %x(#{@python} -c '#{command}').chomp if @python
end

#to_sObject



107
108
109
# File 'lib/rubypython/pythonexec.rb', line 107

def to_s
  @realname
end