is using the pip --user option as safe as creating

2019-04-02 15:04发布

Does installing packages in the home directory of a user with pip install --user provide the same level of protection against system-breaking changes as using a virtualenv?

2条回答
Rolldiameter
2楼-- · 2019-04-02 15:17

Does installing packages in the home directory of a use with pip install --user provide the same level of protection against system-breaking changes as using a virtualenv?

By "system-breaking changes", I suppose you mean packages installed by the operating system's package manager tool. With the --user option of pip, packages will be installed in the user's home directory. And since the package manager is not supposed to depend on user directories, but use only packages installed at the designated location in the system, independent from users's junk, a properly managed system should not be possible to break using pip install --user.

However, if you work with more than one Python project with your user, it makes sense to always use virtualenv consistently, to prevent versioning conflicts between the projects.

查看更多
祖国的老花朵
3楼-- · 2019-04-02 15:35

Using a virtualenv is preferable for a few small reasons, and one big reason.

  • virtualenv has a "relocate" option (Note: this feature has been flagged as having issues and may not work in all circumstances). Using --user you would need to reinstall all packages if you tried to relocate your project to another machine.

  • Unless you change the PYTHONPATH so that modules in site-packages are not loaded, and reinstall every module in your user directory, python will continue to search for modules that are installed in the system directory. If you are considering using --user, I assume you either don't have permission to install system packages, or you are worried about breaking links in the future. Unlike --user, virtualenv keeps track of all modules (including system-wide modules and modules installed in the virtualenv), and as such I'd imagine it will be less likely to "break something" (or, at least, it will be easier to identify what the problem is) if you're using virtualenv.

These problems could be nuisances, but they are surmountable. The biggest difference between --user and virtualenv is that virtualenv will allow you to store one version of each package for every environment you create, thereby eliminating versioning concerns (i.e., you build a project to work with one version of a package, then later you upgrade the package to work on a new project using some new feature and find that your old project is now broken). This is a pretty big deal and --user does nothing to help in this respect (unless you want to create a new user account on your machine for each project you work on, which I don't recommend).

查看更多
登录 后发表回答