Class: RubyPython::PythonExec
- Inherits:
-
Object
- Object
- RubyPython::PythonExec
- 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
-
#library ⇒ Object
readonly
The Python library.
-
#python ⇒ Object
readonly
The python executable to use.
-
#realname ⇒ Object
readonly
The real name of the python executable (with version).
-
#sys_prefix ⇒ Object
readonly
The sys.prefix for Python.
-
#version ⇒ Object
readonly
The Python version.
Instance Method Summary collapse
-
#initialize(python_executable) ⇒ PythonExec
constructor
Based on the name of or path to the Python executable provided, will determine:.
- #inspect ⇒ Object
-
#run_command(command) ⇒ Object
Run a Python command-line command.
- #to_s ⇒ Object
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
#library ⇒ Object (readonly)
The Python library.
120 121 122 |
# File 'lib/rubypython/pythonexec.rb', line 120 def library @library end |
#python ⇒ Object (readonly)
The python executable to use.
114 115 116 |
# File 'lib/rubypython/pythonexec.rb', line 114 def python @python end |
#realname ⇒ Object (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_prefix ⇒ Object (readonly)
The sys.prefix for Python.
118 119 120 |
# File 'lib/rubypython/pythonexec.rb', line 118 def sys_prefix @sys_prefix end |
#version ⇒ Object (readonly)
The Python version
122 123 124 |
# File 'lib/rubypython/pythonexec.rb', line 122 def version @version end |
Instance Method Details
#inspect ⇒ Object
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_s ⇒ Object
129 130 131 |
# File 'lib/rubypython/pythonexec.rb', line 129 def to_s @realname end |