Do I need to install python packages as root [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What are site-packages in python and can you install/use them if you aren’t root?
sudo apt-get install pip
creating /usr/local/lib/python2.7/dist-packages/boto
error: could not create '/usr/local/lib/python2.7/dist-packages/boto': Permission denied
pip install boto modifies the packages under /usr/local/bin in a Ubuntu box. Is this the way its supposed to work?

You can use the --user flag when installing python packages via pip or setup.py. This bypasses the need for root access by installing the package for the current user.
Some packages, such as virtualenv need to be installed by root but this is not a common requirement. It's always best to check package docs for installation requirements just in case.
See for more details:
How to manually install a pypi module without pip/easy_install?
How can I install packages in my $HOME folder with pip?

The only package you should install as root is virtualenv :
#pip install virtualenv
Then, you can work in a virtual environment as a normal user. It permits you to experiment without breaking everything in your system:
$virtualenv myproject
$cd myproject/
$./bin/pip install boto

Every python package you install will be installed in root directory, unless you use virtualenv. So you will need root access to modify the files in the installation folder. Try using sudo pip install boto.

Related

Module not found error in Python after pip install package --user

I had Permission Denied error when I installed seaborn (or any libray) library (pip install seaborn - on command prompt and Jupyter notebook). I tried pip install seaborn --user and it showed that Requirements already satisfied. However, when I try to import seaborn on jupyter notebook, it returns module not found error.
You're probably attempting to download package to a system folder where you don't have permission to write or modified, maybe it's due to modification on your pip.ini file which specify by default the packages location ?
Check pip documentation and his topic about configuration here.
Furhtermore, I advise you, if you start learning python and using pip package, to learn fundamental about the principle of virtualenv using venv lib here. VirtualEnv is a recommended practice in python who allow you to compartmentalize project in a specified place with a personal pip package directory and other things, these methods aim to avoid dependancy concurrence between projects and also avoid pip package permissions issue by downloading pip package only for your virutalenv.
Step by step to setup venv ( you should be in the root folder of your
projet) :
user#hostname > python3 -m venv venv
user#hostname > source ./env/bin/activate
(venv) user#hostname > python -m pip install foo-packages
Or the second option but not recommended is to use sudo to
install packages to the system folder :
sudo#hostname > sudo python -m pip install foo-packages

How can I see what packages were installed using `sudo pip install`?

I know that installing python packages using sudo pip install is bad a security risk. Unfortunately, I found this out after installing quite a few packages using sudo.
Is there a way to find out what python packages I installed using sudo pip install? The end goal being uninstallment and correctly re-installing them within a virtual environment.
I tried pip list to get information about the packages, but it only gave me their version. pip show <package name> gave me more information about an individual package such as where it is installed, but I don't know how to make use of that information.
any modules you installed with sudo will be owned by root, so you can open your shell/terminal, cd to site-packages directory & check the directories owner with ls -la, then any that has root in the owner column is the one you want to uninstall.
When you run sudo pip install, pip will install the package in your global site-packages directory.
So, to determine which packages you've installed with sudo pip install, you can navigate to your /site-packages directory.
The site-packages directory is a sub-directory of your python installation. For example, /Users/me/Library/Python/2.7/lib/python/site-packages.
This SO post has a more detailed discussion regarding how to find the site-packages directory.
Hope this helped!
try the following command: pip freeze

pip has to reinstall all packages in exported virtualenv

I have a question about python virtualenv. I get a virtualenv for a project with all packages required to run that project. But when i run it for the first time and it crash 'cause python has some requirements not satisfied. So i check if there is all packages inside:
virtualenv/lib/python2.7/site-packages/
and all packages required are inside.
But when i type:
pip list
packages doesn't shown. So i have to run:
pip install -r requirements.txt
pip downloads them again.
So my question is, why pip downloads and reinstall them again if they are installed yet ? And how i can force pip to reinstall all packages inside virtualenv ?
The problem was that all scripts inside the virtualenv were created on another pc with them paths. Indeed when i launched python or pip from virtualenv they ran from my global path 'cause couldn't find virtualenv script path and in particular pip shown my global packages.
Fixing directives path of all script inside virtualenb/bin/ to my real virtualenv path solved this issue.

Equivalent in python of package.json and "npm install --save" command to easly save every new package [duplicate]

I've been looking around for a package manager that can be used with python. I want to list project dependencies in a file.
For example ruby uses Gemfile where you can use bundle install.
How can I achieve this in Python?
The pip tool is becoming the standard in equivalent of Ruby's gems.
Like distribute, pip uses the PyPI package repository (by default) for resolving and downloading dependencies.
pip can install dependencies from a file listing project dependencies (called requirements.txt by convention):
pip install -r requirements.txt
You can "freeze" the current packages on the Python path using pip as well:
pip freeze > requirements.txt
When used in combination with the virtualenv package, you can reliably create project Python environments with a project's required dependencies.
Pipenv
(I know it's an old question, and it already has an answer but for anyone coming here looking for a different answer like me.)
I've found a very good equivalent for npm, It's called pipenv. It handles both virtualenv and pip requirements at the same time so it's more like npm.
Simple Use Case
pip install pipenv
then you can make a new virtualenv with third version of python, as well as making a pipfile that will be filled with your projects requirement and other stuff:
pipenv install --three
using your created virtualenv:
pipenv shell
installing a new python package:
pipenv install requests
running your .py file is like:
pipenv run python somefile.py
you can find it's doc here.
Python uses pip for a package manager. The pip install command has a -r <file> option to install packages from the specified requirements file.
Install command:
pip install -r requirements.txt
Example requirements.txt contents:
Foo >= 1.2
PickyThing <1.6,>1.9,!=1.9.6,<2.0a0,==2.4c1
SomethingWhoseVersionIDontCareAbout
See the Requirements Parsing section of the docs for a full description of the format: https://pip.pypa.io/en/stable/user_guide/#requirements-files
This is how I restrict pip's scope to the current project. It feels like the opposite if you're coming from NodeJS's npm or PHP's composer where you explicitly specify global installations with -g or --global.
If you don't already have virtualenv installed, then install it globally with:
pip install virtualenv
Each Python project should have its own virtualenv installation. It's easy to set one up, just cd to your project's root and:
python3 -m virtualenv env # creates env folder with everything you need
Activate virtualenv:
source env/bin/activate
Now, any interaction with pip is contained within your project.
Run pip install package_name==version for each of your dependencies. They are installed in ./env/lib/python3.x/site-packages/
When you want to save your project's dependencies to a file, run:
pip freeze > requirements.txt
You actually don't need -l or --local if you're in an activated project-specific virtualenv (which you should be).
Now, when you want to install your dependencies from requirements.txt, set up your virtualenv, and run:
pip install -r requirements.txt
That's all.
This is an old question but things are constantly evolving.
Further to the other answer about pipenv. There is also a python package manger called poetry.
There is a detailed comparison between pipenv and poerty here: Feature comparison between npm, pip, pipenv and poetry package managers. It also links the features to common npm features.
Here is a comparison of pipenv vs poetry vs pdm: https://dev.to/frostming/a-review-pipenv-vs-poetry-vs-pdm-39b4
The conclusion is that pdm is the winner.
But in my experience, poetry is easier than pdm to integrate with IDEs.

pip installs packages, but doesn't mark them as executable, so python doesn't find them

I've been trying to use sudo to install python packages on a system that I am on the sudoers list, but don't have general root access (i.e. don't have the password for su). I can install packages, for example
sudo pip install django
however when I try and use them python simply claims not to have the package installed. Investigating the contents of /usr/lib/python it appears that other packages directories and .eggs have executable permissions for ugo, however the packages I install using sudo pip do not have this permission. Manually giving these files executable permissions fixes the problem, but that is laborious, particularly when pip installed several dependencies that I need to chase up.
Is this a known issue? What can I do about it? For the record this is a RHEL6.4 machine and I'm using pip 1.4.1.
You best bet is virtualenv Do your workaround to install the virtualenv.
sudo pip install virtualenv
Resources for virtualenv to get you started:
http://simononsoftware.com/virtualenv-tutorial/
http://www.virtualenv.org/en/latest/

Categories

Resources