Wednesday, June 25, 2014

Using Virtualenv with SublimeREPL

Setup:

  • Sublime Text 3
  • SublimeREPL (2.1.1) 
  • Virtualenv (1.11.6)
  • Mac OS X 10.7.5
  • System Python (2.7.1) vs. Macports-installed Python (2.7.7)
Problem:
  1. I want the SublimeREPL: Python to use a virtual environment
  2. I want to use a different version of Python for my virtual environment
Note:
If you're a complete beginner at this, anything I wrote with ~$ means that's something done in Terminal.

Solution:

For problem #1: I want the SublimeREPL: Python to use a virtual environment
  • From the SublimeREPL docs, it says you *must* use some Virtualenv wrapper for SublimeREPL to recognize your virtual environments.
  • The docs suggested either virtualenvwrapper or venv.  I used venv, which works for me. I didn't try virtualenvwrapper.
  • venv was easy to use. Just download the venv.bash file and save it anywhere you like and run it.
    • For example, if you saved it in your home directory ~/, make sure the file is executable. If not, make it executable by: ~$ chmod +x venv.bash  To run it, do:

      ~$ source venv.bash
  • Now, you can create your virtual environment. Let's say we want to make the environment called 'venv0':

    ~$ venv create venv0

    Notice, it will also automatically activate the environment:

    (venv0) ~$_
  • The environment resides in:

    /Users/your_username/.venv/venv0

    *keep in mind I'm using OS X 10.7
  • To start using the myenv environment from SublimeREPL (assuming it's already installed. If not, follow the SublimeREPL link on top):
    • Launch Sublime Text 3
    • Do: CMD+SHIFT+p
    • Find the "SublimeREPL: Python - virtualenv" item from the list. (Shortcut: in the text field, start typing "virtualenv")
    • Select "SublimeREPL: Python - virtualenv" (click, or use arrow key and hit enter if it's highlighted)
    • Now venv0 ("/Users/your_username/.venv/venv0/bin/") should be showing in the view.
    • Select venv0, and SublimeREPL will launch with the environment activated.

For problem #2: I want to use a different version of Python for my virtual environment
  • Note: you cannot change your Python version if you already created your virtual environment. You will have to create a new environment, and reinstall all your module in the new environment.
  • I have the default system Python (2.7.1), and just recently installed a newer version of Python (2.7.7) using Macports.
  • The Macports' version of Python is installed in:

    /opt/local/bin/python2.7
  • To use this alternative Python in your virtual environment, use the argument -p [path_to_python] with Virtualenv. For example

    ~$ virtualenv -p /opt/local/bin/python2.7 myenv

    Don't worry - it doesn't mean that the environment will start using a 'global' Python; it will still use the Python located in myenv/bin, but now that Python is the same version as the one in the path.
  • To do this with venv, just modify the venv.bash file (using your text editor of choice). Find the line:

    virtualenv $@ --no-site-packages --distribute --prompt='$(__venv_prompt)'  "$VENV_DIR/$env"

    and change it to (emphasis added):

    virtualenv -p /opt/local/bin/python2.7 $@ --no-site-packages --distribute --prompt='$(__venv_prompt)'  "$VENV_DIR/$env"

    And save the file.
  • For the changes to take effect, re-run the venv.bash again.

    ~$ source venv.bash
  • Now, create another environment:

    ~$ venv create venv1
    (venv1) ~$_


    Run Python:

    (venv1) ~$ python
  • You should see now venv1 is using the alternative Python version (in this case: 2.7.7). If you use the other environment (venv0), you can check that it's using 2.7.1.
  • Finally, you can use venv1 from SublimeREPL as well.

Additional notes:
  • To quit/exit/deactivate the virtual environment from venv:

    (venv0) ~$ deactivate
  • To use/activate a virtual environment (e.g. venv0) using venv:

    ~$ venv use venv0
  • To delete a virtual environment (e.g. venv0) using venv:

    ~$ venv destroy venv0

CAVEAT:
The big 'gotcha' here is that Sublime/SublimeREPL has its own weird way with the environment path. If you're using a virtual environment, SublimeREPL have no problem accessing any Python modules installed within the virtual environment.

If you have a global module you want to use, or a custom module you just made, you would have to add the path manually. I.e. (in SublimeREPL):

>>>  import sys
>>>  sys.path.append('/your_path/to/the/module')

I have not found a way to set the path to the libraries outside of the virtual environment to be included when launching SublimeREPL. Please let me know if you do.

Friday, June 13, 2014

Sublime Text 3, SublimeREPL, and Python modules on Mac OS X 10.7

Setting:
  • Mac OS X 10.7.5
  • Sublime Text 3
  • SublimeREPL 2.1.1
Problem:
  • I have globally-installed Python modules (e.g. Pylab, Matplotlib, Scipy) that I cannot import when using SublimeREPL: "no module named pylab".  I can import pylab from Python REPL from the Terminal just fine.
  • I tried adding the path to the module, but it complains with RuntimeError:
* I noticed this is the exact Python version referred to by the asker in this StackOverflow thread

Issue:

SublimeREPL seems to have some weird default configuration (honestly, I still don't quite understand why this is so) which makes it unable to point to my PYTHONPATH correctly. For example, I don't understand why in the screenshot above it said "module compiled against API version 6 but version of numpy is 4" while I have no problem importing matplotlib when using Python REPL in the Terminal. If anyone have a good explanation what that is so, that would be very much appreciated.

Solution:

Change the path to the Python executable.
  1. Open the SublimeREPL settings file for Python (it doesn't need to be done from Sublime Text, but it's convenient):
  2. Around line 22, add the "cmd" value with the desired Python executable. The path for my system is: "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin/python" (from: this SO answer)
  3. The file is located in: /Users//Library/Application Support/Sublime Text 3/Packages/SublimeREPL/config/Python/Main.sublime-menu
    (Remember, this is Mac OS X 10.7.5)



  4. This is weird; if I comment out line 22: "cmd": ["python", "-i", "-u"], SublimeREPL won't launch. Both lines must be used. Again, I apologize for not having an explanation why it has to be this way.
  5. Save the file.
  6. Try opening the SublimeREPL for Python
  7. Now I was able to import all the modules I need:


    Notice how it now says "Python 2.7.6".
Hope that helps.



Resources/Similar Issues:

Saturday, March 1, 2014

VirtualBox Virtual Drive not Expanding!

Setup:


  • VirtualBox 4.3.6 r91406
  • Host OS: Mac OS X 10.7.5
  • Guest OS: Ubuntu 12.04 32-bit

Problem:

I have an Ubuntu guest OS with a Dynamically allocated storage type virtual harddrive (.vdi). Initially, the capacity of the drive was 8GB, but after installing a couple of stuff, Ubuntu complains that there was no more space left!

Troubleshooting:

So I loaded Gparted to check the drive. Turns out, the virtual harddrive were partitioned into 6GB of primary partition and 2GB of swap. Not sure who did this (VirtualBox or Ubuntu), but reading around indicates that Ubuntu does this. To this day, I still don't know WHY VirtualBox didn't automatically expand the drive as I expected it to, which it did in the past (previous versions of VirtualBox). I couldn't find the answer online, but if anyone knows, please let me know.

Solution:

NOTE: this only applies to dynamically-allocated storage types. If your virtual harddrive was created as a fixed storage type, this does not apply.

1. Resize the drive using VBoxManage modifyhd

Use the VBoxManage modifyhd command in Terminal to manually resize the drive. 

 VBoxManage modifyhd     uuid|filename  
                         [--type normal|writethrough|immutable|shareable|  
                         readonly|multiattach]  
                         [--autoreset on|off]  
                         [--compact]  
                         [--resize megabytes|--resizebyte bytes]  
( --resize units in megabytes, --resizebytes units in bytes)
[source]

In my case, I resized the .vdi to 15GB (15360 MB):
  path_to_my_vdi$ VBoxManage modifyhd Ubuntu1204.vdi --resize 15360  

Output:
 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%  


2. Rearrange the partition using GParted

Just in case, you may want to backup your data if necessary. (Sorry, no screenshots for now)
  1. Shut down the Guest OS if not already
  2. Mount the GParted .iso file to your virtual CD/DVD drive
  3. Make sure the CD/DVD drive is prior to the Virtual Hard Drive in the boot sequence
  4. Boot the virtual machine
  5. GParted will ask a few options as it loads. Just hit Enter to all of them to get through the options - I think that works in most cases.
  6. Once GParted loads, you can see the partitions in the virtual drive
  7. I deleted my swap partition (Note: don't worry - none of these changes will be made to the drive until "Apply" is clicked. So it's safe to make/undo any changes.)
  8. Then I increased the capacity of my primary partition using the Resize/Move button (or right-click on the partition and choose Resize/Move).
  9. I left 2GB at the end of the partition as swap. To do this:
    1. Since my capacity is now 15GB, I resized my primary partition to 13GB
    2. Then, the remaining 2GB will say "Unallocated". Click on that partition and click "New" from menu.
    3. On the "New" window, on the lower right side, choose "swap" as the partition type.
  10. On the lower half of the GParted window, there's the list of actions queued. If you're happy with the results, hit "Apply" to apply the changes (NOTE: there's no turning back after this point!).
And that's how I work around manually resizing my dynamically allocated storage virtual hard drive in VirtualBox 4.3.6.