去年夏天,我想学习Web开发,所以我安装了Django 1.8(一个不稳定的版本)。我没有点子就安装了它。我最近决定重试,但想使用稳定版本1.7.1,并希望使用pip进行安装以简化操作。
[我读到为了删除不带pip的Django,必须从.egg
中删除整个/Library/Python/2.7/site-packages/
,所以我这样做了。
从那时起,我一直在收到有趣的错误。当我发出'pip install django'时,我得到
bash-3.2$ pip install django
Downloading/unpacking django
Downloading Django-1.7.1-py2.py3-none-any.whl (7.4MB): 7.4MB downloaded
Installing collected packages: django
Cleaning up...
Exception:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/basecommand.py", line 122, in main
status = self.run(options, args)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/commands/install.py", line 283, in run
requirement_set.install(install_options, global_options, root=options.root_path)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 1435, in install
requirement.install(install_options, global_options, *args, **kwargs)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 671, in install
self.move_wheel_files(self.source_dir, root=root)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/req.py", line 901, in move_wheel_files
pycompile=self.pycompile,
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 215, in move_wheel_files
clobber(source, lib_dir, True)
File "/Library/Python/2.7/site-packages/pip-1.5.6-py2.7.egg/pip/wheel.py", line 205, in clobber
os.makedirs(destdir)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py", line 157, in makedirs
mkdir(name, mode)
OSError: [Errno 13] Permission denied: '/Library/Python/2.7/site-packages/django'
当我检查虚假目录时,发现/ django不存在。发出sudo将使错误消失,但是我发现我仍然无法启动django项目。
以前有没有人看过这个,或者不知道如何解决?
这不是真正的错误-由于您的普通用户帐户无法写入全局/Library
文件夹,因此您正在从操作系统中收到此权限被拒绝的错误。
[最佳做法是在称为virtual environment的virtualenv
中工作。这样可以确保在尝试使用Python软件包时,不会意外破坏Python在系统范围内的安装。
在使用的Mac上,可能还有其他依赖于Python的系统实用程序,如果系统默认的Python损坏,它们可能会停止工作。
因此,第一步是在系统上安装virtualenv globally。为此,您需要超级用户权限,但这仅执行一次:
sudo easy_install virtualenv
以上将要求输入密码-只需输入您的普通用户密码。
上述命令完成后,普通用户从命令行运行以下命令:
$ virtualenv my_env # This creates a new Python environment called my_env, completely
# separate from your system wide install.
$ source my_env/bin/activate # Activates the virtual envrionment
(my_env) $ pip install django # Install django in this new environment the (my_env)
# indicates you are working inside a virtual environment
...
(my_env) $ deactivate # Deactivates the environment,
# returning you to the normal system Python
$
您在虚拟环境中安装的所有内容仅在虚拟环境中可用,因此在运行脚本时需要确保您在正确的环境中。
我知道了。我必须去/usr/local/bin/
并删除所有对django 1.8安装的引用(django-admin等),然后按照说明安装virtualenvwrapper
并在虚拟环境中工作。
为了避免virtualenvwrapper
,我建议在Mac OS中使用virtualenv
,
将软件包安装在类似于以下的位置:
~~ Library / Python / 2.7 / lib / python / site-packages
来自pip install --user Django
:
pip install --help
然后我想再用virtualenv和virtualenvwrapper --user Install to the Python user install directory for your platform. Typically ~/.local/, or %APPDATA%\Python on Windows.
(See the Python documentation for site.USER_BASE for full details.)
安装Django,]?
首先,需要设置虚拟环境或工作区并激活它。
Windows用户:以(以管理员身份运行)启动电源外壳或以(以管理员身份运行)启动CMD运行以下命令后]
运行以下命令: