WSL paths in Windows

Windows Subsystem for Linux (WSL) allows you to install Linux distributions such as Ubuntu under Windows. The root filesystem you see under WSL is located at a directory inside %LOCALAPPDATA% in Windows.

For example, the root directory of Ubuntu 18.04 can be located in Windows here: %LOCALAPPDATA%\Packages\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\LocalState\rootfs

Advertisements

How to view path of Python module

To view the paths of an imported Python module, say tensorflow:

import tensorflow
print(tensorflow.__path__)

# Output is:
# ['/usr/local/lib/python3.6/site-packages/tensorflow/python/keras/api/_v1',
#  '/usr/local/lib/python3.6/site-packages/tensorflow_estimator/python/estimator/api/_v1',
#  '/usr/local/lib/python3.6/site-packages/tensorflow',
#  '/usr/local/lib/python3.6/site-packages/tensorflow/_api/v1']

This can be useful to check if the right module from the right location is being imported. It is also useful to know where to go check out the Python source files of a module for inspection or debugging.

How to convert Unix and Windows paths in Cygwin

Cygwin uses Unix paths. It can be sometimes confusing to figure out where in the Windows filesystem a Cygwin path is actually referring to. This is precisely why Cygwin ships with the cygpath tool.

  • To print the Windows path of a Cygwin Unix path:
$ cygwin -w /some/unix/path
C:\cygwin64\some\unix\path
  • To print the Windows path of a Cygwin Unix path in a DOS-compatible path format:
$ cygwin -d /some/unix/path
  • To find out how a Windows path can be accessed inside Cygwin as an Unix path:
$ cygwin -u "C:\Windows"
/cygdrive/c/Windows

How to autocomplete path in Vim insert mode

Vim path autocomplete in insert mode
Vim path autocomplete in insert mode

It is useful to be able to autocomplete a path to a directory or filename while typing code or script files. This helps in ensuring that there are no mistakes and the path is correct. Thankfully, Vim has an autocompletion feature for typing path in insert mode! It calls this feature as file name completion.

  • Ctrl-x Ctrl-f: This is the shortcut to autocomplete path in insert mode. Start typing a path (absolute or relative) and use this keyboard shortcut. You will see a dropdown menu filled with the next entries from the filesystem that can be used to fill the path.

  • Ctrl-n or Ctrl-p: Shortcuts to move up and down the entries in the autocomplete dropdown menu. If you move off the top or bottom of the dropdown menu, you get the empty entry.

  • Absolute path: Any path that you begin typing with a slash is autocompleted as an absolute path.

  • Relative path: This is any path that does not begin with a slash. They are autocompleted too. Note that they will use the directory that Vim was launched from (PWD) as the root.

  • Hidden files or directories: I found that this was autocompleted only if I typed the first period and then used Ctrl-x Ctrl-f.

Tried with: Vim 7.4 and Ubuntu 14.04

PATH environment variable in sudo

Using sudo is a common, safe and recommended method to execute commands that require superuser privileges. However, this command resets the PATH environment variable. So, some badly written installation scripts that require a particular PATH will fail in strange ways when run as sudo.

