bash 使 OSX 中的 matplotlib 在虚拟环境中工作的最简单方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38090455/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the simplest way to make matplotlib in OSX work in a virtual environment?
提问by Charlie Parker
I just discovered that matplotlib
has issue with virtual environments. I tried the solutions in the FAQsbut they didn't work. An ideal solution should only involve pip
commands, but it might be hard or unrealistic to request that. Anyway, I tried what they had on the OS X section to make a framework
bash file in your venv
folder and run command through it.
我刚刚发现matplotlib
虚拟环境有问题。我尝试了常见问题解答中的解决方案,但没有奏效。理想的解决方案应该只涉及pip
命令,但要求这样做可能很困难或不切实际。无论如何,我尝试了他们在 OS X 部分的内容,framework
在您的venv
文件夹中创建一个bash 文件并通过它运行命令。
$ frameworkpython krls.py
when I did that I got a permission deniel error:
当我这样做时,我收到了许可拒绝错误:
Permission denied
I am sort of scared of sudoing commands that I am not 100% what they do...anyway, it seemed the bash script isn't doing anything too dangerous so I went ahead and sudo-ed it. However, the response of my terminal was weird, it said:
我有点害怕 sudo 命令,因为我不是 100% 他们所做的......无论如何,bash 脚本似乎没有做任何太危险的事情,所以我继续并对其进行了 sudo 编辑。然而,我的终端的反应很奇怪,它说:
sudo frameworkpython krsl.py
sudo: frameworkpython: command not found
which means it doesn't recognize frameworkpython
as a command. Without the sudo it says:
这意味着它不能识别frameworkpython
为命令。没有 sudo 它说:
frameworkpython krsl.py -bash: /Users/my_name/path/venv/bin/frameworkpython: Permission denied
frameworkpython krsl.py -bash: /Users/my_name/path/venv/bin/frameworkpython: 权限被拒绝
which seems it recognizes frameworkpython as a command (?) but it didn't execute it due to permissions? It seems strange to me. Anyone any ideas?
它似乎将 frameworkpython 识别为命令(?),但由于权限而没有执行它?这对我来说似乎很奇怪。有人有什么想法吗?
I also tried:
我也试过:
$ pip install TKAgg
Collecting TKAgg
Could not find a version that satisfies the requirement TKAgg (from versions: )
No matching distribution found for TKAgg
but it didn't work.
但它没有用。
So I tried the next option which is using PySide which also didn't work and gave a giant error output:
所以我尝试了下一个使用 PySide 的选项,它也不起作用并给出了一个巨大的错误输出:
$ pip install pyside
Collecting pyside
Using cached PySide-1.2.4.tar.gz
Building wheels for collected packages: pyside
Running setup.py bdist_wheel for pyside ... error
Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/tmpUR9qFCpip-wheel- --python-tag cp27:
Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
running bdist_wheel
running build
Python architecture is 64bit
error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.
----------------------------------------
Failed building wheel for pyside
Running setup.py clean for pyside
Failed to build pyside
Installing collected packages: pyside
Running setup.py install for pyside ... error
Complete output from command /Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside:
Removing /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/pyside_package
running install
running build
Python architecture is 64bit
error: Failed to find cmake. Please specify the path to cmake with --cmake parameter.
----------------------------------------
Command "/Users/my_name/path/venv/bin/python2.7 -u -c "import setuptools, tokenize;__file__='/private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-UVA_F4-record/install-record.txt --single-version-externally-managed --compile --install-headers /Users/my_name/path/venv/bin/../include/site/python2.7/pyside" failed with error code 1 in /private/var/folders/nr/rxlk6w192hx8r74813yg6r500000gn/T/pip-build-_yzDki/pyside/
After that then I tried the next option using WX Phonix. Unfortunately, I went through their site and couldn't find how to do it.
之后,我使用WX Phonix尝试了下一个选项。不幸的是,我浏览了他们的网站,但找不到如何操作。
采纳答案by Pedro Cattori
Update:
更新:
With Python3, you can use the built-in implementation of virtualenv
via -m venv
:
使用 Python3,您可以使用virtualenv
via的内置实现-m venv
:
python -m venv <name of virtualenv>
source <name of virtualenv>/bin/activate
Python3's builtin implementation builds the virtualenv such that Python is set up as a framework, so no need to configure anything to get it to work. More details here.
Python3 的内置实现构建了 virtualenv,这样 Python 就被设置为一个框架,所以不需要配置任何东西来让它工作。更多细节在这里。
Original Answer:
原答案:
As stated in the FAQS you reference, you will need to create a frameworkpython
scripts in venv/bin
. It looks like your system is unable to find frameworkpython
so it is either not on your systems $PATH
or it is not executable.
正如您参考的常见问题解答中所述,您需要frameworkpython
在venv/bin
. 看起来您的系统无法找到frameworkpython
它,所以它要么不在您的系统上,$PATH
要么无法执行。
Make sure you make this scripts executable via chmod +x venv/bin/frameworkpython
!
确保您通过chmod +x venv/bin/frameworkpython
!
From Linux/Unix docs(I added the bold):
来自Linux/Unix 文档(我添加了粗体):
PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executablefiles.
PATH 是 Linux 和其他类 Unix 操作系统中的环境变量,它告诉 shell 搜索可执行文件的目录。
Your system will onlylook for executablefiles accessible via the PATH
environment variable, skipping over non-executable files.
您的系统将只查找可通过环境变量访问的可执行文件PATH
,跳过不可执行的文件。
Creating the frameworkpython
script is necessary because matplotlib
requires a framework build of python. From the link above:
创建frameworkpython
脚本是必要的,因为matplotlib
需要 Python 的框架构建。从上面的链接:
Unfortunately virtualenv creates a non framework build even if created from a framework build of Python.
不幸的是,即使从 Python 的框架构建创建,virtualenv 也会创建非框架构建。
Here's another postwith more details on framework builds
这是另一篇文章,其中包含有关框架构建的更多详细信息
回答by sufi
If you are using Python 2.x then, use these commands in virtual environment:
如果您使用的是 Python 2.x,请在虚拟环境中使用这些命令:
import matplotlib
matplotlib.use(‘TkAgg')
import matplotlib.pyplot as plt
This makes the matplotlib work in the virtual environment too.
这使得 matplotlib 也可以在虚拟环境中工作。
I hope this helps.
我希望这有帮助。
回答by jbndlr
Much like your setup, I am using matplotlib
from within a virtual environment on OSX.
与您的设置非常相似,我matplotlib
在 OSX 上的虚拟环境中使用。
I see that you have spent quite some effort to get things running and I am not certain whether or not this will actually solve your problem, since no two setups are the same. However, I drop my configuration such that you can compare and possibly find something that helps you solving this issue.
我看到您已经花费了相当多的精力来让事情运行起来,我不确定这是否真的能解决您的问题,因为没有两个设置是相同的。但是,我放弃了我的配置,以便您可以比较并可能找到可以帮助您解决此问题的内容。
Disclaimer: I use virtualenvwrapper
and have stored my venvs under ~/.virtualenvs/
.
免责声明:我使用virtualenvwrapper
并将我的 venvs 存储在~/.virtualenvs/
.
Copy the
frameworkpython
file to~/.virtualenvs/YOUR_VENV/bin
Make sure to change the
PATHTOPYTHON
variable within theframeworkpython
bash script to refer to the correct location. For my setup, I have changed it to/usr/bin/
(while the original setting was/usr/local/bin
)Set executable flag
chmod +x frameworkpython
Install libs using
pip
as usual after switching to virtual environment using virtualenvwrapper:workon YOUR_VENV
将
frameworkpython
文件复制到~/.virtualenvs/YOUR_VENV/bin
确保更改
PATHTOPYTHON
frameworkpython
bash 脚本中的变量以引用正确的位置。对于我的设置,我已将其更改为/usr/bin/
(而原始设置为/usr/local/bin
)设置可执行标志
chmod +x frameworkpython
pip
使用 virtualenvwrapper 切换到虚拟环境后,照常使用安装库:workon YOUR_VENV
Sourcing frameworkpython
as regular user (no root, pls!) and importing matplotlib
yields:
frameworkpython
以普通用户身份采购(没有 root,请!)并导入matplotlib
收益:
$ ./frameworkpython
Python 2.7.0 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib as mpl
>>> mpl.__version__
'1.5.1'
>>> import matplotlib.pyplot as plt
>>> ^D
回答by pylang
Extending @Gator_Python's and @juanpa.arrivillaga's suggestions, I recommend installing the latest version of anaconda. It is a cross-platform, pip-like package manager that also handles virtual environments. It also installs large packages like numpy
, pandas
and matplotlib
with little headache.
扩展@Gator_Python 和@juanpa.arrivillaga 的建议,我建议安装最新版本的anaconda。它是一个跨平台的、类似 pip 的包管理器,也可以处理虚拟环境。它还可以安装像numpy
,这样的大包,pandas
而且matplotlib
几乎不会让人头疼。
Setup: From the commandline, just create a virtual environment and install from there. Here is an example of how to set up conda (substitute the appropriate environment name and python version number respectively):
设置:从命令行,只需创建一个虚拟环境并从那里安装。以下是如何设置 conda 的示例(分别替换相应的环境名称和 python 版本号):
conda update conda
conda update anaconda
conda create -n <envname> python=<version> matplotlib
source activate <envname>
conda info --envs # show current env
conda list # see installed env packages
This should place you in an isolated, clean environment. You can see a minimal list of packages installed in this environment. Make sure pip
and setuptools
are listed (they are installed automatically). For any other packages you need, use either conda install <package>
or pip install <package>
, in that order. Install your project:
这应该将您置于一个孤立、干净的环境中。您可以看到此环境中安装的最小软件包列表。确保pip
和setuptools
已列出(它们是自动安装的)。对于您需要的任何其他软件包,请按顺序使用conda install <package>
或pip install <package>
。安装你的项目:
cd <path\to\project>
python setup.py develop
Removal: When you are ready to remove your environment:
移除:当您准备好移除您的环境时:
source deactivate
conda env remove -n <envname>
Note: alternatives to setup.py develop
are:
注意:替代方案setup.py develop
是:
回答by Luke Exton
To get pyside working:
要让 pyside 工作:
brew install cmake
brew is a pretty standard OSX dependency.
brew 是一个非常标准的 OSX 依赖项。
You are right in identifying that this is a framework issue Differences between Framework and non-Framework builds of Python on Mac OS XTo get this running, you will need the framework python running.
您确定这是一个框架问题是正确的 Mac OS X 上 Python 的框架构建和非框架构建之间的差异要使其运行,您需要运行框架 python。
And even more correct in not running sudo commands w/o understanding them. Why you might be able to see it, but not be able to run it could be due to not having the environment established correctly as sudo. How to keep Environment Variables when Using SUDO
更正确的是不运行 sudo 命令而不理解它们。为什么您可能能够看到它,但无法运行它,可能是因为没有将环境正确设置为 sudo。使用 SUDO 时如何保持环境变量
if you run which frameworkpython
it should show you the path to the executable on the file system. I would check the file permissions on this file, in particular check the ownership.
如果您运行which frameworkpython
它,它应该会显示文件系统上可执行文件的路径。我会检查这个文件的文件权限,特别是检查所有权。
ls -l $(which frameworkpython)
The ouptut of which frameworkpython
should be the path that could be executed with root, but I would avoid it, as any other dependancies might not be loaded correctly, or can have the wrong permissions set, it might work now, but will get you in trouble later on.
的输出which frameworkpython
应该是可以用 root 执行的路径,但我会避免它,因为任何其他依赖项可能无法正确加载,或者可能设置了错误的权限,它现在可能工作,但以后会给你带来麻烦在。
/path/to/frameworkpython krls.py
When I ran into this issue this blogwas an awesome resource.
当我遇到这个问题时,这个博客是一个很棒的资源。
回答by user3480223
sudo apt-get install tk-dev libpng-dev libffi-dev dvipng texlive-latex-base
Then uninstalled and reinstalled matplotlib:
然后卸载并重新安装matplotlib:
pip uninstall matplotlib
pip install matplotlib
回答by Charlie Parker
To echo Pedro Cattori's answer, the easiest way that has worked every time for me is to simply use python 3 with a virtual env as in:
为了回应 Pedro Cattori 的回答,对我来说每次都有效的最简单方法是简单地将 python 3 与虚拟环境一起使用,如下所示:
python -m venv my-virtualenv
source my-virtualenv/bin/activate
or
或者
python3 -m venv my-virtualenv
source my-virtualenv/bin/activate
from original FAQ page: http://matplotlib.org/faq/osx_framework.html#osxframework-faq
来自原始常见问题解答页面:http: //matplotlib.org/faq/osx_framework.html#osxframework-faq
Everything else I've tried has resulted in ridiculous amount of code and time spent figuring out whats wrong. With that it always works every time without me further doing much more beyond pip installing matplotlib.
我尝试过的所有其他方法都导致了大量的代码和花在找出问题上的时间。有了它,它每次都可以正常工作,而我除了 pip 安装 matplotlib 之外,还做更多的事情。