Probably a silly question, I am coming from another language (perl/C) and I had to install python for a new project (v 3.6)
As I had this problem before in Perl to check which interpreter is doing what (not anymore with perlbrew),I checked with import sys and print (sys.version) and the system's one (2.7) is running. now I realized I did not brewed the python version I simply used the dmg from python.org and it seems that uninstalling is painful.
so long story short how do I find the interpreter path for the installed python?
tried with which--python and there is just the Library/Framework/../2.7
So have I done something wrong somewhere?
update
on macOS x yosemite v 10.10.5,
which -a python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
/usr/bin/python
which -a python 3
/Library/Frameworks/Python.framework/Version/3.6/bin/python3
/usr/local/bin/python3
shebang tried
#!/usr/bin/ env python (python3 and python 3.6)
#!usr/local/bin/python (python3)
then I run the following using cd folder than python3 script name
#!/usr/bin env python3 (or any other shebang)
import sys, platform
print (sys.version)
print platform.python_version()
output is always
2.711 (v.2.7.11:6d1b6a68f775. Dec 5 2015. 12:54:16)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)]
2.7.11
Tried both in emacs / atom (my editors) and bash
Related
When I run python3 in terminal, it states that I am running Python 2.7.10. I updated a pip package and conda package and since I have no way of running python3. I am sure python 2 is running as writing 'print "hello"' works which is should not, if python3 successfully ran.
Daves-MBP:Desktop dave$ python3
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
This should NOT work if python3 is running.
How can I get python3 running when I want to?
Do I need to set up the environment path, or something else?
Good news. I have solved the problem myself (using a different forum article)
Setting the alias to the different python version in terminal solves this error.
$ alias python='python3.7'
$ alias python2='python2'
$ alias python3='python3.7'
So when I type 'python' in terminal, python3 is called. Not sure if my issue was an alias or path issue, as I was not aware of both concepts before I had the problem.
Make sure you have the python version installed before typing in python3.7 or 3.5 for instance.
Also, python3 alone does not work.
Type python3.1 or python3.3
I'm doing a learn python the hardway tutorial, and they are using python2.7
I got it downloaded but unable to switch back from 3.3 to 2.7
I manipulated PATH variable, adding C:\Python27 but this was no use
any other suggestion?
Rename the python interpreter executables to their respective versions. The OS is just executing the first 'python' executable it finds in the path, which is probably the 3.x version. So in command line, you can type python2 or python3 to select the version of interpreter you want.
Another option is.
you can create virtual environment for python 2.7 version.
And Activate the environment.
And use your virtual env for your python 2.7 learning.
username#mypc:~/dev/learn-code$ virtualenv myenv -p /usr/bin/python
Already using interpreter /usr/bin/python
New python executable in /home/username/dev/learn-code/myenv/bin/python
Installing setuptools, pip, wheel...done.
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$
username#mypc:~/dev/learn-code$ source myenv/bin/activate
(myenv) username#mypc:~/dev/learn-code$
(myenv) username#mypc:~/dev/learn-code$ python
Python 2.7.6 (default, Oct 26 2016, 20:30:19)
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print "hello"
hello
>>>
Setting up in windows
environment also similar. see this link
I have a Linux drive with no root access. It has Python 2.4.2 installed in /usr/bin/python directory.
which python
/usr/bin/python
I installed Python 2.7.8 in my local folder:
cd /usr2/steve/bin
ls
python2.7
If I still run Python it starts older version:
python
Python 2.4.2 (#1, May 6 2011, 13:26:21)
[GCC 4.1.2 20070115 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
I have added /usr2/steve/bin to the PATH:
echo $PATH
/usr2/steve/usr2/steve/bin:/usr2/steve/local/mnt/workspace/steve/Python-2.7.8:/usr2/steve/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin
How do I force the system to use the newer Python version as opposed to the older one?
You only have a python2.7 binary in your /usr2/steve/bin folder. You can make a symlink, then just calling python should work:
cd /usr2/steve/bin
ln -s python2.7 python
make your life simple by creating an alias. Are you using BASH (hopefully you are)? If so this is the syntax
alias pythonsteve='/usr2/steve/bin/python2.7'
(drop the = sign if you are running CSH). Next, run to verify
$pythonsteve -c 'import sys; print sys.prefix'
/usr2/steve/bin
Put the alias command in your shell user start-up file. On typical BASH installs this is the file ~/.bashrc. Note, when you manually edit the ~/.bashrc file you must either log out/log in, or run source ~/.bashrc for the commands to execute.
If you want to use python 2.7 you can specify it by appending it after 'python'
which python2.7
I have a problem similar to this post: Install python module to non default version of python on Mac, so I am aware of those solutions, but they do not work for me.
I am installing M2Crypto on CentOS, which means I much use fedora_setup.sh build followed by fedora_setup.sh install in order to install on my architecture.
Unfortunately, the default Python version is 2.6, but I use 2.7. How do I execute the build and install commands so that they build and install to Python2.7 site-packages? Is there a simple command I don't know? I've been searching around here: http://docs.python.org/2/install/ in the Python Docs, but I don't see anything about .sh scripts?
You should run your scripts in a virtualenv created for your app's environment. This creates an isolated environment that uses the Python interpreter you created the virtualenv with, but with its own set of libraries.
# create the virtualenv folder: M2Crypto-venv
python2.7 virtualenv.py --distribute M2Crypto-venv
# activate the virtualenv, changing environment variables to use its Python interpreter
. M2Crypto-venv/bin/activate
# see how the current python has changed
which python # should be M2Crypto-venv/bin/python
python --version # should be 2.7
# after activating, run your install scripts
If you're using mod_wsgi or something similar to serve content, you'll want to modify your WSGI file to activate the virtualenv before doing anything else (adapted from mod_wsgi instructions):
import os.path
virtualenv_path = '/path/to/M2Crypto-venv'
activate_this = os.path.join(virtualenv_path, 'bin/activate_this.py')
execfile(activate_this, dict(__file__ = activate_this))
# rest of the WSGI file...
This was an incredibly difficult answer to come by, but the support team at Webfaction where I am hosted were spectacular in assisting me. Directly from the support I was given:
First build swig,
wget http://prdownloads.sourceforge.net/swig/swig-2.0.8.tar.gz
tar -xf swig-2.0.8.tar.gz
cd swig-2.0.8
./configure --prefix=$HOME
make
make install
Than get m2crypto,
svn checkout http://svn.osafoundation.org/m2crypto/tags/0.21/ m2crypto-0.21
cd m2crypto-0.21/
Edit fedora_setup.sh from this
SWIG_FEATURES=-cpperraswarn python setup.py $*
to this,
SWIG_FEATURES=-cpperraswarn python2.7 setup.py $*
Then build, then install,
./fedora_setup.sh build
./fedora_setup.sh install --prefix=$HOME
[me#web342 lib]$ python2.7
Python 2.7.5 (default, May 16 2013, 20:16:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import M2Crypto
>>> print M2Crypto
<module 'M2Crypto' from '/home/me/lib/python2.7/site-packages/M2Crypto-0.21-py2.7-linux-x86_64.egg/M2Crypto/__init__.pyc'>
Obviously, substitute your own details throughout. Hope this helps the next guy trying to install M2Crytpo using fedora_setup to a non-default python version.
I'm running Mac OS X Lion 10.7.1 that has both python 2.6 and 2.7 installed. I've made 2.6 my default version. I am trying to install a package and it installs to 2.7. My setup looks like this:
~:hi› which python
/usr/bin/python
~:hi› python -V
Python 2.6.6
~:hi› python
Python 2.6.6 (r266:84292, Jun 16 2011, 16:59:16)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.prefix
'/System/Library/Frameworks/Python.framework/Versions/2.6'
>>> sys.exec_prefix
'/System/Library/Frameworks/Python.framework/Versions/2.6'
Shouldn't it be installed in 2.6 site-packages? Am I misunderstanding how this ought to work?
Edit
The package in question is virtuanenvwrapper
I made 2.6 my default version like so:
defaults write com.apple.versioner.python Version 2.6
I tried installing it like this:
sudo python setup.py install
sudo /usr/bin/python setup.py install
When setup.py installs a Python package, it pays no attention to the Apple system settings. The only thing it knows is what version of Python you use to invoke it. If you say:
python2.6 setup.py …
then that version gets used, and the same with
python2.7 setup.py …
If you use the first of these two commands, does the package get installed under 2.6 like you want? My guess is that the shell that sudo runs might have 2.7 as its default, regardless of which Python your normal shell wants to use. What happens if you say:
sudo python -V