Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

How Do You Use MacPorts & Python To Create A Program?

I have Yosemite and MacPorts installed along with Python on MacPorts.  I need to create a program with Python but it seems all the directions on the internet discuss using Python without MacPorts.  I had to use MacPorts because I couldn't get Python to load outside of MacPorts.  I posted that question yesterday and no one has offered to help so that's why I went the MacPorts route.
Avatar of serialband
serialband
Flag of Ukraine image

python exists on the Mac already.


I don't use Macports, so I don't know where it places its copy of python.  If it's placed in another directory, like Homebrew does, you'll have to change the path to include it.
In terminal type
which python

Open in new window


This will tell you which instance of python on your mac is active

The version of python can be confirmed using
python --version

Open in new window


serialband is correct in that python is pre-installed on OSX and shouldn't need a custom installation

You only need to install manually if the version included in OSX doesn't meet your needs.

I saw your post about fixing a failed install but to be honest troubleshooting failed installs can be a real nightmare as there are 101 reasons for failure and there was nothing obvious in your post.  Maybe the pre-existing python install was the conflict .. then again maybe not.
Avatar of sharingsunshine

ASKER

You only need to install manually if the version included in OSX doesn't meet your needs.
I need a python to work so I can work of developing this script and modify it to my needs.

Open in new window


Maybe the pre-existing python install was the conflict .. then again maybe not.
Can someone just get me a working python that can work with this code:
http://www.tulane.edu/%7Ehoward/CompCultEN/webpages.html

Open in new window


I really don't care if I use MacPorts or not I just need a working python install so please advise me.

Thanks,
rjw which python
/opt/local/bin/python
rjw python --version
Python 2.7.12
That looks like it's pointing to the MacPorts Python.  Mac's default Python should be in /usr/bin/python
OK then, how do I change it?
I'm not sure precisely what your problem is .. you've Python installed .. its version 2.7 and it is working by all accounts .. you can run the "python" command in terminal - it doesn't matter that you've used the Homebrew version or the default version included with OSX

On the URL where are you having a problem?
http://www.tulane.edu/%7Ehoward/CompCultEN/webpages.html

Step 9.2.1?  Does it work now or what error are you getting?
rjw pip install BeautifulSoup4
Collecting BeautifulSoup4
  Using cached beautifulsoup4-4.5.0-py2-none-any.whl
Installing collected packages: BeautifulSoup4
Exception:
Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/commands/install.py", line 317, in run
    prefix=options.prefix_path,
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req/req_set.py", line 742, in install
    **kwargs
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req/req_install.py", line 831, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/req/req_install.py", line 1032, in move_wheel_files
    isolated=self.isolated,
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/wheel.py", line 346, in move_wheel_files
    clobber(source, lib_dir, True)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/wheel.py", line 317, in clobber
    ensure_dir(destdir)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
    os.makedirs(path)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
    mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beautifulsoup4-4.5.0.dist-info'

Open in new window

The clue is the last line ..
"OSError: [Errno 13] Permission denied:"

You can run into issues when you've multiple versions of python installed

This article covers the various bits
http://superuser.com/questions/915810/pip-not-working-on-hombrew-python-2-7-install

Basically you need to
1. Check that the homebrew base directory is on your $path variable ( this is normall done when installing Homebrew) but worth checking
In terminal enter
echo $PATH
If /usr/local/bin is not in your $PATH, put this line at the end of your ~/.profile file.
export PATH="/usr/local/bin:$Path"

2. Test that the Homebrew python install is fully complete
brew list python | grep pip
It should show you the homebrew python install

3. Run the command to check for any permissions after the install
brew postinstall python

Alternatively .. you could REMOVE the Homebrew python install from your mac and revert to using the OSX version as it should be perfectly OK
rjw echo $PATH
/Library/Frameworks/Python.framework/Versions/3.5/bin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/local/mysql/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/rjw/android-sdk-mac/tools:/usr/local/mysql/bin:/usr/local/mysql/bin
rjw brew list python | grep pip
-bash: brew: command not found

