我正在尝试使用以下命令将 timestring 库安装到 ubuntu 16.04 上的 python3.5 中:pip3 install timestring
收到此错误:
Exception:
Traceback (most recent call last):
File "/home/user/.local/lib/python3.5/site-packages/pip/basecommand.py", line 215, in main
status = self.run(options, args)
File "/home/user/.local/lib/python3.5/site-packages/pip/commands/install.py", line 317, in run
prefix=options.prefix_path,
File "/home/user/.local/lib/python3.5/site-packages/pip/req/req_set.py", line 742, in install
**kwargs
File "/home/user/.local/lib/python3.5/site-packages/pip/req/req_install.py", line 831, in install
self.move_wheel_files(self.source_dir, root=root, prefix=prefix)
File "/home/user/.local/lib/python3.5/site-packages/pip/req/req_install.py", line 1032, in move_wheel_files
isolated=self.isolated,
File "/home/user/.local/lib/python3.5/site-packages/pip/wheel.py", line 346, in move_wheel_files
clobber(source, lib_dir, True)
File "/home/user/.local/lib/python3.5/site-packages/pip/wheel.py", line 317, in clobber
ensure_dir(destdir)
File "/home/user/.local/lib/python3.5/site-packages/pip/utils/__init__.py", line 83, in ensure_dir
os.makedirs(path)
File "/usr/lib/python3.5/os.py", line 241, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/local/lib/python3.5/dist-packages/pytz-2013b0.dist-info'
我不确定到底出了什么问题。如何安装时间字符串库?
答案1
它正尝试安装在系统目录中,但您的用户没有权限执行此操作。
您有 2 个选择:
- 本地安装,通常在虚拟环境中。请参见此处:http://docs.python-guide.org/en/latest/dev/virtualenvs/
- 全局安装,使用 sudo 以便您拥有 root 权限:
sudo pip install timestring
就我个人而言,我更喜欢选项 1,并将每个项目的依赖关系隔离在 venv 中,但这取决于您。
答案2
查看回溯,pip3
正在尝试在中创建一个目录,/usr/local/lib/python3.5/dist-packages
但它没有权限这样做,因为您可能没有pip3
使用运行sudo
。不过,安装的最佳方法timestring
是完全避免sudo
使用 而改用virtualenv
。以下代码对我有用:
sudo apt update && sudo apt install virtualenv # install virtualenv if you haven't
cd ~/<project directory> # cd to your project directory
virtualenv venv -p python3 # create virtualenv named venv for Python 3
source venv/bin/activate # activate virtualenv
pip install timestring
完成项目代码编写后,运行deactivate
以停用虚拟环境。