Is it somehow possible to programatically get the bin/Scripts directory path related to the current runtime environment? It's typically /usr/lib/pythonX.Y/bin (or C:\PythonXY\Scripts), but it can be different in e.g. virtual environments.
I'm aware of the constants in distutils.command.install, but I hope there is an easier way to get the path.
the virtual environment path is stored in the $VIRTUAL_ENV environment variable.
$VIRTUAL_ENV/bin/Scripts
will give you what you want if you have a virtual environment loaded. you can check to see if that virtual environment is set, and if not, fall back on the system runtime directory:
if [ -z "$VARIABLE" ]
then
dir=$VIRTUAL_ENV/bin/Scripts
else
dir=/usr/lib/pythonX.Y/bin/Scripts
fi
another thing you can try is to extrapolate the runtime directory from the output of the command 'which python', which would give you something like this:
$(which python | cut -d "'" -f 3 | sed 's/python$//')"Scripts"
Related
I want to simplify my powershell prompt when using Python virtual environment: I want to see the name of the current environment, and the current directory only (not the full path).
For example, I want to convert this
(venv) PS C:User\me\Desktop\project\subfolder>
to this:
(venv) PS subfolder>
I tried many Powershell profile.ps configuration, such as
function prompt {
$p = Split-Path -leaf -path (Get-Location)
"$p> "
}
But this command hides the current virtual environment.
Does someone already achieved to do what I want ?
I recently switched mac computers. My old computer had conda and all the relevant libraries and environments installed. I transferred all the files from the old computer to my new one. However, my new computer has a different username (home directory name) than the old one, so whenever I run conda commands, I get errors like the following:
__conda_exe:2: no such file or directory: /Users/[old_username]/anaconda/bin/conda
I can circumvent this initial error by manually setting $PATH each time, but then I still get errors like this:
__add_sys_prefix_to_path:6: command not found: dirname
__add_sys_prefix_to_path:7: command not found: dirname
__conda_exe:2: no such file or directory: /Users/[old_username]/anaconda/bin/conda
Is there a way to change this so the commands point to my new home directory?
I believe you need to use CPR (conda-prefix-replacement) for this.
However, I'd instead recommend not manually transferring the files, since this usually fails to retain hardlinks. Instead, dump each of your environments to YAML
conda env export -n envname > env.yaml
and recreate them on the new system (after a minimal base install, like Miniconda)
conda env create -n envname -f env.yaml
I would not recommend transferring the old base, but if you must, put it in a new location:
# old system
conda env export -n base > old_base.yaml
# new system
conda env create -n old_base -f old_base.yaml
I have few conda environments that I use in different projects, say:
ml37 (for machine learning)
etl37 (for data pipelines)
I have local projects organized in their own directories:
apps/some_app
apps/other_app
...
Each time I cd to a specific project, I already know which env I would like to use. So I end up doing conda activate [some env] each time I change directories. I feel like there must be a better way.
What would be a clean way to automatize this?
Or is my use of conda environments wrong?
I made a script similar to the one of Corey Chafer, but this one extends the cd command.
cd() { builtin cd "$#" &&
if [ -f $PWD/.conda_config ]; then
export CONDACONFIGDIR=$PWD
conda activate $(cat .conda_config)
elif [ "$CONDACONFIGDIR" ]; then
if [[ $PWD != *"$CONDACONFIGDIR"* ]]; then
export CONDACONFIGDIR=""
conda deactivate
fi
fi }
Put this few lines of code at the bottom of your shell profile and then create a .conda_config file inside the directory you want to activate the env for.
The .conda_config file must contain only the env name.
In this way, every time you cd into a directory that has a .conda_config file, the script will activate the env, and every time you cd out it will deactivate.
I created a repo for reference Conda-autoactivate-env
EDIT:
There was a bug in the elif condition.
Basically [-n $CONDACONFIGDIR] returns always True and its logic is backwards actually.
The fix is:
quote the variable (or use double squared brackets) and remove -n
[ "$CONDACONFIGDIR" ] OR [[ $CONDACONFIGDIR ]].
The above code is already up-to-date!
I have just moved to Linux (Ubuntu 15.04) and I am trying to add my python directory (:~/Documents/Python/Programs) to the path variable, but I am struggling..
I have tried export PATH = $PATH:~/Documents/Python/Programs, and then turning off and on again, but nothing happens
I have also looked at my ~/.profile, but don't under stand it, it comes up with (I have removed a tonnes of comments from the top):
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
UPDATE:
What I am trying to do is add my python directory to PATH so that I will be able to import self made modules from within this directory
I was under the impression I had to add this to PATH by adding
PATH="$HOME/Documents/Python/Programs/:$PATH"
To the bottom of my ~/.profile document, was this wrong, and what should I actually be doing to solve this?
You have to set your PATH e.g. inside ~/.profile or ~/.bashrc depending on where you want to use Python. Add something like this to the end of either of them:
PATH="$HOME/Documents/Python/Programs/:$PATH"
As stated by the comments, this change will only be take into account, after either restarting a login shell or new start of X session (e.g. newboot). If you need the changes directly, either source the file or export it manually... so
either
source ~/.profile
source ~/.bashrc
(you could also use the . operator, but this is only working in Bash)
or export the variable
export PATH="$HOME/Documents/Python/Programs/:$PATH"
It's important to add your custom path before original PATH as the shell will call the 1st file found inside $PATH.
I'm using virtualenv to switch my python dev env. But when I run workon my_env, I meet such error message:
Error: deactivate must be sourced. Run 'source deactivate'
instead of 'deactivate'.
Usage: source deactivate
removes the 'bin' directory of the environment activated with 'source
activate' from PATH.
After some searches on google, it seems that workon, which is defined in /usr/local/bin/virtualenvwrapper.sh, calls deactivate. And there is a script with the same name is present in Anaconda's bin, so it gets called by workon by mistake.
Any suggestion for working around this conflict?
One solution which works for me is to rename deactivate in Anaconda's bin:
mv deactivate conda-deactivate
I agree with the comment by #FredrikHedman that renaming scripts in the anaconda/miniconda bin directory has the potential to be fragile. His full post led me to what I feel is a more robust answer. (Thanks!)
Rather than simply throwing away any errors thrown by calling deactivate, we could simply condition that call on whether the function would be called versus the file. As mentioned, virtualenv and virtualenvwrapper create a function named deactivate; the *condas call a script file of the same name.
So, in the virtualenvwrapper.sh script, we can change the following two lines which test for whether deactivate is merely callable:
type deactivate >/dev/null 2>&1
if [ $? -eq 0 ]
with the stricter test for whether it's a shell function:
if [ -n $ZSH_VERSION ] ; then
nametype="$(type -w deactivate)"
else
nametype="$(type -t deactivate)"
fi
if [ "${nametype##* }" == "function" ]
This change avoids triggering the spurious error noted in the original question, but doesn't risk redirecting other useful errors or output into silent oblivion.
Note the variable substitution on nametype in the comparison. This is because the output of type -w under zsh returns something like "name: type" as opposed to type -t under bash which returns simply "type". The substitution deletes everything up to the last space character, if any spaces exist, leaving only the type value. This harmlessly does nothing in bash.
(Thanks to #toprak for the zsh test and for the correct flag, type -w, under zsh. I look forward to more cross-shell coding tips!)
As ever, I appreciate constructive feedback and comments!
You can edit /usr/local/bin/virtualenvwrapper.sh to make deactivate point to an absolute path to whatever deactivate it is supposed to be referencing.
As I do not have enough reputations to add a comment:
Thomas Capote's suggestion is fine (thx 4 that), except "zsh" does not have a "-t" option for the build-in command "type". Therefore its necessary to add another conditional statement to get desired result for "nametype":
# Anaconda workaround for "source deactivate" message:
# Start of workaround:
#type deactivate >/dev/null 2>&1
#if [ $? -eq 0 ]
if [ -n $ZSH_VERSION ] ; then
nametype="$(type -w deactivate)"
else
nametype="$(type -t deactivate)"
fi
if [ "${nametype##* }" == "function" ]
# End of workaround
Hope it helps other zsh users.
In anaconda activate is an executable script located in the anaconda bin directory, but is a function in in virtualenvwrapper.sh. So this is sort of a namespace collision problem, but also a case of overlap in functionality.
Anacondas is a python distribution and – among many other things – has support for dealing with virtual environment via conda env while the virtualenvwrapper is focused on working with different virtual environments. Just renaming the anaconda/bin/activate script is a brittle solution and may break conda.
The code of virtualenvwrapper.sh (function workon) executes deactivate which happens to use the anaconda script. This script returns an error. The workon code then goes on and removes the deactivate name and sources its own deactivate and also creates a deactivate function on the fly.
In summary, it does the right thing and the "error" can be viewed more as a warning. If you want to make it go away you can modify the workon function (search for the line # Deactivate any current environment "destructively")
deactivate
-->
deactivate >/dev/null 2>&1
(I have suggested this change to the virtualenvwrapper maintainer)