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])
Related
Hello I'm trying to add a custom step to my Python3 package. What I wish for, is to execute a make command in a root directory of my Python package. However, when I install my package with pip3 install ., the CWD (current working directory) changes to /tmp/pip-req-build-<something>
Here is what I have:
from setuptools import setup, find_packages
from setuptools.command.develop import develop
from setuptools.command.install import install
import subprocess, os
class CustomInstall(install):
def run(self):
#subprocess.run(['make', '-C', 'c_library/']) # this doesn't work as c_library/ doesn't exist in the changed path
subprocess.run('./getcwd.sh')
install.run(self)
setup(
cmdclass={
'install': CustomInstall
},
name='my-py-package',
version='0.0.1',
....
)
Now, what's interesting to me is that the getcwd.sh gets executed, and inside of it I have this. It also prints the TMP location.
#!/bin/bash
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
echo $SCRIPTPATH > ~/Desktop/my.log
Is there a way to get the path from where the pip install . was run?
Python 3.8.5, Ubuntu 20.04, Pip3 pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.8)
I'm trying to install automatically each library in python if it isn't installed.
#!/usr/bin/python
# -*- coding: utf-8 -*-
from pip._internal import main
pkgs = ['Bio==0.1.0','argparse==1.4.0']
for package in pkgs:
try:
import package
except ImportError:
main(['install', package])
But, this error occurs:
Collecting Bio==0.1.0
Using cached https://files.pythonhosted.org/packages/14/c2/43663d53b93ef7b4d42cd3550631552887f36db02c0150d803c69ba636a6/bio-0.1.0-py2.py3-none-any.whl
Installing collected packages: Bio
Successfully installed Bio-0.1.0
Collecting argparse==1.4.0
Using cached https://files.pythonhosted.org/packages/f2/94/3af39d34be01a24a6e65433d19e107099374224905f1e0cc6bbe1fd22a2f/argparse-1.4.0-py2.py3-none-any.whl
ERROR: Could not install packages due to an EnvironmentError: [Errno2] No such file or directory: '/tmp/pip-req-tracker-6sqtap8q/139713c65f8ac559a034717470dc5a6e30a6db86bdc3d69fe2bc0e63'
I notice that this occur always after the first library installation, e.g.: If I change ['Bio','argparse'] for ['argparse','Bio'], then argparse will be installed, but Bio will not.
Do not do this.
Instead:
either actually package your Python project properly with accurate dependencies (look up setuptools, poetry, flit, or any other similar project);
or instruct your users to install your project's dependencies themselves, you can facilitate this by providing a pip-compatible requirements.txt file.
Additional note here is how to "Using pip from your program".
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.
while i am using "import watchdog" in python...its showing no module named watchdog....im working in linux (centos)
directory of watchdog module-----'/home/admin/watchdog'
i have tried all the following code
1)
import os
import sys
env=os.path.expanduser(os.path.expandvars('/home/admin/watchdog/src/watchdog/event'))
sys.path.insert(0, env)
import home.admin.watchdog.src.watchdog.event
2)
import sys
sys.path.append('/home/admin/watchdog/src/watchdog/event/')
3)
from home.admin.watchdog.observers import Observer
from home.admin.watchdog.src.watchdog.events import FileSystemEventHandler
4)
PYTHONPATH="${PYTHONPATH}:/home/admin/watchdog/src/watchdog/event/"
export PYTHONPATH
Use PyCharm to write code in python
It helps to solve module errors by itself
It will show hint button when there is an error. So if we press that it will show how to fix or fix it by itsown
You should install watchdog using pip (or pip3) with pip install watchdog (or pip3 install watchdog). This way pip will take care of everything and then you can import it with import watchdog.
Since you have the source you can also go to the base directory and execute pip install -e . or python setup.py install which will both install it such that you can import watchdog with import watchdog.
Finally the proper directory you should include in PYTHONPATH should actually be /home/admin/watchdog/src/.
import sys
sys.path.append('/home/admin/watchdog/src/')
import watchdog.event
UPDATE
This is a bug in the setup.py of the package
Attempting to install certain packages using pip and I get this error:
pip install saspy
command python setup.py egg_info failed with error code 1
Upon reading the traceback I see that it failed while attempting to import saspy.This is an excerpt from the setup.py which is indeed attempting to import from saspy, while installing saspy. How is this supposed to work? I am using setuptools 36.0.1, pip 9.0.1 and (long story) python 2.7.8.
try:
from setuptools import setup
except ImportError:
from distutils.core import setup
from saspy import __version__
with open('README.md') as f:
readme = f.read()
This is a buggy setup.py. It's a common mistake, since with a source installation, you can import the package from the unpacked source tree before it's been installed.
saspy requires Python3. I would expect this issue is due to using Python2 to try to install it. Though I have never seen that error before when installing it.