Python: Unable to install as a module but a package - python

As I need to run python with specifying a module, but not to run the install package as the executable, I tried to install the package locally,
but it does not install as a module but as a package for some reason.
Here is the series of commands what I did.
# enable virtualenv
# now, no external pip package is installed
$ . ./venv02/bin/activate
# install the module
$ pip install mitmproxy
# I thought this should be worked, but it does not.
$ python -m mitmproxy --mode upstream:https://localhost:3148 --listen-port 8085 --scripts ./mitmproxy_main.py
/home/user/pg/python/venv02/bin/python: No module named mitmproxy.__main__; 'mitmproxy' is a package and cannot be directly executed
# but as a package I can run
# but this is not what I desire,
# since in the later step I need to use the "module" mitmproxy in vscode launch.json to debug the python script ./mitmproxy_main.py.
$ mitmproxy --mode upstream:https://localhost:3148 --listen-port 8085 --scripts ./mitmproxy_main.py
The virtualenv Python version and pip version is as follows.
(venv02) user#pc:~/pg/python$ python --version
Python 3.6.12
(venv02) user#pc:~/pg/python$ pip --version
pip 21.2.4 from /home/user/pg/python/venv02/lib/python3.6/site-packages/pip (python 3.6)
So How can I install a "module" locally to run python -m [module]? Thanks.

As much as I know, to install python packages locally :
for python 3: pip3 install --user <package>
Also try doing :
pip3 install <your_python_module_name> -t lib/

Related

pip installing python packages for the wrong version

I'm trying to use pip to install some packages that I need, but I came across a problem. My default version of python is python3.10 but when I try using pip to install packages for it, it installes them for python3.8
This is the output of pip --version:
pip 22.1.2 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
Is there a way to change this so that I can install packages for python 3.10?
When I try to run python3 -m pip --version I get:
/usr/local/bin/python3: No module named pip
Download get-pip.py file from any of the following options:
Download it manually from here.
Download it from terminal/cmd using: wget https://bootstrap.pypa.io/get-pip.py
Download it from terminal/cmd using: curl https://bootstrap.pypa.io/get-pip.py
Then run the command python3 get-pip.py, it will install pip in the python version invoked by python3(which is 3.10 as you have stated).
Install packages using command python3 -m pip install package_name
Since you've said you can call the desired python version with the command python3, it's best to call pip via
python3 -m pip
This ensures you're installing packages to the correct version (Dealing with multiple Python versions and PIP?).
To install pip if it's missing, you can use ensurepip (as per this answer)
python3 -m ensurepip
It's probably worth mentioning venv which lets you create a local python environment to keep your packages separate for different projects.

Are pip and python consistent? Seems like the answer is no. Can someone help me decrypt the documentation?

