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

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

Related

Installing requirements via "python -m pip install" randomly throws WinError5 exception

OS: Windows 10, 64-bit
Python: 3.9.1
Pip: 21.0.1
Editor: JetBrains Pycharm 2020.3.3
I have cloned a private GitHub repository via Pycharm and created the project's venv as usual after selecting my project interpreter. Ran python -m pip install -U --force-reinstall pip setuptools wheel after. Versions of all modules are up-to-date.
Pycharm automatically activates the created venv, so there is no need to manually activate it.
When running python -m pip install -r requirements.txt, this error (seemingly) randomly occurs:
ERROR: Could not install packages due to an OSError: [WinError 5] Access is denied: {file_path within venv}
Consider using the `--user` option or check the permissions.
I have checked for this error, and all sources seem to imply a permission mismatch, but I cannot figure out why there would be any.
So, I opened cmd as admin, navigated to my project folder and activated the venv through .\venv\Scripts\activate. There was no issue here.
Naturally, I ran python -m pip install -U --force-reinstall -r requirements.txt and the same error still occurred - even with admin permissions. (pip seems to fail randomly - sometimes e.g. numpy is able to be installed, sometimes not)
What could I try next in order to make it work? Have I made any mistakes so far? How could I proceed?
EDIT:
Using the --user flag does not work. ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.
EDIT 2:
Deleting the venv directory, and reinstalling it with the following commands seems to have fixed the issue when installing the project requirements for the first time:
python -m venv .\venv
.\venv\Scripts\activate
python -m pip install -U --force-reinstall pip setuptools wheel
python -m pip install -r requirements.txt
The project is able to be run now, but out of curiosity, I tried running this command again:
python -m pip install -U --force-reinstall -r requirements.txt
The error still occurs, so it doesn't seem like the issue is fixed by recreating the venv, but at least the project can be run now. Not closing the question myself, as the "actual problem" does not seem to be fixed.
You probably have the problem, that sometimes code doesn't work due to a very long path.
Try to reinstall python(/the venv python) and search for a point where you can check a box so that the path isn't limited anymore

Python on anaconda cannot find azure.mgmt.datafactory

I am trying to run this tutorial
https://learn.microsoft.com/en-US/azure/data-factory/quickstart-create-data-factory-python
but I fail to install the packages. I tried several installations but I keep getting the error No module named 'azure.mgmt.datafactory' when trying to run from azure.mgmt.datafactory import DataFactoryManagementClient.
I am using anaconda and windows 10.
I tried running the recommended anaconda packages https://anaconda.org/anaconda/azure and https://anaconda.org/clinicalgraphics/azure-mgmt-resource under a python 3.5 environment and I also tried to manually install everything from github (https://github.com/Azure/azure-sdk-for-python) using
git clone git://github.com/Azure/azure-sdk-for-python.git
cd azure-sdk-for-python
python setup.py install
In both the normal (Python 3.6) and the new (Python 3.5, using Anaconda version with Python 3.5) environment. None of this worked.
What am I missing?
(Note that from azure.mgmt.resource import ResourceManagementClient worked fine with the anaconda installation)
EDIT
After the first response, I ran the following commands from the powershell
pip install azure-mgmt-resource
pip install azure-mgmt-datafactory
pip install azure-mgmt
which resulted in ModuleNotFoundError: No module named 'azure.mgmt'
Uninstalling the three packages and installing azure-mgmt as a first one did not solve the issue either. However, I don't know how to uninstall the manually installed package from python setup.py install, which still might be an issue.
Have you tried pip install in powershell/cmd?
pip install azure-mgmt-datafactory
Update (Jan's answer):
pip freeze > requirements.txt
pip uninstall -r requirements.txt
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory (this might not be needed as it comes with azure-mgmt)
Ok, this is how I got the required azure libraries to work (thx to Saul Cruy, who gave me the idea)
Using this post What is the easiest way to remove all packages installed by pip?, I created a requirements file in PowerShell
pip freeze > requirements.txt
In this file, I manually kept only the entries with azure.
Then, I deleted all packages in the file
pip uninstall -r requirements.txt
The steps above were repeated twice, as upon first delete, some azure packages survived.
Then, I ran (all in PowerShell, in that order)
python -m pip install azure-common
python -m pip install azure-mgmt
python -m pip install azure-mgmt-datafactory
The reason might(!) be that installing packages in the anaconda console using the conda commands causes confusion in the dependencies (I tried a similar approach in a conda environment as it seemed like a good idea to seperate the azure packages from the other ones, but without success).

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.

Install package locally

I use python's pip to install packages. Now I want to install scipy, which is already installed on the system, but an old version and on a part of the system where I don't have access to. If I try
pip install scipy
pip rightfully tells me that the package is already installed. If I do
pip install scipy --upgrade
pip tries to upgrade the package but I don't have the access rights to do that.
How can I tell pip to install the package local to my user and to ignore the other scipy package?
I think the best way for avoid override packages it's using a virtual environment. Python has it's own virtual environment and you could install it by:
Python 2.7
> sudo apt-get install python-virtualenv
Python 3
> sudo apt-get install virtualenv
With modern python versions, virtualenv is usually included. Once installed, you could generate a virtual enviroment typing:
> virtualenv venv
This would create a folder in the current directory named venv (you could name it whatever you want). In this package the libraries will be installed.
So, it's time to activate the virtual environment
> source venv/bin/activate
You could verify the environment has been activated by checking the prompt changes. If it happens, all the packages installed using pip will be installed locally.
(venv)> pip install scipy
You could check this website for more info.
Don't forget that you eventually have to clear your $PYTHONPATH variable, in order for it to not pick up other packages.

Recommended way to install a Python package with pip and no sudo privileges

I usually just use the command:
pip install --user <package>
but I've seen here that this:
pip install <package> --install-option="--prefix=~"
can also be used to bypass the need for sudo privileges. About this command the site says:
There is also a –user option with pip install, which installs into ~/.local. This is fine for the python module, but it puts the corr2 executable into ~/.local/bin, which is probably not in your path. The above command will instead install corr2 into ~/bin.
So apparently it does not behave the same way as the first command.
Is one way preferred over the other and if so why?
The official Python package installation guide is here:
https://packaging.python.org/en/latest/installing.html
It recommends creating Python virtual environments per project using virtualenv command (or python3.4 -m venv).
This is because if you are working with multiple Python projects they have different dependencies and having per project installation environments is the sane way to deal with this in Python.

Categories

Resources