Trouble creating a virtualenv for python3 on OS X - python

I'm having a strange problem with a virtualenv I've created for python 3. I went through the usual steps:
$ virtualenv --python=/opt/local/bin/python3.3 .py3
$ source .py3/bin/activate
The problem I'm having is that when I call python --version, it is still reporting 2.7.5, even though the paths all appear to be set up correctly. My virtualenv was created in /Users/barry.flinn/projects/.py3/bin, and I get thes results when I run which python:
$ which python
/Users/barry.flinn/projects/.py3/bin/python
The bin folder has the following python executables:
lrwxr-xr-x 1 barry.flinn obfuscated\Domain Users 9 Sep 23 19:39 python -> python3.3
lrwxr-xr-x 1 barry.flinn obfuscated\Domain Users 9 Sep 23 19:39 python3 -> python3.3
-rwxr-xr-x 1 barry.flinn obfuscated\Domain Users 9100 Sep 23 19:39 python3.3
Clearly, invoking python should give me python 3.3, and yet it still seems to revert to the system python, which is 2.7.5. My $PATH when the virtualenv is active is:
/Users/barry.flinn/projects/.py3/bin:/usr/local/mysql/bin:/opt/local/lib/postgresql92/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin:/usr/local/munki
Since all of this seems correct, I'm stumped as to what is going on here.
Update:
This reports correctly:
$ env python --version
Python 3.3.2
Which, to me, is slightly more baffling.

If you have a shell alias defined for the python interpreter, it will override the python chosen for your virtualenv. For example:
user#x790:~/temp$ alias python=/usr/bin/python
user#x790:~/temp$ python --version
Python 2.7.4
user#x790:~/temp$ virtualenv --python=/usr/bin/python3 foo
Running virtualenv with interpreter /usr/bin/python3
Using base prefix '/usr'
New python executable in foo/bin/python3
Also creating executable in foo/bin/python
Installing distribute.................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................done.
Installing pip................done.
user#x790:~/temp$ . foo/bin/activate
(foo)user#x790:~/temp$ python --version
Python 2.7.4
Note that the python version reported was 2.7.4, even though python3 was selected for the virtualenv. Using the env command circumvents the alias:
(foo)user#x790:~/temp$ env python --version
Python 3.3.1
Lastly, you can get around this issue by either temporarily disabling the python alias for a single command or permanently undefining it:
(foo)user#x790:~/temp$ \python --version
Python 3.3.1
(foo)user#x790:~/temp$ unalias python
(foo)user#x790:~/temp$ python --version
Python 3.3.1

Related

configure: error: Mesos requires Python < 3.0