So i'm trying to implement stripe on a Django app and i'm having issues.
I installed Stripe using pip3 -install stripe and it downloaded. However when I run the server it says
ModuleNotFoundError: No module named 'stripe'
So looking around and on this I think I found some sort of an answer.
https://nomodulenamed.com/a/I-have-installed-the-package-using-pip#fail-to-install
Are pip and python consistent?
Seems like the answer is no.
pip3 -V returned pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
and
python3 -V returned Python 3.8.2
It seems that the easy fix is using python3 -m pip3 -V but that returns No module named pip3
and
python3 -m pip -V returns pip 20.1 from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
but pip -V returns zsh: command not found: pip
which leaves me quite confused
more over i'm suppose to do # install your package
python -m pip <your-package-name>
so what goes in the place of
<your-package-name>
and I'm I suppose to use pip of pip3 since I use pip3 to install thing.
Since you can have more than one Python2 installation and more than one Python3 installation available on your machine, your question is better answered by understanding virtual environments.
It is precisely the reason why virtual environments exist!
when you create a python3 virtual environment there is no need to call pip3 as it is the default pip.
start by creating your virtual env (Assuming you have virtualenv installed ... if not install it on linux ubuntu by calling
sudo apt-get install virtualenv
sudo apt-get install python3-pip
python3 -m venv env
source bin/env/activate
pip install <yourpackage>
but I believe you are on macOS since you are getting zsh error,
fix your installation by using homebrew
brew install python3
pip3 install virtualenv
virtualenv -p python3 <path-to your-project>
source <path-to your-project>/bin/activate

How to install a module to one of many pythons in Fedora 20

I have on my Fedora 20 additionally to 2.7 a python3.6 version installed.
When I run a script with the 3.6 version it's missing the requests module.
When I try to install it with the pip command it says it's already there.
So, how can I install this module in python3.6?
Any hints?
Thanks
Check if pip36 or more likely pip3 is a function you can run. Often times the pip command corresponds to the first installed python version, so if you install one later it gets the suffix according to its version. If that is the case then you'll want to do pip36 (pip3) install moduleXYZ.
Quick and dirty answer is
For python2,x install any package using pip install requests
For python 3.x install any package using pip3 install requests
If you get error during pip3 please run sudo dnf install python3-pip
But the right way is to do it using Virtual Environment in fedona
For py3.4+
$ python3.5 -m venv env # create the virtualenv
$ . env/bin/activate # activate it
(env)$ python -m pip install requests # install a package with pip
For py2.x, 3.x
$ dnf install python-virtualenv # install the necessary tool
$ virtualenv --python /usr/bin/python2.7 env # create the virtualenv
Running virtualenv with interpreter /usr/bin/python2.7
New python executable in env/bin/python2.7
Also creating executable in env/bin/python
Installing setuptools, pip...done.
$ . env/bin/activate # activate it
(env)$ python -m pip install requests # install a package with pip
Refrence

How to install pip for a specific python version

I have my deployment system running CentOS 6.
It has by default python 2.6.6 installed. So, "which python" gives me /usr/bin/python (which is 2.6.6)
I later installed python3.5, which is invoked as python3 ("which python3" gives me /usr/local/bin/python3)
Using pip, I need to install a few packages that are specific to python3. So I did pip install using:-
"sudo yum install python-pip"
So "which pip" is /usr/bin/pip.
Now whenever I do any "pip install", it just installs it for 2.6.6. :-(
It is clear that pip installation got tied to python 2.6.6 and invoking pip later, only installs packages for 2.6.6.
How can I get around this issue?
If pip isn’t already installed, then first try to bootstrap it from the standard library:
$ python3.5 -m ensurepip --default-pip
If that still doesn’t allow you to run pip:
Securely Download get-pip.py.
Run sudo python3.5 get-pip.py.
Now you can use pip3 to install packages for python3.5. For example, try:
$ sudo pip3 install ipython # isntall IPython for python3.5
Alternatively, as long as the corresponding pip has been installed, you can use pip for a specific Python version like this:
$ python3.5 -m pip install SomePackage # specifically Python 3.5
References:
Ensure you can run pip from the command line
work with multiple versions of Python installed in parallel?
I have python 3.6 and 3.8 on my Ubuntu 18.04 WSL machine. Running
sudo apt-get install python3-pip
pip3 install my_package_name
kept installing packages into Python 3.6 dist directories. The only way that I could install packages for Python 3.8 was:
python3.8 -m pip install my_package_name
That installed appropriate package into the Python 3.8 dist package directory so that when I ran my code with python3.8, the required package was available.
Example of how to install pip for a specific python version
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
/opt/local/bin/python2.7 get-pip.py
Script is from official doc: https://pip.pypa.io/en/stable/installing/
On Ubuntu 18.04.1 LTS I wanted to install pip for my second python version (python3) and the following command did the trick for me:
$ sudo apt install python3-pip

Can't import PIL after installing Pillow

I have python 3.x, and was told to install Pillow for image manipulation. After installing it with pip however, i'm unable to import PIL from the python interpreter. It just says ImportError: No module named 'PIL'. Running pip list in the command line shows that Pillow is indeed installed.
It ended up installing correctly after using easy_install instead of pip.
You might have used pip for a different python executable. To make sure that the correct pip command is used, run:
$ python3 -m pip install pillow
I don't really have the time or the means to create an entire virtual machine just for the purposes of testing this one problem.
virtualenv is not a virtual machine. All you need to create a virtualenv from scratch:
$ python3 -m pip install --user virtualenv # install package
$ python3 -m virtualenv venv # create virtualenv named 'venv'
Then to activate the created virtualenv on POSIX system:
$ source venv/bin/activate # activate on POSIX
Or on Windows:
C:\> venv\Scripts\activate
Now, python, pip commands refers to the virtualenv. To deactivate virtualenv, run:
$ deactivate

Categories

Resources