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
29
30
31
32
# 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]"

  @dirname = File.dirname(@python)
  @realname = @python.dup
  if (@realname !~ /#{@version}$/ and @realname !~ /\.exe$/)
    @realname = "#{@python}#{@version}"
  else
    basename = File.basename(@python, '.exe')
    @realname = File.join(@dirname, "#{basename}#{@version.gsub(/\./, '')}")
  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.



120
121
122
# File 'lib/rubypython/pythonexec.rb', line 120

def library
  @library
end

#pythonObject (readonly)

The python executable to use.



114
115
116
# File 'lib/rubypython/pythonexec.rb', line 114

def python
  @python
end

#realnameObject (readonly)

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



116
117
118
# File 'lib/rubypython/pythonexec.rb', line 116

def realname
  @realname
end

#sys_prefixObject (readonly)

The sys.prefix for Python.



118
119
120
# File 'lib/rubypython/pythonexec.rb', line 118

def sys_prefix
  @sys_prefix
end

#versionObject (readonly)

The Python version



122
123
124
# File 'lib/rubypython/pythonexec.rb', line 122

def version
  @version
end

Instance Method Details

#inspectObject



133
134
135
136
137
138
139
# File 'lib/rubypython/pythonexec.rb', line 133

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

#run_command(command) ⇒ Object

Run a Python command-line command.



125
126
127
# File 'lib/rubypython/pythonexec.rb', line 125

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

#to_sObject



129
130
131
# File 'lib/rubypython/pythonexec.rb', line 129

def to_s
  @realname
end