How to Dynamically Add to $Path on Linux/Os X

how to dynamically add to $PATH on Linux / OS X

First create an empty file in your home directory, this file will be a place to collect all the new additions to your path, so

touch ~/.build_path

Next you need to ensure that all your new path additions are processed when your ~/.bashrc file is processed, so add this line to your ~/.bashrc file:

source ~/.build_path

Finally, add this function into your ~/.bashrc file, this function makes an immediate change to the current PATH setting, and adds a new entry to the ~/.build_path file so that future shells will pick up the new path.

function addpath
{
echo "export PATH=\"$1\":\${PATH}" >> ~/.build_path
export PATH=$1:$PATH
}

That should pretty much do it. The only obvious problem is that if you have two running shells changing the path in one shell will not cause the path in the second to be updated, you'd need to restart the second shell.

How I could add dir to $PATH in Makefile?

Did you try export directive of Make itself (assuming that you use GNU Make)?

export PATH := bin:$(PATH)

test all:
x

Also, there is a bug in you example:

test all:
PATH=bin:${PATH}
@echo $(PATH)
x

First, the value being echoed is an expansion of PATH variable performed by Make, not the shell. If it prints the expected value then, I guess, you've set PATH variable somewhere earlier in your Makefile, or in a shell that invoked Make. To prevent such behavior you should escape dollars:

test all:
PATH=bin:$$PATH
@echo $$PATH
x

Second, in any case this won't work because Make executes each line of the recipe in a separate shell. This can be changed by writing the recipe in a single line:

test all:
export PATH=bin:$$PATH; echo $$PATH; x

dynamic absolute path symlinks

Your original idea to use relative linking was perfectly appropriate, but most likely just wasn't implemented correctly. To correctly create relative symlinks:

  • Given directory structure:

    SiteName
    docroot
    folder
  • Your current working dir: SiteName

  • You want: docroot/folder1 -> docroot/folder

Try:

$ ln -s folder docroot/folder1

If you had the tree program, you could see the structure, as well as the symlink folder1 successfully pointing to docroot's folder:

$ tree
.
└── docroot
├── folder
└── folder1 -> folder

3 directories, 0 files

Explanation

Your original attempt at relative links failed likely due to a common misunderstanding about what is required for the relative link:

Shadur's Unix & Linux answer mentions:

Symbolic links are relative to the location the link is in, not the location you were when you created the link. ...

So,

  • From folder1's perspective, relative path to folder is simply folder since they are siblings of the same directory
  • The command syntax is ln -s <relative path> <where to create new link>
  • When running the command from SiteName, the new link would be created at docroot/folder1

So the final command is ln -s folder docroot/folder1 to correctly create at docroot/folder1 to point to folder within the same directory.

So it will now work as long as you don't change their relative locations.

Relative paths based on file location instead of current working directory

What you want to do is get the absolute path of the script (available via ${BASH_SOURCE[0]}) and then use this to get the parent directory and cd to it at the beginning of the script.

#!/bin/bash
parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )

cd "$parent_path"
cat ../some.text

This will make your shell script work independent of where you invoke it from. Each time you run it, it will be as if you were running ./cat.sh inside dir.

Note that this script only works if you're invoking the script directly (i.e. not via a symlink), otherwise the finding the current location of the script gets a little more tricky)

Python: Best way to add to sys.path relative to the current running script

If you don't want to edit each file

  • Install you library like a normal python libray

    or
  • Set PYTHONPATH to your lib

or if you are willing to add a single line to each file, add a import statement at top e.g.

import import_my_lib

keep import_my_lib.py in bin and import_my_lib can correctly set the python path to whatever lib you want

In Python script, how do I set PYTHONPATH?

You don't set PYTHONPATH, you add entries to sys.path. It's a list of directories that should be searched for Python packages, so you can just append your directories to that list.

sys.path.append('/path/to/whatever')

In fact, sys.path is initialized by splitting the value of PYTHONPATH on the path separator character (: on Linux-like systems, ; on Windows).

You can also add directories using site.addsitedir, and that method will also take into account .pth files existing within the directories you pass. (That would not be the case with directories you specify in PYTHONPATH.)



Related Topics



Leave a reply



Submit