Here is some useful information about sudo and the PATH environment variable:

  • To ensure safety, sudo by default does not use the PATH environment variable of the user or that of root.

  • sudo also ignores the system-wide environment variables set in /etc/environment or in /etc/profile.d/*.sh.

  • The PATH variable for sudo is hardcoded to /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

  • If you really want sudo to pick up the system-wide PATH or other environment variables, then try this:

    1. Set the PATH or environment variable in a new file named /etc/profile.d/name_anything.sh using export
    2. Start root shell using sudo su -
    3. Check if your path is correct: echo $PATH
    4. Run the command that requires superuser privilege.
  • Reference:

Tried with: Ubuntu 14.04

Environment variables in Fish

Environment variables can be set just like any other variable in the Fish shell.

  • To export an environment variable to the shell, add it in ~/.config/fish/config.fish:
set -x FOO_ENV_VAR /home/joe/bin/some_foo_dir
  • If this a environment variable with many values, then set it as an array. In Fish, you set an array variable with a space-delimited series of values:
set -x FOO_ENV_VAR /home/joe/bin/some_foo_dir /usr/local/foo_dir

Under Bash, you might have set this same environment variable in ~/.bashrc as:

export FOO_ENV_VAR=/home/joe/bin/some_foo_dir:/usr/local/foo_dir
  • If the environment variable with many values already exists and you want to prepend or append a value to it, the technique is the same. For example, to prepend to PATH:
set -x PATH /home/joe/bin/some_foo_dir $PATH
  • Three special environment variables: PATH, CDPATH and MANPATH are treated differently by Fish. These arrive from the environment to Fish as colon-separated, they are converted to array so we can set them easily as shown above using space-delimiter and then they are sent back to the environment as colon-separated. This logic can be seen in the Fish source code file src/env.cpp here. This is also explained in this issue.

  • The problem now is that there are many other environment variables which need to be colon-delimited, but Fish does not do that. For example, the dynamic linker ld.so that is used to load up DLLs when a binary executes requires LD_LIBRARY_PATH environment variable to be colon-delimited. I got errors when this variable was set using space-delimiters: cannot open shared object file: No such file or directory

  • If you run into any problems with an environment variable that takes a series of values, then check back to see how it is set in traditional shells like Bash. If it is colon-delimited there, then you might need to make it colon-delimited in Fish too. But, do remember to enclose it in double quotes for variable expansion to work correctly. For example:

set -x FOO_ENV_VAR "/home/joe/bin/some_foo_dir:$FOO_ENV_VAR"

Tried with: Fish 2.2 and Ubuntu 14.04

Autojump path repeats in PATH in Fish

Problem

Autojump can be installed and used with Fish shell as described here. However, if you source your Fish config file repeatedly, then the autojump path appears repeated in your PATH environment variable:

$ echo $PATH
/home/joe/.autojump/bin /home/joe/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin
$ source ~/.config/fish/config.fish
$ echo $PATH
/home/joe/.autojump/bin /home/joe/.autojump/bin /home/joe/bin /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin

Solution

I have a commit here that fixes this behavior. This pull request hopefully be merged into Autojump and if you get the latest version of Autojump you should no longer face this problem.

Tried with: Autojump 22.2.4, Fish 2.2 and Ubuntu 14.04

Pip install error with PyCUDA

Problem

  • I tried to install PyCUDA using pip:
$ sudo pip install pycuda
  • The installation tries to compile a few C++ files and it failed on the very first file with this error:
In file included from src/cpp/cuda.cpp:1:0:
src/cpp/cuda.hpp:14:18: fatal error: cuda.h: No such file or directory
#include <cuda.h>
                ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Investigation

  • This error was strange because I had set CUDA_ROOT and had added the bin path of CUDA installation to PATH environment variable. So, the installer should have found cuda.h which I could see was present in $CUDA_ROOT/include

  • To see what was happening, I tried the same command with verbosity:

$ sudo pip -vvv install pycuda
  • Now I could see that it was failing to find nvcc.

  • On downloading the source code of PyCUDA and checking setup.py, I saw that the check for nvcc was used to figure out the CUDA_ROOT and CUDA_INC_DIR.

  • The reason nvcc was not visible was that CUDA_ROOT was set for my user, but this PATH is not visible when a command is run under sudo, as described here. The solution was to make the CUDA bin path visible to sudo.

Solution

To make the $CUDA_ROOT/bin available in PATH for sudo, we can follow the steps described here. For example, on my system with CUDA 7.0 I followed these steps:

  • Created a new file /etc/profile.d/cuda.sh and added this line:
export PATH=/usr/local/cuda-7.0/bin:$PATH
  • Opened root shell without resetting PATH and ran the pip installation:
$ sudo su -
$ pip install pycuda

This worked and PyCUDA was installed successfully! 🙂

Tried with: PyCUDA 2015.1.2, CUDA 7.0 and Ubuntu 14.04

How to view full path in statusline of Vim

This post shows how to quickly view the full path of the currently open file in Vim.

Some of you might like have the full path of file always be shown in the statusline. To do this set the %F item in the statusline. For example:

set statusline += '%F'

If you are using the vim-airline plugin, you can display the full path in its statusline by replacing the %f in its g:airline_section_c variable with %F. This post shows how you can view the current value of this variable.

Tried with: Vim 7.3 and Ubuntu 12.04 LTS