Skip to content
This repository has been archived by the owner on Dec 29, 2022. It is now read-only.

PYTHONPATH needs to be exported #2

Open
sherpya opened this issue Jun 12, 2015 · 15 comments
Open

PYTHONPATH needs to be exported #2

sherpya opened this issue Jun 12, 2015 · 15 comments

Comments

@sherpya
Copy link

sherpya commented Jun 12, 2015

the .sh script does not export PYTHONPATH, you need to export it using export PYTHONPATH or prepending it to exec call

@Storyyeller
Copy link
Contributor

That's weird. It works when I try it. Does this depends on the system?

@dwayneyuen
Copy link

Mine also needs to export PYTHONPATH, otherwise I get this error:
"Error while finding spec for 'enjarify.main' (<class 'ImportError'>: No module named 'enjarify')"

I'm using Python 3.4.3 on OSX Yosemite.

@Storyyeller
Copy link
Contributor

Maybe it's an issue with Macs? Unfortunately, I don't have any to test on.

@sherpya
Copy link
Author

sherpya commented Jun 15, 2015

no, it's bash (I'm on linux), you need to explicitly export or put on the exec command line, you may already have set PYTHONPATH, so your system does not expose the problem

@pwelyn
Copy link

pwelyn commented Jun 16, 2015

I'm using Python 3.4.3 on OSX 10.10.3
ln -s "$PWD/enjarify.sh" ~/bin/enjarify
Using python3 as Python interperter
/usr/local/bin/python3: Error while finding spec for 'enjarify.main' (<class 'ImportError'>: No module named 'enjarify')

@Storyyeller
Copy link
Contributor

It looks like I have PYTHONPATH set to /usr/local/buildtools/current/sitecustomize, but I can't imagine how that would make a difference.

It's weird that on your system you have to explicitly export it, while on mine, merely setting it seems to be enough.

@dwayneyuen
Copy link

Are you running from the enjarify directory or somewhere else and using a symlink?

@Storyyeller
Copy link
Contributor

I'm running from elsewhere using a symlink.

Anyway, I went ahead and added export. Does it work now?

@dwayneyuen
Copy link

Working for me. :)

@moneytoo
Copy link

Well, the output of readlink is blank so it prevents me from calling enjarify from any outside dir (it's same for both macOS/Linux).

echo $(dirname "$(readlink "${BASH_SOURCE[0]}")")

prints .

@Storyyeller Storyyeller reopened this Sep 11, 2016
@tiann
Copy link

tiann commented Dec 9, 2016

readlink doesn't work for me, too. my OS version is Mac OSX 10.11; I change it to this:

$(cd dirname $0; pwd)

And it worked for me.

@ybf326
Copy link

ybf326 commented Mar 14, 2017

Copy the following to enjarify.sh, you can solve this problem. good luck.

function guess {
	if [ -z "$PYTHON" ]; then
		result=$($1 -c "print(range)" 2>/dev/null)
		if [ "$result" = "<class 'range'>" ]; then
			PYTHON=$1
		fi
	fi
}

guess "pypy3"
guess "python3"
guess "pypy"
guess "python"

#Gets the root path of the current script
getCurrentShellRootPath()
{
oldPath=`pwd`
SOURCE="$1"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR2="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
echo ${DIR2}
cd $oldPath
}

if [ -z "$PYTHON" ]; then
	echo "Unable to find python3 on path"
else
	echo "Using $PYTHON as Python interpreter"

	# Find location of this bash script, and set its directory as the PYTHONPATH
	#export PYTHONPATH=$(dirname "$(readlink "${BASH_SOURCE[0]}")")
	#Modify the path to PYTHONPATH
	shellRootPath=`getCurrentShellRootPath $0`
	export PYTHONPATH=${shellRootPath}
	

	# Now execute the actual program
	exec $PYTHON -O -m enjarify.main "$@"
fi

@AzenHuang
Copy link

Copy the following to enjarify.sh, you can solve this problem. good luck.

function guess {
	if [ -z "$PYTHON" ]; then
		result=$($1 -c "print(range)" 2>/dev/null)
		if [ "$result" = "<class 'range'>" ]; then
			PYTHON=$1
		fi
	fi
}

guess "pypy3"
guess "python3"
guess "pypy"
guess "python"

#Gets the root path of the current script
getCurrentShellRootPath()
{
oldPath=`pwd`
SOURCE="$1"
while [ -h "$SOURCE"  ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR2="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
echo ${DIR2}
cd $oldPath
}

if [ -z "$PYTHON" ]; then
	echo "Unable to find python3 on path"
else
	echo "Using $PYTHON as Python interpreter"

	# Find location of this bash script, and set its directory as the PYTHONPATH
	#export PYTHONPATH=$(dirname "$(readlink "${BASH_SOURCE[0]}")")
	#Modify the path to PYTHONPATH
	shellRootPath=`getCurrentShellRootPath $0`
	export PYTHONPATH=${shellRootPath}
	

	# Now execute the actual program
	exec $PYTHON -O -m enjarify.main "$@"
fi

it worked! Thanks!

@biranchi2018
Copy link

biranchi2018 commented Dec 11, 2018

Try this,
import os
os.environ['PYTHONPATH'] = "/usr/bin/python3"

@FranklinYu
Copy link

FranklinYu commented Mar 6, 2019

readlink only works if you create the symbolic link, as mentioned in README. There is no need to write your own fancy script. If you installed it your own way, just remove the readlink command (making PYTHONPATH simply dirname "${BASH_SOURCE[0]}").

I have tried on macOS Mojave and the script works as expected.

chubbymaggie pushed a commit to chubbymaggie/enjarify that referenced this issue Dec 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants