Install homebrew without sudo in MacOS

Homebrew needs /usr/local to be chown-ed to your user, and you need sudo for that. If you can’t you have to install it elsewhere. Some people use ~/.brew or ~/homebrew; you can use anything but avoid paths with spaces.

Let’s say you want to install in ~/.brew; run the following command:

git clone --depth=1 https://github.com/Homebrew/brew ~/.brew

Then ensure the bin and sbin directories are in your PATH. If you’re using Bash add the following in your ~/.bash_profile:

export PATH="$HOME/.brew/bin:$HOME/.brew/sbin:$PATH"

Run source ~/.bash_profile or restart your shell and run brew doctor to see if it’s installed correctly. It should warn you it’s not installed into /usr/local but that’s expected here.

binding.gyp not found while trying to load binding.gyp mac

Check the error you get while executing: node-gyp rebuild

If it is a permission denied then retry the command with sudo.
If the error is still persistent execute the following:

xcode-select –install

Keyboard shortcuts giving me errors in tkinter

I am trying to create a text editor with python 3 and tkinter. The text editor works great except for when I try to use my keyboard shortcuts. Whenever I use any of the shortcuts, I get an error that says this:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 1699, in __call__
    return self.func(*args)
TypeError: newFile() takes 0 positional arguments but 1 was given

newFile() can be replaced with copySelected(), selectAll(), or whatever command I am trying to use. This only happens when I am trying to use the key bindings. It works just fine from the menu bar. The wierd thing is that when I am cutting, copying, or pasting I get the error they actually work in the app. Here is the key binding code:

textField.bind("<Command-n>", newFile)
textField.bind("<Command-N>", newFile)
textField.bind("<Command-o>", openFile)
textField.bind("<Command-O>", openFile)
textField.bind("<Command-s>", saveFile)
textField.bind("<Command-S>", saveFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-n>", newFile)
textField.bind("<Command-z>", undo)
textField.bind("<Command-Z>", undo)
textField.bind("<Command-Shift-z>", redo)
textField.bind("<Command-Shift-Z>", redo)
textField.bind("<Command-x>", cutSelected)
textField.bind("<Command-X>", cutSelected)
textField.bind("<Command-c>", copySelected)
textField.bind("<Command-C>", copySelected)
textField.bind("<Command-v>", paste)
textField.bind("<Command-V>", paste)
textField.bind("<Command-a>", selectAll)
textField.bind("<Command-A>", selectAll)

I am currently testing the code on Mac OS but I have already made the code os specific so that it will work on Windows and Linux as well. The Windows and Linux code is exactly the same other than the fact that Command is replaced with Control. The error occurs on all three of the platforms.

Any help is greatly appreciated. Thanks!

Solution:

When you bind a key to the function, tkinter will automatically pass an object to the callback. This object represents the event that caused the callback to be called. It has information such as the widget that received the event, the x and y coordinate of the mouse, and other details unique to the event (mouse button, keyboard character, etc).

When you bind a function to an event, your function must be able to accept this parameter. For example:

def newFile(event):
    ...

Note that this is different than if you call the function via the command attribute of a widget. In that case no event object is passed. If you want to be able to call the function both via a binding and via a command attribute then you can make the parameter optional (and make sure that your function doesn’t actually attempt to use it, since it may not be present):

def newFile(event=None):
    ...

">" not working to direct output of python command to file

I have decided to try snakefood to help with a refactoring to check the imports. It keeps dumping output on the screen and “>” does not send it to a file, it just creates an empty file.

I had to unfortunately create a virtualenv with Python 2.7 as it probably does not work properly in Python 3. Still, it can probably check a Python 2 project, even though it is written in Python 2. Am using a Mac, but it seems to use similar commands to Linux on the command line.

I did

pip install six
pip install graphviz
pip install snakefood

once the Python 2 environment was activated.

Then if I type

$ sfood-checker path/to/folder

..it dumps a huge amount of text on the screen, but

$ sfood-checker path/to/folder > check.txt

..only creates an empty file. Any idea what is wrong, how to fix it? Would like to carefully go through the file in sublime.

Solution:

You are redirecting stdout, but your program is writing to stderr. The fix is to redirect stderr:

$ sfood-checker path/to/folder 2> check.txt

Or redirect both stdout and stderr:

$ sfood-checker path/to/folder &> check.txt

Background: when processes are initially created, they generally always have three initial file descriptors already opened for them:

  • 0, stdin, “Standard Input”, a read-only stream
  • 1, stdout, “Standard Output”, a write-only stream
  • 2, stderr, “Standard Error”, a write-only stream

There is precisely zero difference between stdout and stderr, other than convention and the file descriptor number. By convention, then, status messages and other “informational” content is output to stderr (some version of fwrite(stderr, informational_data);, and the data required for normal operations of the program are written to stdout.

How to define path to import python library

I have a MacBook Pro and successfully installed xgboost on it. If I start a Python shell in ~/xgboost/python-package I can easily import xgboost by running the command:

import xgboost

But if I go to some other folder, start the python shell and run the above command I get the following error:

ImportError: No module named xgboost

I am guessing this is related to $PATH. How can I fix it?

Thanks.

Solution:

You have to set the directory in the pythonpath so python knows where to look at:

export PYTHONPATH=$PYTHONPATH:/your/path/to/your/module

Bash delete files from path that contain space

#!/bin/bash

files_path="/Volumes/HDD/Bogdan Data/"

rm -r "$files_path/files/*"

I’m getting an error that path cannot be found, probably due to the space character in the folder name. How should I approach this?

Solution:

$files_path needs to be quoted, but the * glob must not be.

rm -r "$files_path"/files/*