Something strange and unexpected is happening with the sys.path of any virtual environment I set. For example, a clean env:
$ virtualenv test
$ source test/bin/activate
(test) $
This is the expected PYTHONPATH:
(test) $ python
>>> import sys
>>> print '\n'.join(sys.path)
/home/user/test/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/user/test/local/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/home/user/test/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/home/user/test/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/home/user/test/lib/python2.7
/home/user/test/lib/python2.7/plat-linux2
/home/user/test/lib/python2.7/lib-tk
/home/user/test/lib/python2.7/lib-old
/home/user/test/lib/python2.7/lib-dynload
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/home/user/test/local/lib/python2.7/site-packages
/home/user/test/lib/python2.7/site-packages
But this is the one I really get:
(test) $ bpython
>>> import sys
>>> print '\n'.join(sys.path)
/usr/bin
/usr/lib/python2.7
/usr/lib/python2.7/plat-linux2
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages
I can't figure out the reason of the two different sys.paths.
Because of that, no pip installation works!
I'm using Virtualenv 1.7.2, Ubuntu 12.04, Python 2.7.3.
Any help will be appreciated.
Rather than installing one copy of bpython per virtualenv, I've added this function to my shell profile (for example ~/.bashrc or ~/.zshrc). It wraps the bpython command with some logic to load the virtual environment's python path (if you have an active virtual environment).
bpython() {
if test -n "$VIRTUAL_ENV"
then
PYTHONPATH="$(python -c 'import sys; print ":".join(sys.path)')" \
command bpython "$#"
else
command bpython "$#"
fi
}
I found that I needed to deactivate and reactivate my virtualenv after installing bpython for it to work.
pip install bpython
deactivate
. bin/activate # or your equivalent activation command
My hypothesis is that you have not installed bpython after you have activated the new virtualenv.
I followed it up exactly like you mentioned:
mkvirtualenv bpython
(bpython)~ $ pip install bpython
(bpython)~ $bpython
and then ran the commands:
>>> import sys
>>> print '\n'.join(sys.path)
/Users/xxxx/.virtualenvs/bpython/bin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python27.zip
/Users/xxxx/.virtualenvs/bpython/lib/python2.7
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-darwin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-tk
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-old
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages
and did the same thing again by running python under the activated virtualenv
(bpython)~ $ python
.....
>>> import sys
>>> print '\n'.join(sys.path)
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages/pip-1.1-py2.7.egg
/Users/xxxx/.virtualenvs/bpython/lib/python27.zip
/Users/xxxx/.virtualenvs/bpython/lib/python2.7
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-darwin
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-tk
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-old
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/lib-dynload
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac
/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages
/Users/xxxx/.virtualenvs/bpython/lib/python2.7/site-packages
I saw no difference in the two results
I also discovered that if you have bpython installed locally, you need to create your virtualenv with --no-site-packages for it to work properly. If you created your virtualenv without that flag, you can create an empty file named no-global-site-packages.txt in ~/.virtualenvs/<env-name>/lib/python2.7/ as noted in this Stack Exchange answer.
Related
I am working on a Macbook Pro running MacOS Monterrey, and I'm following instructions to work in a virtual environment from this site. https://duncanleung.com/set-up-python-pyenv-virtualenv-poetry/
While I am able to create a virtualenv, I am unable to activate it.
On running pyenv activate my-env, I get this error message:
Failed to activate virtualenv.
Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.
My zshrc file has this content:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv virtualenv-init -)"
if command -v pyenv 1>/dev/null 2>&1; then
eval "$(pyenv init -)"
fi
PATH=$(pyenv root)/shims:$PATH
Result of which python:
/Users/abc/.pyenv/shims/python
Result of python -V: Python 3.9.9
What could be possible reasons for the virtualenv not activating?
I have a sed command to change something in a file of python:
sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/local/lib/python2.7/site.py
so I want to replace "ascii" by "utf-8" but this does not work.
Someone who can explain why it isn't working and what do I need to change?
>> import sys
>> reload(sys)
>> sys.getdefaultencoding();
--> "ascii"
Run my command
>> import sys
>> reload(sys)
>> sys.getdefaultencoding();
--> "ascii"
Are you doing it after installing Python, aren't you? Could you post your dockerfile to try to reproduce your problem? Did you try to execute an bash shell to test if that file was changed as expected?
This is my working Dockerfile example:
FROM ubuntu:14.04
MAINTAINER github/ojgarciab
# Use the "noninteractive" debconf frontend
ENV DEBIAN_FRONTEND noninteractive
# Add python
RUN apt-get update && apt-get install -y python && \
apt-get clean
RUN sed -i 's|encoding = "ascii"|encoding = "utf-8"|g' /usr/lib/python2.7/site.py
ADD ejemplo.py /ejemplo.py
CMD [ "python", "/ejemplo.py" ]
Before using sed command the output was:
redstar#nvidiastar:~$ docker run -t python
ascii
And now the output is:
redstar#nvidiastar:~$ docker run -t python
utf-8
The content of ejemplo.py script:
import sys
reload(sys)
print sys.getdefaultencoding();
Note that the path used was /usr/lib/python2.7/site.py and not /usr/local/lib/python2.7/site.py.
I'm running into an issue installing a package that's reliant on ocamlfind but I'm getting an ocamlfind: command not found error when making.
I have installed ocamlfind with the ocaml package manager and have tried reinstalling using "opam reinstall ocamlfind".
I have also tried the 'eval opam config env' command to see if updates my bin.
Has anyone run into a similar issue/know what this might be caused by
The output when running the make:
make
ocamlfind ocamlc -pp "camlp4o -I lib/dcg -I lib/ipp pa_dcg.cmo pa_ipp.cmo" -w usy -thread -I lib -I lib/dcg -I lib/ipp -c semantics.ml
/bin/sh: ocamlfind: command not found
The output when trying ocamlfind
ocamlfind
-bash: ocamlfind: command not found
ocaml is installed
opam install ocamlfind
[NOTE] Package ocamlfind is already installed (current version is 1.5.5).
and when running the eval command
eval 'opam config env'
CAML_LD_LIBRARY_PATH="/home/centos/.opam/system/lib/stublibs:/usr/lib64/ocaml/stub libs"; export CAML_LD_LIBRARY_PATH;
MANPATH="/home/centos/.opam/system/man:"; export MANPATH;
PERL5LIB="/home/centos/.opam/system/lib/perl5"; export PERL5LIB;
OCAML_TOPLEVEL_PATH="/home/centos/.opam/system/lib/toplevel"; export OCAML_TOPLEVEL_PATH;
PATH="/home/centos/.opam/system/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/centos/.local/bin:/home/centos/bin"; export PATH;
I'm on a server running centos 7
This command
eval 'opam config env'
is almost assuredly a typo and was supposed to be
eval `opam config env`
though using $(...) instead is the modern equivalent and avoids this font-fact confusion
eval $(opam config env)
That being said that just sets the environment variables in the current shell session (and exports them for use by processes run by this shell session).
As such that needs to be run in every shell session that needs those set (including each line of the makefile that expects them to be set if the environment that runs make doesn't already have them set and exported).
try
sudo apt-get install ocaml-findlib
I found that the most convenient way of installing virtualenv + virtualenvwrapper is by using virtualenvburrito.
Now I can manage to automate my pip installs in a vagrant provision by the following:
Line in Vagrantfile:
config.vm.provision :shell, :path => "bootstrap.sh"
Lines in bootstrap.sh:
curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | $SHELL
source /root/.venvburrito/startup.sh
cd /vagrant
mkvirtualenv my_project
pip install -r requirements.txt
Then I run vagrant ssh but then I have to run the following to access my virtual environment:
sudo -i
source /root/.venvburrito/startup.sh
workon my_project
I don't want to always have to run sudo -i and source /root/.venvburrito/startup.sh, I just want to be able to run workon my_project directly.
But
(I.) I can't seem to append source /root/.venvburrito/startup.sh to my ~/.profile and
(II.) even if it was appended to that file I'd get a permissionerror. I can't seem to change the permissions for any protected file either.
The best way to deal with (I.) and (II.) is to set the privileged attribute in the Vagrantfile to false.
See here
I have being using fabric to deploy an app with virtualenv. I was using fabric 1.4 and upgraded to 1.5.1 last week. My script stopped working.
It can't install the requirements. It seems it's not activating the virtualenv. In my code, I have:
with cd('%(path)s' % env):
with prefix('source bin/activate'):
run('pip install -U distribute')
I'm getting a permission denied error: error: could not delete '/usr/local/lib/python2.7/dist-packages/pkg_resources.py': Permission denied
The command being executed is:
Executed: /bin/bash -l -c "cd /var/www/myproject && source bin/activate && export PATH=\"\\$PATH:\\"/var/www/myproject\\" \" && pip install -U distribute"
If I ssh to the remote machine and run cd /var/www/myproject && source bin/activate && pip install -U distribute, it works just fine.
Why is my fabric script not working?
Thanks in advance
Instead of the serial approach with..
source bin/activate
pip install -U distribute
..directly use the pip executable of the virtualenv:
myenv/bin/pip install -U distribute
Although not exactly a solution, fabtools has a number of functions related to virtualenvs that are very handy. They do (almost) all of the hard work for you, and are probably worth using to check it isn't something else going wrong.
# Cut (and modified) from the fabtools documentation
from fabric.api import *
from fabtools import require
import fabtools
#task
def setup():
# Require a Python package
with fabtools.python.virtualenv('/home/myuser/env'):
require.python.package('pyramid')