Opencv-python: OSX and sudo install requirements

Created on 2 May 2017  路  7Comments  路  Source: opencv/opencv-python

When trying to install opencv-python on OS X (python3 installed via Homebrew), installing as an unprivileged user failed with the following message:

Exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
File "/usr/local/lib/python3.6/site-packages/pip/commands/install.py", line 342, in run
    prefix=options.prefix_path,
File "/usr/local/lib/python3.6/site-packages/pip/req/req_set.py", line 784, in install
    **kwargs
File "/usr/local/lib/python3.6/site-packages/pip/req/req_install.py", line 851, in install
    self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/usr/local/lib/python3.6/site-packages/pip/req/req_install.py", line 1064, in move_wheel_files
    isolated=self.isolated,
File "/usr/local/lib/python3.6/site-packages/pip/wheel.py", line 377, in move_wheel_files
    clobber(source, dest, False, fixer=fixer, filter=filter)
File "/usr/local/lib/python3.6/site-packages/pip/wheel.py", line 323, in clobber
    shutil.copyfile(srcfile, destfile)
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 121, in copyfile
    with open(dst, 'wb') as fdst:
PermissionError: [Errno 13] Permission denied: '/usr/local/LICENSE-3RD-PARTY.txt'

After installing with sudo -H pip3 install opencv-python, the package worked.

Can the sudo requirement be eliminated by not writing the license to /usr/local/?

Most helpful comment

You can try pip install --user opencv-python or use virtualenvs. And you are correct, sudo should not be used to install packages with pip.

However, the root of this issue lies in MacOS or in how some of its package managers handle Python installations. I'm not too famialiar with all the MacOS weirdness, but the data_files parameter in setup.py uses relative paths so distutils behaves just like documented. If I move the licenses to a subfolder in this package, the issue stays the same. Even if there are no data_files, the problem would still most likely persist.

I have no access to a Mac currently, but this should be pretty easy to test with a dummy package. Moving the licenses to package_data in setup.py could also help if the issue is related only to the data_files.

All 7 comments

You can try pip install --user opencv-python or use virtualenvs. And you are correct, sudo should not be used to install packages with pip.

However, the root of this issue lies in MacOS or in how some of its package managers handle Python installations. I'm not too famialiar with all the MacOS weirdness, but the data_files parameter in setup.py uses relative paths so distutils behaves just like documented. If I move the licenses to a subfolder in this package, the issue stays the same. Even if there are no data_files, the problem would still most likely persist.

I have no access to a Mac currently, but this should be pretty easy to test with a dummy package. Moving the licenses to package_data in setup.py could also help if the issue is related only to the data_files.

In would think that the licenses should be package_data. from installing-package-data:

These files are often data that鈥檚 closely related to the package鈥檚 implementation

data_files is used for a general purpose. It can be install pretty much wherever installing-additional-files:

If directory is a relative path, it is interpreted relative to the installation prefix

The installation prefix here is /usr/local, which is probably not where the license should be saved.
I will admit I do not fully understand what the target directory is:

You can specify the data_files options as a simple sequence of files without specifying a target directory, but this is not recommended, and the install command will print a warning in this case. To install data files directly in the target directory, an empty string should be given as the directory.

But this issue can probably be solve by using package_data instead of data_files.

Yes, I agree that moving the files into package_data might help. Feel free to open pull request.

Note that the Travis builds are currently broken (I don't know yet why) but I'm going try to fix the issue asap since OpenCV 3.3.0 release is getting closer.

I am not sure where to include them. Should the files be installed in the cv2 folder?
A bit more insight: https://github.com/pypa/pip/issues/2874#issuecomment-109429489

I'm not sure either, but I would like to keep the license files in the root of this project so that they can be found easily. I would have used MANIFEST.in to collect the additional data files but it's not supported in binary wheels. Some options:

Option 1

Add

[metadata]
license_file = <license filename>

to setup.cfg. This requires that all the licenses are moved into a single file. See: https://bitbucket.org/pypa/wheel/issues/138/include-more-than-one-license-file-or

Option 2

Copy the license files during build to cv2 folder and include them there with package_data. This is probably better because the licenses can be kept separated.

Please comment if you have any better ideas. For some reason Python's packaging tools make these simple looking things unbelievably complex.

Submitted a pull request for package_data.

Was this page helpful?
0 / 5 - 0 ratings