How to add Python Interpreter to PyCharm? - python

I followed a lot suggestions to add the interpreter to PyCharm, but they're simply not working. I want to figure out what the exact problem is here.
First, it said no interpreter is found, so I tried using Python 3.10 in Anaconda. But it seems "pip install packages" don't get used for Anaconda's Python. So, I used Terminal to get a different Python PATH: open -e .bash_profile and to see which python versions
I have using python -v, python3 -v, which python, which python3. So, I wanted to input /usr/local/bin/python3 or /usr/bin/python for the interpreter path.
The problem is that I can't add them at all... "Add Python Interpreter" doesn't let me click 'ok'.
Need to Add Python Interpreter
I watched lots of YouTube tutorials and went through relevant websites for suggestions, but nothing works. I even got rid of Python from all the folders manually (except the ones that didn't allow me to trash) and using Terminal as instructed (How to uninstall Python 2.7 on a Mac OS X 10.6.4?): sudo rm -rf Python, sudo rm -rf "/Applications/Python 2.7",
cd /usr/local/bin/
ls -l /usr/local/bin | grep '../Library/Frameworks/Python.framework/Versions/2.7' | awk '{print $9}' | tr -d # | xargs rm
Then, I tried reinstalling them using pip install python3. It says Python 3.9 already exists in some Frameworks location, and I am unsure where to go about from now since nothing seems to be working.
Any suggestions would be great. Thank you.
I'm new to programming and Stack Overflow (came here a couple of times for solutions but only recently signed up and posted), so if my questions should be posed another way, please let me know.

The interface shown in your image is for creating a new python environment (i.e. virtual environment). It sounds like you want to use an existing environment. So click on the Existing environment radia button in your image instead of New Environment. Then navigate to the python executable you want.
The prompts might make more sense to you if you click System Interpreter on the left hand side, but that shouldn't be necessary.

Related

Install Python 3 to /usr/bin/ on macOS

I installed python2.x and python3.x using homebrew and the executable python paths are listed below:
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
$ which python3
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
It's quite too long and not so clean to write a shebang in a python code to make it runnable on Terminal:
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python OR
#!/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
I prefer
#!/usr/bin/python OR
#!/usr/bin/python3
My issue here is, how can I correcly move or reinstall python on macOS to /usr/bin such as /usr/bin/python OR /usr/bin/python3 Instead of
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3
This is NOT possible on Mac OS X El Capitan anymore as from then on System Integrity Protection prevents that. More info in Cannot create a symlink inside of /usr/bin even as sudo
Create a symbolic link in /usr/bin/
Open terminal and do:
$ sudo ln /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python
$ sudo ln /Library/Frameworks/Python.framework/Versions/3.5/bin/python3 /usr/bin/python3
You can now do what you wanted to do.
Edit: Unfortunately as you can read from the other answers, this solution no longer works on MacOS >= El Capitan due to System Integrity Protection. (See here)
A possible alternative is to use the folder /usr/local/bin that should be accessible.
The basic question in the OP seems to not be doable because the newer Mac OSes have "System Integrity Protection" which prevents "unauthorized" changes to key directories such as /usr/bin, and sudo cannot override that.
The suggestion of using /usr/local/bin and /usr/local/Frameworks seems like it should work (I haven't tried it). However, at https://opensource.com/article/19/5/python-3-default-mac, Matthew Broberg suggests that it is likely to create problems when updating.
In that same article, Moshe Zadka recommends using pyenv to manage Python environments and using shell aliases rather than symlinks.
I was going to go with the /usr/local suggestion above, but having found this contraindication, I'm going to try Moshe's method. I'll report back here if I hit any snags.
Update: I followed the method and I still was getting pip not found and which python and python -V were not giving me the expected results. Doh! I ran:
. ~/.bash_profile
(where I had put the recommended eval "$(pyenv init -)" command) and suddenly everything was finding the paths and versions as expected.

How to make python script executable on osx?

I just want to make my script as an application. double-click and run instead of running in terminal. I have done it before with automator but now, on el capitan it doesn't work. It only gives error without explanation.
When I try with automator I get this error:
"The action “Run Shell Script” encountered an error."
Also I tried the trick below, still I am not able to do this.
#!/usr/bin/env python
chmod +x script.py
SOLVED:
After these two steps. I changed "Open with" to terminal for only this
file and changed the #!/usr/bin/env python , it works. But it doesn't work without the two steps below, you need to follow all
steps.
Add #!/usr/local/bin/python in the beginning of the code. Then run
chmod +x myscript.py in terminal. After that change the application
open with to Terminal.
It worked for me.
I have changed the mode by
sudo chmod +x file-name.py
Then Added the following line on top of the file-name.py
#!/usr/bin/env python
Then run the file by running ./file-name.py command and it works fine.
Quick step-by-step to create clickable .app to launch your python scripts.
Launch the Apple ScriptEditor (located in /Applications/Utilities/) and type in the following into the editor:
tell application "Terminal"
do script with command "python /path/to/your/script.py"
end tell
After, simply hit save and choose to save as an application.
Ps.: If anyone reading this know how to get rid of the Terminal window that opens when you launch the .app, let me know.
If you want to get more advanced, check out Platypus.
Assuming Python is installed, this should work:
https://docs.python.org/2/using/mac.html
Select PythonLauncher as the default application to open your script
(or any .py script) through the finder Info window and double-click
it. PythonLauncher has various preferences to control how your script
is launched. Option-dragging allows you to change these for one
invocation, or use its Preferences menu to change things globally.
ADDENDUM:
http://docs.python-guide.org/en/latest/starting/install/osx/
The latest version of Mac OS X, El Capitan, comes with Python 2.7 out
of the box.
You do not need to install or configure anything else to use Python.
Having said that, I would strongly recommend that you install the
tools and libraries described in the next section before you start
building Python applications for real-world use. In particular, you
should always install Setuptools, as it makes it much easier for you
to use other third-party Python libraries.
The version of Python that ships with OS X is great for learning but
it’s not good for development.
ADDENDUM 2:
Apple made some changes in El Capitan (including System Integrity Protection) that could cause installs to fail with the infamous "no software found to install". For example:
http://trac.wxwidgets.org/ticket/17203
WORKAROUND:
Use Homebrew. Which is exactly what the Installing Python on Mac OS X I cited above recommends:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
$ vi ~/.profile =>
...
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
$ brew install python
Please let me know if this doesn't work for you.
#https://stackoverflow.com/users/7885903/lucas-mendes-mota-da-fonseca
To hide the terminal window, I believe you could rename the .py to .pyw and call that.
https://stackoverflow.com/a/34739687/6713477

setting/changing PYTHONPATH in OS Mavericks

Ok, I hate to start another of seemingly hundreds of other threads about this, but how do I set or tweak the PYTHONPATH? I'm very new to Mac and the only other time I've successfully done this was on Windows using Rapid Enviroment Editor. This looks like a whole different beast. I tried installing Django and it failed to do it for 2.7, while I need 3.4, so the current path must be for 2.7. Running which python in Terminal shows the /usr/bin/python directory, which I then can't cd into, let alone find by browsing. I see that my Python 3.4 directory has the Update Shell Profile file, but it has a lot of limitations. I also see other threads mention PYTHONPATH commands in IDLE and creating one of the bash profile type files for the Terminal. How can I set this and not worry about it anymore until I need to run a different version of Python? I'm on Mac 10.9.2.
"Explain like I'm five".
If you want to install packages for python 3.4 use: pip3 install django
When installing for python 2.7 just use: pip install django
To use python 3.4 type python3 in your shell.
To see where all installations of python are use: which -a python
Depending on how you installed the new versions of python you will see output like:
/usr/local/bin/python
/usr/bin/python
If you wanted to use the python in /usr/local/bin/python you can edit your .bashrc file and add export path="/usr/local/bin:$path".
Save, then type source .bashrc in your shell and when you type which python it will show something like /usr/local/bin/python
Don't screw around too much with different versions of python, you will end up causing yourself a lot of problems.
You should not have to change your PYTHONPATH, just specify which python or pip version you want to use and that will most likely be all you need to do.
To update PYTHONPATH you can run from the terminal:
export PYTHONPATH=$PYTHONPATH:/desired/path/to/add
Then to check the updated PYTHONPATH you can run:
echo $PYTHONPATH
I'm not sure if this completely answers your question, but this is one way to make sure modules are visible to python when you import them.

Python installation mess on Mac OS X, cannot run python

I stupidly downloaded python 3.2.2 and since then writing 'python' in the terminal yields 'command not found'. Also, when starting the terminal I get this:
Last login: Wed May 2 23:17:28 on ttys001
-bash: export: `folder]:/Library/Frameworks/Python.framework/Versions/2.7/bin:/opt/local/bin:/opt/local/sbin:/usr/local/git/bin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Applications/android-sdk-mac_86/tools:/Applications/android-sdk-mac_86/platform-tools:/usr/local/git/bin:/usr/X11/bin:/usr/local/ant/bin': not a valid identifier
Why the Android SDK folder is there is beyond me. It's all jazzed up. Any ideas how can I remove the offending file, folder or fix his problem? I've checked the System Profiler and python 2.6.1 and 2.7.2.5 shows up.
If the binary was not overridden it's typically located in
/System/Library/Frameworks/Python.framework/Versions/X.X/bin/python
where X.X is your version (probably 2.7) if it's not located there it could be sometimes found in
/Library/Frameworks/Python.framework/Versions/X.X/bin/python
If you find the right binary and can run it, go ahead and start symlinking it where you need to and making the necessary patches. My first piece of advice would be changing your Current to point to the old version.
sudo ln -s /System/Library/Frameworks/Python.framework/Versions/X.X /System/Library/Frameworks/Python.framework/Versions/Current
or
sudo ln -s /Library/Frameworks/Python.framework/Versions/X.X /Library/Frameworks/Python.framework/Versions/Current
Something got messed up in your $PATH. Have a look in ~/.profile, ~/.bashrc, ~/.bash_profile, etc., and look for a line starting with export that doesn't end cleanly.
A very ugly solution to your profile problem since $PATH seems to be very wrong (as was mentioned by blahdiblah)
/bin/cp ~/.profile /tmp/bkup_profile
/bin/cat /tmp/bkup_profile | /usr/bin/grep -v "PATH" > ~/.profile
Open a new terminal and see if you can run commands normally try ls, echo, sudo for example.

How do I re-install python on the mac, but version 3.2 (latest) and have all the command line stuff work

I uninstalled python using the following commands:
sudo rm -rf /Library/Frameworks/Python.framework
sudo rm -rf /Applications/Python *
then deleted all python related things in here:
/usr/local/bin
And I just installed python 3.2, but the terminal python doesn't work, $> python doesn't do anything, other than say command not found
I uninstalled all the python stuff, because, there was a problem with the older pythons using global site-packages... so I figured it would be good to start clean (i had like... 5 versions before)
Hmm not sure I'd really go around deleting things that were installed my Mac OS.... grab your install CD and get the default Python back :)
Then install MacPorts type sudo port install python32 and you'll have a seperate install of Python 3.2 in /opt somewhere safe from the default one.
Try ActivePython. Not only does it automatically create symlinks in /usr/local/bin, but also comes with a tool called pythonselect which you can use to set the "active" Python version.
First thing you need to figure out is where your current installation is. The reason I ask is because I have several versions of python installed side-by-side though each with a different name. /usr/local/bin/python is just a sym-link to the real binary file.
For example, maybe you have /usr/local/bin/python-3.2 instead of /usr/local/bin/python. Once you know where it is, all you have to do is create a symbolic link to it via:
ln -s /usr/local/bin/python-3.2 /usr/local/bin/python
That's only an example of course and you're paths might be different. Also you'll have to run that command as root (sudo).

Categories