All seems to work just fine for both python 2 and 3:
$ which Python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ python --version
Python 2.7.9
$ python2
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
$ python3
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 23 2015, 02:52:03)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
My .bash_profile settings are as follows:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
PATH="/Library/Frameworks/Python.framework/Versions/3.4/bin:${PATH}"
export PATH
The problem:
$ virtualenv My_Env
-bash: /usr/local/bin/virtualenv: /usr/local/opt/python3/bin/python3.4: bad interpreter: No such file or directory
The path above does not exist. I downloaded my python packages directly from the python site and installed using the default settings.
Prior to this, I used Python installed with HomeBrew, but this path does not appear to be a brew file path anyway.
Bottom line... My file path is messed up somewhere other than in my .bash_profile and I have no idea where to look to fix it.
Also note that I set up my other computer with the same exact .bash_profile and python installations and it works perfectly...
Both are running Mac Yosemite.
Thanks in advance!
Ok, I was able to fix the problem with a fresh install of virtualenv. I assume that is where the path issue was located (somewhere in the installation of virtualenv).
My problem was as simple as having spaces in the directory at the top of venv/bin/pip which meant that the path was inside quotes. As soon as I made sure that it wasn't under a directory with spaces and removed the quotes it worked.
eg. #!"/Users/Tomm/MY PROJECT/venv/bin/python" should be changed to #!/Users/Tomm/MY-PROJECT/venv/bin/python
You can check the "activate" file in your virtualenv bin folder, and see what is written there at around line 42, and then can actually manually set it, while keeping in mind that all the executables in this bin folder are hard coded for the first shebang line.
You will need to change them as well. Or just move the folder to the right place for you current temporary task, otherwise you might need a virtualenv relocation tool or just re-install it.
Re-installing virtualenv should solve this. I also went through same bug and reinstalling using pip solved this for me.
Related
Yesterday, I did put my laptop on upgrade 19.10 to 20.04 but due to power failure, that became a partial-upgrade, the system broked. I resolved everything but my Django app wasn't running due to PYTHONPPATH so I tried uninstalling python3 and everything got broken. I re-installed that again.
Now when I do python --version I got
bash: python: command not found
whereas python3 --version gives correct answer.
Python 3.8.2
I have python2.7 and python3 both installed. So for now, my Python is not working and also I think I've messed up my PYTHONPATH and I really don't know what I'm going to do now.
My ./~bashrc file looks like below :
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME=$HOME/gems
export PATH=$HOME/gems/bin:$PATH
# Install Ruby Gems to ~/gems
export GEM_HOME="$HOME/gems"
export PATH="$HOME/gems/bin:$PATH"
I'm using Ubuntu 20.04.
Please specify how are you running your project and what exactly is the issue you are facing. May be you can paste the error message you get.
For python command,
In Linux, generally the base commands (like python) without version in it, would actually be pointing the specific (python) version executable through symbolic links (or simply links).
[foo#linuxbox ~]$ ls -l /usr/bin/python
lrwxrwxrwx. 1 root root 16 Feb 9 16:26 /usr/bin/python -> /usr/bin/python3
These links can be created or even edited to our need to point to the version we need. Use the below command to link python to python3. This is equivalent to setting alias for python3 as python but bit more than that as all users/process can run python but in case of alias the tool/user must be running from bash or corresponding shell where alias was created.
sudo ln -f -s /usr/bin/python3 /usr/bin/python
I feel in Ubuntu 20 you have to run command python2 to go into 2.7.* interpreter. python and python3 command both refers to Python3. But anyway your python command should work.
#ideapad:~$ python
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python2
Python 2.7.17 (default, Apr 15 2020, 17:20:14)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
ideapad:~$ python3
Python 3.6.9 (default, Apr 18 2020, 01:56:04)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
To solve your issue, use an alias. Place command alias python=python3 into ~/.bashrc file, after adding this run source ~/.bashrc.
Other solutions:
run command which python it will reveal the location of installed Python and then try adding the location given by which python command to PYTHONPATH
Reinstall your python - sudo apt install python
I am trying to make a Python script on my Mac (MacOS 10.14.6 Mojave) and I am getting trouble with installing a module (watchdog). I have the built-in Python 2 and I installed Python 3 with Homebrew.
If a type in the terminal python, I get:
Python 2.7.16 (default, Oct 16 2019, 00:34:56)
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
So Python 2 correctly uses GCC (the one provided by Apple? I am not sure) instead of Clang. Now, if I do python -m pip install watchdog it just works. But I want to install the module on Python 3, that for some reason uses Clang instead of GCC. In fact, if I type `python3', I get:
Python 3.8.0 (v3.8.0:fa919fdf25, Oct 14 2019, 10:23:27)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
I assume that as a consequence python3 -m pip install watchdog gives a compile error because Python 3 is not using GCC. The error message is very long and can be found here: https://pastebin.com/DEAKANQ9
In my $PATH I have /usr/local/bin (where gcc is installed) before /usr/bin, i.e.
echo $PATH
/Library/Frameworks/Python.framework/Versions/3.8/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/opt/X11/bin
I would say everything is set up correctly, but apparently that is not the case. How can I make Python 3 use GCC instead of Clang?
I have fixed the issue.
First, I have removed Python 3.8 which was installed using the pkg installer (by accident actually). Then, I have created an alias for Python 3 in the .bash_profile. Another mistake was that I created the file ~/.bashrc (as in Ubuntu) instead of editing the file ~/.bash_profile to modify the $PATH.
# ~/.bash_profile
#
# Python alias
alias python=/usr/local/bin/python3
# Setting PATH for Python 3.7.5 (Homebrew)
PATH="/usr/local/bin:${PATH}"
export PATH
# Set module path
PYTHONPATH=$PYTHONPATH:/usr/local/lib/python3.7/site-packages
# Set PATH priority to Homebrew installation folder
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
With this settings, I was able to install the module and make it work in my script!
OSX. When I installed the Canopy IDE, Enthought Canopy Python 2.7.6 was set as the default Python for the bash shell:
$ python
Enthought Canopy Python 2.7.6 | 32-bit | (default, Sep 15 2014, 17:36:57)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Currently, I am using another IDE (PyCharm) for a project where a Python module is needed. I tried to download the module directly from the bash shell using pip but it did not work:
pip install psycopg2 <---module
I believe it has something to do with Enthought Canopy Python running, so I attempted to restore the original python. However, I could not restore it. The instructions from this Enthought post (2011) did not work:
OS X - After EPD installation, restore system python to be default
They say to comment out the following lines from the ~/.bash_profile file, save the changes and restart the bash shell:
# PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:${PATH}"
# export PATH
Didn't work.
There's something wrong with my OSX system and python that no amount of googling has fixed. I've uninstalled all traces of python except the system python package with OSX that I'm not supposed to uninstall, and then started afresh with a new python from python.org, and installed pip.
Now...not sure if this particular behavior below is part of the issue, but it seems strange to me:
I ran python twice. Once with sudo and once without. Without sudo, I can't access pip. What's going on?
$ sudo /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named pip
However...
$ /Library/Frameworks/Python.framework/Versions/2.7/bin/python
Python 2.7.9 (v2.7.9:648dcafa7e5f, Dec 10 2014, 10:10:46)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pip
>>>
I've already referred to: sudo python runs old python version
I have nothing in my .bash_profile, or anything in any other profiles.
All I've done is the following:
export PYTHONPATH=/lib/python2.7/site-packages/
ls $PYTHONPATH returns:
_markerlib pip pkg_resources.pyc setuptools-8.0.1.dist-info virtualenv.pyc
easy_install.py pip-1.5.6.dist-info setuptools virtualenv-1.11.6.dist-info virtualenv_support
easy_install.pyc pkg_resources.py setuptools-7.0.dist-info virtualenv.py
which pip returns:
/bin/pip
sudo overrides your export. It's the same Python (as you can easily tell from the version information it prints) but it runs with a different (system default) PYTHONPATH.
This is one of the jobs of sudo; it sanitizes the environment to safe defaults. You may be able to tweak this, but the real question is, what are you trying to accomplish? If you need to run as root with a particular environment, set up a virtualenv and/or write a wrapper script which sets things up before dispatching Python.
What do you get when you compare the output of which pip and sudo which pip?
On my system I get different outputs. If you do, I'm not sure how to fix that, but you could try to force the sudo'd python to look in the correct directory:
import sys
sys.path.insert(0, '/lib/python2.7/site-packages/')
import pip
I've installed python27 on my MacBook Pro (OS X 10.6.8) using Macports. Here is the output for sudo port select --list python:
Available versions for python:
none
python25-apple
python26-apple
python27 (active)
But when I type the python command, it's running python26:
rod:~ $python
Python 2.6.6 (r266:84292, Feb 11 2012, 09:09:16)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
I've tried uninstalling my Macports pythons (I used to have the python31 and python25 macports as well) and reinstalling, but to no avail.
I should note that I recently had to do a time machine restore due to a hard drive failure, and I've been finding that some of the soft links seem not to have restored properly. I wonder if this could be a source of my problems here. Any tips?
Does your .profile still have the /opt/local paths prepended to $PATH? If its not present then despite being selected you wont get it by default because it symlinks the version to /opt/local/bin/python