I want to install Mesos on Oracle Linux 8.3 which does not have Internet access. I followed the process exactly in its site in part CentOS 7.1 and installed some packages before building mesos. I installed these packages using rpm -i name of package:
glib2-2.56.4-156.el8.x86_64.rpm
glib2-2.56.4-8.el8.x86_64.rpm
glib2-devel-2.56.4-156.el8.x86_64.rpm
glib2-devel-2.56.4-8.el8.x86_64.rpm
libcurl-7.61.1-14.el8.x86_64.rpm
libcurl-devel-7.61.1-14.el8.x86_64.rpm
libsecret-devel-0.18.6-1.el8.x86_64.rpm
libserf-1.3.9-9.module_el8.4.0+632+d2bf8782.x86_64.rpm
pcre-8.42-4.el8.x86_64.rpm
pcre-cpp-8.42-4.el8.x86_64.rpm
pcre-devel-8.42-4.el8.x86_64.rpm
pcre-utf16-8.42-4.el8.x86_64.rpm
pcre-utf32-8.42-4.el8.x86_64.rpm
cyrus-sasl-md5-2.1.27-5.el8.x86_64.rpm
python2-2.7.15-24.el8.x86_64.rpm
python2-2.7.18-7.module_el8.5.0+894+1c54b371.x86_64.rpm
python2-devel-2.7.15-24.el8.x86_64.rpm
python2-libs-2.7.15-24.el8.x86_64.rpm
python2-libs-2.7.18-7.module_el8.5.0+894+1c54b371.x86_64.rpm
python2-pip-9.0.3-18.module_el8.4.0+642+1dc4fb01.noarch.rpm
python2-pip-wheel-9.0.3-18.module_el8.5.0+743+cd2f5d28.noarch.rpm
python2-setuptools-wheel-39.0.1-13.module_el8.5.0+743+cd2f5d28.noarch.rpm
sqlite-devel-3.26.0-11.el8.x86_64.rpm
subversion-1.10.2-4.module_el8.3.0+703+ba2f61b7.x86_64.rpm
subversion-devel-1.10.2-4.module_el8.3.0+703+ba2f61b7.x86_64.rpm
subversion-libs-1.10.2-4.module_el8.3.0+703+ba2f61b7.x86_64.rpm
utf8proc-2.1.1-5.module_el8.3.0+703+ba2f61b7.x86_64.rpm
zlib-devel-1.2.11-16.el8_2.x86_64.rpm
Also, I installed python2.7 in this way:
tar zxvf Python-2.7.15.tgz
./configure --enable-optimizations
make altinstall
After that I set its path in ~/.bashrc like this:
alias python=/usr/local/bin/python2.7
export PATH=$PATH:/usr/local/bin/python2.7
For its pip also I run this command:
python2.7 -m ensurepip --default-pip
When I run python --version it show me Python 2.7.15.
Again extract Mesos folder and create build directory, then run ../configure; but I receive the same error before installing python 2.7.15:
configure: error: Mesos requires Python < 3.0
-------------------------------------------------------------------
The detected Python version is 3.6.
If you already have Python 2.6+ installed (and it's the default python on the path), you might want to check if you have the PYTHON environment variable set to a version of Python greater than 3.0.
However, when I run python command and receive this result which show that python 2.7.15 is default, mesos recognize python 3.6:
Python 2.7.15 (default, Sep 10 2022, 16:33:49)
Output of printenv:
LD_LIBRARY_PATH=/opt/oracle/instantclient_21_4:/opt/oracle/instantclient_21_4:/opt/oracle/instantclient_21_4:
LS_COLORS=rs=0:di=38;5;33:ln=38;5;51:mh=00:pi=40;38;5;11:so=38;5;13:do=38;5;5:bd=48;5;232;38;5;11:cd=48;5;232;38;5;3:or=48;5;232;38;5;9:mi=01;05;37;41:su=48;5;196;38;5;15:sg=48;5;11;38;5;16:ca=48;5;196;38;5;226:tw=48;5;10;38;5;16:ow=48;5;10;38;5;21:st=48;5;21;38;5;15:ex=38;5;40:*.tar=38;5;9:*.tgz=38;5;9:*.arc=38;5;9:*.arj=38;5;9:*.taz=38;5;9:*.lha=38;5;9:*.lz4=38;5;9:*.lzh=38;5;9:*.lzma=38;5;9:*.tlz=38;5;9:*.txz=38;5;9:*.tzo=38;5;9:*.t7z=38;5;9:*.zip=38;5;9:*.z=38;5;9:*.dz=38;5;9:*.gz=38;5;9:*.lrz=38;5;9:*.lz=38;5;9:*.lzo=38;5;9:*.xz=38;5;9:*.zst=38;5;9:*.tzst=38;5;9:*.bz2=38;5;9:*.bz=38;5;9:*.tbz=38;5;9:*.tbz2=38;5;9:*.tz=38;5;9:*.deb=38;5;9:*.rpm=38;5;9:*.jar=38;5;9:*.war=38;5;9:*.ear=38;5;9:*.sar=38;5;9:*.rar=38;5;9:*.alz=38;5;9:*.ace=38;5;9:*.zoo=38;5;9:*.cpio=38;5;9:*.7z=38;5;9:*.rz=38;5;9:*.cab=38;5;9:*.wim=38;5;9:*.swm=38;5;9:*.dwm=38;5;9:*.esd=38;5;9:*.jpg=38;5;13:*.jpeg=38;5;13:*.mjpg=38;5;13:*.mjpeg=38;5;13:*.gif=38;5;13:*.bmp=38;5;13:*.pbm=38;5;13:*.pgm=38;5;13:*.ppm=38;5;13:*.tga=38;5;13:*.xbm=38;5;13:*.xpm=38;5;13:*.tif=38;5;13:*.tiff=38;5;13:*.png=38;5;13:*.svg=38;5;13:*.svgz=38;5;13:*.mng=38;5;13:*.pcx=38;5;13:*.mov=38;5;13:*.mpg=38;5;13:*.mpeg=38;5;13:*.m2v=38;5;13:*.mkv=38;5;13:*.webm=38;5;13:*.ogm=38;5;13:*.mp4=38;5;13:*.m4v=38;5;13:*.mp4v=38;5;13:*.vob=38;5;13:*.qt=38;5;13:*.nuv=38;5;13:*.wmv=38;5;13:*.asf=38;5;13:*.rm=38;5;13:*.rmvb=38;5;13:*.flc=38;5;13:*.avi=38;5;13:*.fli=38;5;13:*.flv=38;5;13:*.gl=38;5;13:*.dl=38;5;13:*.xcf=38;5;13:*.xwd=38;5;13:*.yuv=38;5;13:*.cgm=38;5;13:*.emf=38;5;13:*.ogv=38;5;13:*.ogx=38;5;13:*.aac=38;5;45:*.au=38;5;45:*.flac=38;5;45:*.m4a=38;5;45:*.mid=38;5;45:*.midi=38;5;45:*.mka=38;5;45:*.mp3=38;5;45:*.mpc=38;5;45:*.ogg=38;5;45:*.ra=38;5;45:*.wav=38;5;45:*.oga=38;5;45:*.opus=38;5;45:*.spx=38;5;45:*.xspf=38;5;45:
LC_MEASUREMENT=az_IR
M2_HOME=/usr/local/maven
SSH_CONNECTION=x.x.x.x 57648 xx.xx.xx.xx 22
LC_PAPER=az_IR
LC_MONETARY=az_IR
LANG=en_US.UTF-8
HISTCONTROL=ignoredups
HOSTNAME=kr311
OLDPWD=/root/Downloads/mesos/mesos-1.11.0
JAVA_HOME=/opt/oracle/java/jdk1.8.0_25
S_COLORS=auto
LC_NAME=az_IR
XDG_SESSION_ID=2
USER=root
PYSPARK_HOME=/usr/bin/python3.8
SELINUX_ROLE_REQUESTED=
PWD=/root/Downloads/mesos/mesos-1.11.0/build
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SSH_CLIENT=x.x.x.x 57648 22
SELINUX_LEVEL_REQUESTED=
XDG_DATA_DIRS=/root/.local/share/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share
LC_ADDRESS=az_IR
SPARK_HOME=/opt/spark
LC_NUMERIC=az_IR
SSH_TTY=/dev/pts/0
MAIL=/var/spool/mail/root
TERM=xterm-256color
SHELL=/bin/bash
SELINUX_USE_CURRENT_RANGE=
PYSPARK_DRIVER_PYTHON=python3.8
SHLVL=1
LC_TELEPHONE=az_IR
LOGNAME=root
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/0/bus
XDG_RUNTIME_DIR=/run/user/0
PYSPARK_PYTHON=/usr/bin/python3.8
PATH=/opt/oracle/instantclient_21_4:/opt/oracle/java/jdk1.8.0_25/bin:/opt/oracle/instantclient_21_4:/usr/local/bin/python2.7:/opt/oracle/java/jdk1.8.0_25/bin:/opt/oracle/instantclient_21_4:/opt/oracle/java/jdk1.8.0_25/bin:/usr/local/maven/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/bin/python2.7:/opt/spark/bin:/opt/spark/sbin:/root/bin:/opt/spark/bin:/opt/spark/sbin:/usr/local/bin/python2.7:/opt/spark/bin:/opt/spark/sbin
LC_IDENTIFICATION=az_IR
HISTSIZE=1000
LESSOPEN=||/usr/bin/lesspipe.sh %s
LC_TIME=az_IR_=/usr/bin/printenv
Would you please guide me what is wrong here?
Any help is really appreciated.

Emacs gud-pdb with miniconda python environments

How does one get emacs to use the same PATH to the python and pdb executables as I get from the term when I have set a specific miniconda environment?
I.e. in emacs, when I run M-x pdb, I would like it to use the same executable as I do if I have done the following from the term:
$ source activate my_py3_env
$ pdb
I know currently this isn't what is happening. I switch to a python 3 env, which modifies my PATH appropriately, but when I run M-x pdb for a python script and print sys.version from within that script I get:
2.7.17 |Anaconda, Inc.| (default, Oct 21 2019, 19:04:46)
So it seems to be picking up the 'base' miniconda env which is still 2.7
I would half of expected it to pick up whats in /usr/bin but that doesn't seem to be the case, i.e. if I execute:
$ /usr/bin/python
I get
Python 2.7.15+ (default, Oct 7 2019, 17:39:04)
To summarise, is there a way to get emacs M-x pdb to 'follow' the conda environment I am currently in without me having to manually specify the location of the pdb executable for each environment?
You can find the conda or virtualenv python path with (swap in the analogous conda equivalent for starting the env):
source .py2james/bin/activate
and
which python in the terminal. Note the path to the python exe.
you can do: option+x pdb
then: /Users/janderson/code/python/awsomeapp/.py2james/bin/python -m pdb main.py and the emacs pdb debugger will start.

Tox not finding python3.6 even with the shim present. What is wrong with my pyenv setup?

Trying to breathe some life back into a django package that has fallen into a state of disrepair. They use tox for testing so I've setup pyenv on my MacBook. I've installed 3 versions of python as you can see below, and everything looks like it should work, but if it was I wouldn't be asking why it is not.
I've replaced my home directory with ~ to make it a bit easier to read.
pyenv was installed with brew install pyenv and the various versions of python were installed with pyenv install #.#.#
The shims exist:
$ echo $PATH
~/.pyenv/shims:~/.platformsh/bin:/usr/local/sbin:...
$ which python3.6
~/.pyenv/shims/python3.6
$ which python3.4
~/.pyenv/shims/python3.4
$ which python3.5
~/.pyenv/shims/python3.5
But executing them does not work as expected:
$ pyenv local 3.4.9 3.5.6 3.6.8
$ python3.4
Python 3.4.9 (default, Feb 12 2019, 10:33:47)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.5
pyenv: python3.5: command not found
The `python3.5' command exists in these Python versions:
3.5.6
$ python3.6
pyenv: python3.6: command not found
The `python3.6' command exists in these Python versions:
3.6.8
And tox fails like this:
py34-1.11: commands succeeded
ERROR: py36-1.11: Error creating virtualenv. Note that some special characters (e.g. ':' and unicode symbols) in paths are not supported by virtualenv. Error details: InvocationError("Failed to get version_info for python3.6: pyenv: python3.6: command not found\n\nThe `python3.6' command exists in these Python versions:\n 3.6.8\n\n", None)
ERROR: py36-2.0: Error creating virtualenv. Note that some special characters (e.g. ':' and unicode symbols) in paths are not supported by virtualenv. Error details: InvocationError("Failed to get version_info for python3.6: pyenv: python3.6: command not found\n\nThe `python3.6' command exists in these Python versions:\n 3.6.8\n\n", None)
py36-latest: commands succeeded
docs: commands succeeded
But in the .tox folder you will find these VirtualEnvs that can be activated manually.
$ ls .tox
dist docs flake8 log py34-1.11 py36-1.11 py36-2.0 py36-latest
Because at some point it was working....
I do understand the mechanics of why it isn't working, what I don't understand is why pyenv is not setting up the environment correctly (Or maybe this is exactly how it is supposed to behave). Everything I read seems to indicate that python3.6 should launch a python3.6.8 interpreter
$ bash -x python3.6
+ set -e
+ '[' -n '' ']'
+ program=python3.6
+ [[ python3.6 = \p\y\t\h\o\n* ]]
+ export PYENV_ROOT=~/.pyenv
+ PYENV_ROOT=~/.pyenv
+ exec /usr/local/Cellar/pyenv/1.2.9/libexec/pyenv exec python3.6
pyenv: python3.6: command not found
The `python3.6' command exists in these Python versions:
3.6.8
pyenv by default picks the python "locally", that is it looks for the PYTHON_VERSION environment variable or a .python-version file.
Personally I find this setup a little cumbersome (needing to have these files littered around all projects, especially in projects which need multiple versions). Fortunately, you can make these "shims" function anywhere with a default version of python by using pyenv global #.#.#
In your case, to make the python3.6 shim execute 3.6.8 without needing to set up the .python-version files, you'd run pyenv global 3.6.8 -- you can run this multiple times for different python versions as well: pyenv global 3.6.8 3.5.6 ...
The reason that you're likely not having these resolve inside tox is tox clears the environment when executing, so the PYTHON_VERSION environment variable will not carry through. You can turn that off by setting passenv= in your tox.ini. For example:
[testenv]
passenv = PYTHON_VERSION

pyenv 'system' version inconsistent with 'real' python version

In my ubuntu 16.04 system I have the following pyenv versions
$ pyenv versions
* system (set by /home/myuser/.pyenv/version)
3.6.0
3.6.0/envs/general
general
I intend that the version named system should coincide with the real system one, but when I check which python version will be executed, I get:
$ python --version
Python 3.6.0
$ which python # expecting /usr/bin/python
/home/myuser/.pyenv/shims/python
instead of the expected /usr/bin/python symlink which points to python2.7
$ ll /usr/bin/python
lrwxrwxrwx 1 root root 9 apr 18 10:11 /usr/bin/python -> python2.7*
Why is that? Is there something that I understand wrong?
How do I reset the pyenv system to make it coherent with the real system? I want to be sure of using the real system python when executing normal commands in my home dir.

Change default python version from 2.6.6 to 2.7 and also configure django

I am new to linux (Ubuntu) and python (Django). By default there is python 2.6.6 in my linux, I installed python 2.7. In the command prompt when I type python it goes to python 2.6.6, I want to change my default python to 2.7.
Ubuntu might have a Python2.7 package. If so, install that then use that to create a virtualenv and put Django inside that.
I recommend to use pythonbrew which can install and switch between multi Python interpreters in your $HOME without touch your system directories.
BTW, when install python, use the command
pythonbrew install --no-test 2.7.2
to skip unitest could speed up the installation a lot.
It's all about the links, I tell ya. Look at the links. Check the inode's link count for python2.5, python2.6, python2.7, python3.2, etc. Notice those are greater than one.
Just hunt down the duped link to "python", fix by unlink, and relink it. It may be a real link, or just a sym link. unlink with extreme prejudice.
-rwxr-xr-x 2 root admin 12392 Oct 4 2009 /opt/local/bin/python2.5*
-rwxr-xr-x 2 root wheel 12392 Jul 23 17:14 /opt/local/bin/python2.6*
lrwxr-xr-x 1 root wheel 72 Nov 7 2008 /usr/bin/python# -> ../../System/Library/Frameworks/Python.framework/Versions/2.5/bin/python
-rwxrwxr-x 1 root admin 41476 Jul 3 2010 /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7*
-rwxr-xr-x 3 root wheel 2072216 Jul 27 19:34 /usr/local/bin/python3.2*
The easiest way would be to modify your path and make sure the path to Python 2.7 is before Python 2.6. This is what I have done on my Mac to achieve similar things.
EDIT
I agree with Noufal Ibrahim in the fact about using virtualenv. I would also suggest looking into virutalenv wrapper. However, you still might have trouble using Python 2.7 is your path is not configured correctly, using an Ubuntu package for the install should take care of the path for you. Otherwise to install virtualenv from the bash shell, you can do something along the lines.
$ path/to/python27/python easy_install virutalenv
$ pip install virtualenvwrapper
I believe when installing virtualenv, it will set your PATH for you. You can confirm this by doing:
$which python
This should now show Python 2.7.

Categories