how to control if libraries are installed on the system in python - python

is there a way to create a function that check on the system if the python modules that are needed by the main program are installed and eventually install them automatically?
I searched a lot for something like this but i didn't found anything useful.
Thanks and sorry for the bad english.

The tool you are likely looking for is pip, it's not run as a part of a script but rather is used to install a script.
https://pip.pypa.io/en/stable/reference/
In addition if you wish to develop a script to install your script you can find documentation
http://marthall.github.io/blog/how-to-package-a-python-app/

Related

Friend doesn't has Python module installed is there a "workaround"?

I send mir friend a Python script with the module pytube.
But he hasn't installed the package so he couldn't run the script. It was not the big of a deal because easily installed pytube. But how can I prevent this? So if the script/program had more than one module but my friend doesn't want to install them all how can i "compile" the program that he doesn't need to install the modules?
I heard about Docker is this the right way or is there another way?
You can package your code or use docker indeed. Although, this costs more time then you want to spend on it probably.

How to install python developer package on Windows?

I ran into the same problem as here. So I tried to install python developer package but I didn't find the way to do this.
Cython's helloworld instruction doesn't contain any info about it, which seems strange and stupid to me to not include basic requirements into instruction, named "Basic Tutorial" (Sorry if I don't get something about it.)
Anyway... I'm using Windows 10, PyCharm, virtual environment with python 3. When I went to package install window, search result contained 5 packages:
So, which one?
I tried to install one, but it gives me an error:
I met this error in the past with some other package already. PyCharm's suggestion is to run that command from terminal. But from my experience that never helped. Usually solution was either to change name (because some names are right for Python 2.x, other for Python 3.x), or to download the package (egg/wheel) manually and install it offline.
This question was asked before already, but that one is for linux, which is not the case for me.
Can someone save some time for me and future generations, please? ;)
How do I install it and make Cython's helloworld example work?

how to properly check if a module is installed, and if not attempt to install it

I am working in Python 3.6+ and want to check if a few different modules are installed from within my script. If not, I want to attempt to install them with a few caveats:
1) the proper way to do this, if I remember reading it correctly, is to look into 'packaging and versioning' .. possibly with setuptools .. im not really sure. There is a Digital ocean page that is hard to follow. It discusses this but I keep running into an issue with documents around this topic: they are all based around the assumption that the project will be uploaded to pypip for use with pip. I specifically do not want this. I want to distribute this directly to individuals, by hand. Maybe in the future have it available in a closed, not-open-to-everyone github repo.
Currently in my script I'm using a try and except. Try to import these modules, if they don't exist i run this exception which i don't know if it works.
except ImportError:
from pip._internal import main as pip
pip(['install', colorama])
import colorama
print('colorama imported successfully')
and for what its worth - i have no idea what pip(['install', colorama]) is doing.
The packaging aspect seems to include imported modules with your code. How does one preform this function? Instead of checking if colorama is installed and then attempting to launch a subprocess to install it .. how do i just include the entire thing assuming this is the 'right' way to do this?
One thing that's usually done to avoid this problem is to build your program in a virtual environment which you know to contain the correct python scripts - and then either
package the entire virtual environment with your project as a unit, or
write a requirements.txt file that lists all the packages (and versions) that are expected to be installed before the user runs the program (you'd install everything on that list by doing pip install -r requirements.txt on the command line before running the program with python script_name.py)
Ideally, you'd then have your script fail if the required dependencies aren't there, and have the user install them manually to fix the issue.
Here's python 3's documentation on virtual environments
What you're doing now is unconventional - if it's working, it's working, but it's not great practice. Biggest reason for that is that your script, in its current state, is installing software on the user's machine without their consent, i.e. the user did not tell the program to install this software and was not told that the software was necessary, but the program is installing it anyway. In your case this may be harmless, but in general it's something to stay away from because it can get into really shady territory.

How to install wxPython using virtualenv

I am trying to begin a new python GUI application and I have decided to use wxPython as GUI because I want a multi-platform one.
The problem is that I want to use virtualenv ( with virtualenvwrapper ) to isolate the environment and be able to reproduce it in other machines where I will work, but i cannot install wxPython.
I have it installed in my ubuntu machine via apt-get but that is not enough
I have searched the web for a solution and i have found ...
This page http://batok.github.com/virtualenvwxp/ where it is explained a way to hack the virtualenv environment to use the local installation of wxPython. Not the best solution, but it would be a good workaround. The problem is that it is explained for Mac, and I couldnt make it work in my ubuntu.
Also found this page Installing wxPython in virtualenv under Linux where someone ask something similar. I have tried to build wxPython that way with no success.
Any help would be appreciated.
In the end, I have choose wxPython beacuse it is multiplatform and i can use it without license problems, but as i have not started yet i can change my mind if there is another easier to install framework.
Thanks In advance
20110925: Sorry for the delay and thanks for the answers.
I just have tried to install wxpython using buildout and the links given here, but i still have the same problem. It seems as if I need libgtk2.0-dev package to be able to compile wxpython...
So there is any way to install this package locally to the buildout environment?
Thanks again.
At the end I could not resolve this problem.
I want to create a reproducible python environment with all the requirements inside using buildout and/or virtualenv so I could work in any linux system with just virtualenv, python and a C++ compiler installed.
It seems that the only way to do this is to use buildout cmmi recipes to download and build wxpython and ALL its dependencies. This is a really painful way, and I have no time now.
I have decided to use a workaround: I am going to work on my ubuntu laptop most of the time, so I have installed wxpython from the repositories and use a wx.pth file to make it available to the virtual environment.
This is not a good solution, but seems the best till now ... so if someone knows any better solution please let me know.
When my python project is more mature, I will turn again to this problem and I will probably try the hard way ...
Thanks for all your answers and comments.
The solution I ended up using was to install python to my main system:
Then make a symbolic link from the wx in my system python to my virtual environment:
ln -s /usr/lib/python2.7/dist-packages/wxversion.py <virtual_env_path>/lib/python2.7/site-packages/wxversion.py
Where is the path in my case to a virtual environment named "fibersim" for example is:
/home/adam/anaconda/envs/fibersim
Then import wx worked
Got this from: http://qopml.org/wp-content/uploads/2013/01/README.txt
Buildout allows your to install different parts whose recipe code determines how that part is built. There are cmmi recipes for building stuff with Configure/Make/Make-Install (CMMI). You can use this to build wxPython locally to the buildout and then create a python interpreter that has that build of wxPython and your own eggs in it's path.
See this blog post and this answer for details.
Keep in mind that zc.recipe.egg will also install any setuptools/distribute console_scripts in the buildout's bin directory as well. See also mr.developer for automatically checking out multiple packages from VCS and working on them in the same buildout.

Python: Handling modules

I wrote a program which uses a number of built in modules. The program is meant to be used by different persons on their systems. They dont have enough knowledge in python to install it when their system doesnt have the module needed to run the program. Is there any way of handling that.
Also I want to package the program as an executable in linux. It contains 3 py files and one text file only.
I think what you need is to create a debian package that handles the dependencies and the installation process.
I'm an Ubuntu user but this Complete Ubuntu Packaging Guide should help you get started. Good luck!
You can create an executable that contains your python modules and the python interpreter. You can use PyInstaller for creating such an executable.
I think the easiest way to achieve this on a debian distribution is to package your python application in a debian package. You can use this module to make life easier.

Categories

Resources