去年夏天我想学习做网络开发,所以我安装了 Django 1.8,一个不稳定的版本。我没有pip就安装了它。我最近决定再试一次,但我想使用稳定版本 1.7.1,并且为了简单起见,我想使用 pip 安装。
我读到要删除没有安装 pip 的 Django,必须删除整个 .egg
来自 /Library/Python/2.7/site-packages/
,所以我这样做了。
从那时起,我就遇到了一些有趣的错误。当我发出“pip install django”时,我得到 p>
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。为此,您需要 super 用户权限,但这只会执行一次:
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
$
您在虚拟环境中安装的任何东西都只能在虚拟环境中使用,因此您需要确保在运行脚本时处于正确的环境中。
关于python - django安装问题[python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27539816/