django安装问题[python]

2024-02-06

去年夏天我想学习 Web 开发,所以我安装了 Django 1.8,一个不稳定的版本。我是在没有pip的情况下安装的。我最近决定再试一次,但想使用稳定版本 1.7.1,并且为了简单起见想使用 pip 安装。

我读到为了删除没有 pip 安装的 Django,必须删除整个.egg from /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文件夹,您从操作系统收到此权限被拒绝错误。

最佳实践是在虚拟环境 http://docs.python-guide.org/en/latest/dev/virtualenvs/ called virtualenv简而言之。这可以确保当您尝试使用 Python 包时,不会意外损坏 Python 的系统范围安装。

在 Mac(您正在使用的)上,可能有其他依赖 Python 的系统实用程序,如果系统默认 Python 损坏,它们可能会停止工作。

所以第一步是安装virtualenvglobally在您的系统上。为此,您需要超级用户权限,但这只需完成一次:

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
$

您在虚拟环境中安装的任何内容都只能在虚拟环境中使用,因此您需要确保在运行脚本时处于正确的环境中。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

django安装问题[python] 的相关文章

随机推荐