I don't have homebrew installed just MacPorts
Sorry, I crossed my wires there on custom installs .. MacPorts and Homebrew are both in the same realm but operate a bit differently from each other.  Homebrew has surpassed MacPorts on OSX in terms of its features and functionality and I've stopped with macPorts a few years ago,

In terms of MacPorts install of Python I can see /opt/local/bin is already in the $PATH so you're covered there

The permission problem is a KNOWN problem with installing python packages using when you have a custom python installation from macports.

In short the command "pip install BeautifulSoup4" will NOT work when you're using python from Macports

If BeautifulSoup is a configured package for python you can look for it
port search beautifulsoup

It should find - https://www.macports.org/ports.php?by=library&substr=py27-beautifulsoup

Have you tried installing it using the following command to install BeautifulSoup via MacPorts?
sudo ports install py27-beautifulsoup

Open in new window

I got beautifulsoup and html5lib installed using the sudo port install command but the next command to import I am not clear how to accomplish it.

https://gyazo.com/c079090c5b0c0b1c1e08a02414357122 from looking at other documentation it looks that is the IDLE app but when I click on https://gyazo.com/17b4e2b9a4c01d43e4388570291e5619 IDLE.app nothing happens and no interpreter screen pops up.
If you simply type "idle" as a command in terminal what happens?
rjw idle
Traceback (most recent call last):
  File "/opt/local/bin/idle", line 5, in <module>
    main()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/PyShell.py", line 1553, in main
    root = Tk(className="Idle")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1815, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
You're missing another part
 sudo port install py27-tkinter

Open in new window

rjw sudo port install py27-tkinter
Password:
--->  Computing dependencies for py27-tkinter
--->  Cleaning py27-tkinter
--->  Scanning binaries for linking errors
--->  No broken files found.
rjw idle
Traceback (most recent call last):
  File "/opt/local/bin/idle", line 5, in <module>
    main()
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/idlelib/PyShell.py", line 1553, in main
    root = Tk(className="Idle")
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1815, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable
ASKER CERTIFIED SOLUTION
Avatar of Eoin OSullivan
Eoin OSullivan
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
After installing X11 .. if you still get an error .. try running the following command
 launchctl load -w /Library/LaunchAgents/org.macosforge.xquartz.startx.plist

Open in new window

The cleanest way to switch the path back to the Mac default python, is to run:
sudo port uninstall python

However, you may have to start over with some of the pip installs in the default version.


"OSError: [Errno 13] Permission denied:"

The path errors are usually due to permissions.  MacPorts commands need to be run as root, so you should run it with sudo.  It's one of the reasons I moved on to Homebrew for personal users, since most Mac users don't share their system anywary.  Homebrew asks for root permissions to initially create the Homebrew install folders that are owned by an individual user.  If someone says to use brew, they're talking about Hombrew.  If someone says to use port, they're talking about MacPorts.  You can generally just swap the command name for the simple install, uninstall and status commands.

I started with Fink, then Macports, then Homebrew.  MacPorts and Fink work better if you want more centralized control on a shared computer system.  Homebrew is designed more to be accessed by a single user, although, if you understand unix permissions, you can make it work for multiple accounts too.

The cleanest way to switch the path back to the Mac default python, is to run:
sudo port uninstall python

That should remove the MacPorts versions of python and put the paths back to the original.

When you run Python scripts or environments like idle, those basic errors tell you that you're missing parts.  You can generally just cut and past your error lines into google and you'll find the answers pretty quickly to find what you're missing and you'd be able to add it faster than if you ask questions here.  

When you run Python scripts or environments like idle, those basic errors tell you that you're missing parts.  You can generally just cut and past your error lines into google and you'll find the answers pretty quickly to find what you're missing and you'd be able to add it faster than if you ask questions here and wait for an answer.

idle needs X11.  You can also run the python command without arguments and load your python files without installing X11.  idle has GUI features that help you align and view your source code with color coding easily.
I now have idle working so thanks to both of you.  Serialband einosullivan beat you on all of these answers.
That's ok, I'm not here for points.
Serialband, I appreciate you letting me know and your putting others first.  I hope to interact with you again soon,