Import runtime installed module using pip in python 3 - python

I want to install and import Python 3 modules at runtime.
I'm using the following function to install modules at runtime using pip:
def installModules(modules):
for module in modules:
print("Installing module {}...".format(module))
subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])
The module is installed successfully, but I'm not able to import it at runtime, after the installation finishes. So if I do:
modules = [ "wget", "zipfile2" ]
installModules(module)
import wget
I get a ModuleNotFoundError. If, after that, I start another Python 3 session, I am able to use the modules e.g. wget, which means that the modules have been installed, but they are not available for this current Python 3 session.
Is it possible in Python 3 to install and then import the installed modules in the same Python 3 session i.e. right after installation?
Thank you!
EDIT:
On a fresh Ubuntu 19.04 install inside VirtualBox, after a sudo apt-get install python3-pip, running the following script:
import os, sys
import subprocess
def installModules(modules):
for module in modules:
print("Installing module {}...".format(module))
subprocess.call([sys.executable, "-m", "pip", "install", "--user", module])
def process():
modulesToInstall = [ "wget", "zipfile2" ]
installModules(modulesToInstall)
process()
import wget
def main():
wget.download("http://192.168.2.234/test/configure.py")
if __name__ == "__main__":
main()
I get:
user#user-VirtualBox:~$ python3 script.py
Installing module wget...
Collecting wget
Installing collected packages: wget
Successfully installed wget-3.2
Installing module zipfile2...
Collecting zipfile2
Using cached https://files.pythonhosted.org/packages/60/ad/d6bc08f235b66c11bbb76df41b973ce93544a907cc0e23c726ea374eee79/zipfile2-0.0.12-py2.py3-none-any.whl
Installing collected packages: zipfile2
Successfully installed zipfile2-0.0.12
Traceback (most recent call last):
File "script.py", line 17, in <module>
import wget
ModuleNotFoundError: No module named 'wget'
The Python 3 version is:
user#user-VirtualBox:~$ python3 --version
Python 3.7.3
The pip3 version is:
user#user-VirtualBox:~$ pip3 --version
pip 18.1 from /usr/lib/python3/dist-packages/pip (python 3.7)
Other info:
user#user-VirtualBox:~$ whereis python3
python3: /usr/bin/python3.7m /usr/bin/python3.7-config /usr/bin/python3.7 /usr/bin/python3 /usr/bin/python3.7m-config /usr/lib/python3.7 /usr/lib/python3.8 /usr/lib/python3 /etc/python3.7 /etc/python3 /usr/local/lib/python3.7 /usr/include/python3.7m /usr/include/python3.7 /usr/share/python3 /usr/share/man/man1/python3.1.gz
Any ideas?

By default, at startup Python adds the user site-packages dir (I'm going to refer to it as USPD) in the module search paths. But this only happens if the directory exists on the file system (disk). I didn't find any official documentation to support this statement 1, so I spent some time debugging and wondering why things seem to be so weird.
The above behavior has a major impact on this particular scenario (pip install --user). Considering the state (at startup) of the Python process that will install modules:
USPD exists:
Things are straightforward, everything works OK
USPD doesn't exist:
Module installation will create it
But, since it's not in the module search paths, all the modules installed there won't be available for (simple) import statements
When another Python process is started, it will fall under #1.
To fix things, USPD should be manually added to module search paths. Here's how the (beginning of the) script should look like:
import os
import site
import subprocess
import sys
user_site = site.getusersitepackages()
if user_site not in sys.path:
sys.path.append(user_site)
# ...
Update #0
1 I just came across [Python]: PEP 370 - Per user site-packages directory - Implementation (emphasis is mine):
The site module gets a new method adduserpackage() which adds the appropriate directory to the search path. The directory is not added if it doesn't exist when Python is started.

Related

Python PIP package installed not importing

I have created a package via Github's pip instance, it is successfully installed:
Running setup.py install for lsm-shared ... done
Successfully installed lsm-shared-0.1.0
% pip list | grep shared
lsm-shared 0.1.0
If I start python and manually import it then it works fine:
% python
Python 3.9.6 (default, Jul 5 2021, 01:04:38)
>>> import lsm_shared
If I try and use the package in a project I get:
% quart run -p 3030
File "Development/lsm/servie/service/application.py", line 8, in <module>
import lsm_shared
ModuleNotFoundError: No module named 'lsm_shared'
The only difference between running manually and in a project is that I set PYTHONPATH, I don't know if that makes any difference.
I have looked at other solutions, including this StackOverflow question, and done the pip install with --user to no avail.

Python3 import error: No module named 'rpm'

On a CentOS 7 I have installed python3 with the following command:
yum install -y python3
However, when I call import rpm, I get this error:
File "\<stdin>", line 1, in \<module>
ModuleNotFoundError: No module named 'rpm'
So how do I import the python3 rpm module?
Running rpm --version returns
RPM version 4.11.3
Accessing python (2.7.5) and running the import command works. So I could call a python2 script from python 3, as described here but this just feels wrong.
There are similar questions to this but they all relate to the rpm module being unavailable from Python2 and I don't have that problem. None deal with making the rpm module available in Python3.
Did you install the rpm python package for the python3 distribution?
pip3 install rpm-py-installer
Per the homepage of RPM this seems to be a required step.

How can i install python modules that have spaces in between?

I wanted to run a script that would scan my network and that script uses a awesome library called who-is-on-my-wifi. I have installed the module to run the script but i get errors from the prompt saying that it cannot detect such a module in the system.
This is the script.
from who_is_on_my_wifi import *
WHO = who()
for i in range(0, len(WHO)):
print(WHO[i])
And this is the error that i get.
python scanner.py
Traceback (most recent call last):
File "scanner.py", line 1, in <module>
from who_is_on_my_wifi import *
ImportError: No module named who_is_on_my_wifi
This is the proof that i have installed the module
pip3 install 'who_is_on_my_wifi'
Requirement already satisfied: who_is_on_my_wifi in /home/harein/.local/lib/python3.8/site-packages (1.2.0)
Requirement already satisfied: getmac in /home/harein/.local/lib/python3.8/site-packages (from who_is_on_my_wifi) (0.8.2)
Requirement already satisfied: python-nmap in /home/harein/.local/lib/python3.8/site-packages (from who_is_on_my_wifi) (0.6.1)
Any suggestions on how i can avoid this can continue executing my script ?
EDIT
The script finally executed the way i want by changing the,
python scanner.py to python3 scanner.py
You guys were right, it was the way how i executed the script that generated this error and it was not a problem in the module apparently.
I would like to thank everyone who gave the support.<3
When trying to
import this_is_not_a_module
the error you get:
ImportError: No module named this_is_not_a_module
is the error raised by Python 2.
Python 3 would raise a different one:
ModuleNotFoundError: No module named 'this_is_not_a_module'
So, your actual problem is that your system tries to execute your script with some Python 2 version, while you installed your module for your Python 3.8 version.
sometimes you can't import modules because you have installed two python versions(or conda along with it). if you have, delete one of your python versions or activate conda and try importing your module, or just try:
pip uninstall who_is_on_my_wifi
pip install who_is_on_my_wifi
I usually tend to install modules per project and use virtualenv for it.
It kind of links respective versions of programs (like python interpreter, pip and so on) and takes care of PYTHONPATH and the way of installed dependencies.
$ pip3 install virtualenv
$ virtualenv whoisonwifi
$ source whoisonwifi/bin/activate
$ pip --version
pip 21.0.1 from
/mnt/devel/workonhome/whoisonwifi/lib/python3.7/site-packages/pip
(python 3.7)
$ pip install 'who_is_on_my_wifi'
from there your code (sort of) works for me.

creating python auto import script file

I am a new bee in python programming, and want to write a python script file which I can use as an 'import' statement on top of first executable file of my python program which in turn will download all the required dependencies (pip install ).
Please help me with it.
Try this using the pip module:
import pip
packages = ['numpy', 'pandas'] # etc (your packages)
for pckg in packages:
if hasattr(pip, 'main'):
pip.main(['install', pckg])
else:
pip._internal.main(['install', pckg])
an alternative would be:
import subprocess
import sys
packages = ['numpy', 'pandas'] # etc
for pckg in packages:
subprocess.call([sys.executable, "-m", "pip", "install", pckg])

"ImportError: No module named" but I already typed "pip install"

I'm a newbie and I'm trying to run some scripts in Python for my university classes.
I had to install MacPorts but I installed some Python versions before...
My script has this:
import sys
sys.path.append("/Users/matteo/Dropbox/tesi_magistrale/lmgc90_user/build")
import telepot
import time
from pprint import pprint
I used the sys.path.append because I need that folder if I want to use a software for my thesis.
When I run the script I have:
MacBook-Pro-di-Matteo-2:telegram matteo$ python bot.py
Traceback (most recent call last):
File "bot.py", line 15, in <module>
import telepot
ImportError: No module named telepot
But I typed:
MacBook-Pro-di-Matteo-2:telegram matteo$ pip install telepot
Requirement already satisfied: telepot in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/telepot-12.3-py2.7.egg
Requirement already satisfied: urllib3>=1.9.1 in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (from telepot)
If I type:
MacBook-Pro-di-Matteo-2:telegram matteo$ which python
/opt/local/bin/python
MacBook-Pro-di-Matteo-2:telegram matteo$ which -a python
/opt/local/bin/python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
/usr/local/bin/python
/usr/bin/python
MacBook-Pro-di-Matteo-2:telegram matteo$ which pip
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip
MacBook-Pro-di-Matteo-2:telegram matteo$ which -a pip
/Library/Frameworks/Python.framework/Versions/2.7/bin/pip
/usr/local/bin/pip
My .bash_profile has these lines:
# Setting PATH for Python 2.7
# The orginal version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
##
# Your previous /Users/matteo/.bash_profile file was backed up as /Users/matteo/.bash_profile.macports-saved_2017-09-12_at_16:05:41
##
# MacPorts Installer addition on 2017-09-12_at_16:05:41: adding an appropriate PATH variable for use with MacPorts.
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
# Finished adapting your PATH environment variable for use with MacPorts.
I use a Mac with Yosemite 10.10.4. I have this problem with "telepot" package and all the others, like panda3d, matplotlib etc.
Thank you!
Your pip using a different python executable then python command invoked
You could re-install pip with python get-pip.py, newly installed pip bin will be aligned with python executable in use, then install your packages, they should work.
But I suggest you clean up your local python installations and $PATH setting afterward.

Categories

Resources