Sharing Python file with non-standard modules? - python

Long time reader, first time OP.
I've written a program using 'Beautiful Soup' from the 'bs4' library and the 'requests' library (which is the most amazing thing, achieved in less than five minutes what I had failed to do in a week with urllib2), both of which I had to install (I'm using PyCharm CE).
If I'm sharing this file, what's the best way to make sure these modules are folded in with the package if I send it to someone? Or post it somewhere?
I'm running Python 3.5.2 in PyCharm CE
Thanks in advance!

You are probably asking about packaging and distributing python projects. That document describes how to package your project and upload it to PyPI.
If you only want to share a snippet of code, tell those who you are sending it to to install dependencies first via pip. Share the needed pip install <package> commands list.

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 control if libraries are installed on the system in 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/

How to install a Python package with documentation?

I'm tryin' to find a way to install a python package with its docs.
I have to use this on machines that have no connection to the internet and so online help is not a solution to me. Similar questions already posted here are telling that this is not possible. Do you see any way to make this easier as I'm currently doing this:
downloading the source archive
extracting the docs folder
running sphinx
launching the index file from a browser (firefox et al.)
Any ideas?
P.S. I'm very new to Python, so may be I'm missing something... And I'm using Windows (virtual) machines...
Edit:
I'm talking about two possible ways to install a package:
installing the package via easy_install (or any other to me unknown way) on a machine while I'm online, and then copying the changes to my installation to the target machine
downloading the source package (containing sphinx compatible docs) and installing the package on the target machine off-line
But in any case I do not know a way to install the package in a way that the supplied documentations are installed alltogether with module!
You might know that there exists a folder for the docs: <python-folder>/Doc which will contain only python278.chm after installation of Python 2.78 on Windows. So, I expect that this folder will also contain the docs for a newly installed package. This will avoid looking at docs for a different package version on the internet as well as my specific machine setup problems.
Most packages I'm currently using are supplied with documentation generated with sphinx, and their source package contains all the files necessary to generate the docs offline.
So what I'm looking for is some cli argument for a package installer like it's common for unix/linux based package managers. I did expect something like:
easy_install a_package --with-html-docs.
Here are some scenarios:
packages have documentation included within the zip/tar
packages have a -docs to download/install seperately
packages that have buildable documentation
packages that only have online documentation
packages with no documentation other than internal.
packages with no documentation anywhere.
The sneaky trick that you can use for options 1 & 3 is to download the package as a tar or zip and then use easy-install archive_name on the target machine this will install the package from the zip or tar file including (I believe) any documentation. You will find that there are dependencies that are unmet in some packages - those should give an error on the easy install mentioning what is missing - you will need to get those and use the same trick.
A couple of things that are very handy - virtual-env will let you have a library free version of python running so you can get the requirements and pip -d <dir> which will download without installing storing your packages in dir.
You should be able to use the same trick for option 2.
With packages that only have on-line documentation you could look to see if there is a downloadable version or could scrape the web pages and use a tool like pandoc to convert to something useful.
In the 5 scenario I would suggest raising a ticket on the package stating that lack of accessible documentation makes it virtually unusable and running sphinx on it.
In scenario 6 I suggest raising the ticket but missing out virtually and avoiding the use of that package on the basis that if it has no documentation it probably has a lot of other problems as well - if you are a package author feeling slandered reading this then you should be feeling ashamed instead.
Mirror/Cache PyPi
Another possibly is to have a linux box, or VM, initially outside of your firewall, running a cached or mirroring service e.g. pipyserver, install the required packages through it to populate the cache and then move it, (or its cache to another pip server), inside the firewall and you can then use pip with the documented settings to do all your installs inside the firewall. See also the answer here.

Python Libraries

I'm in desperate need of a cross platform framework as I have vast numbers of .NET products that I'm trying to port to Linux. I have started to work with Python/pyQt and the standard library and all was going well until I try to import non-standard libraries. I'm hearing about pip and easy_install and I'm completely confused about this.
My products need to ship with everything required to execute them, so in the .NET world I simply package my DLLs (or licensed DLLs) with my product.
As a test bed I'm trying to import this library called requests: https://github.com/kennethreitz/requests
I've got an __init__.py file and the library source in my program directory but it isn't working. Please tell me that there is a simple way to include libraries without needing any kind of extra package installer.
I would suggest you start by familiarizing yourself with python packages (see the distutils docs. Pip is simply a manager that install packages directly from the internet repository, so that you don't need to manually go and download them. So for, example, as stated under "Installing" on the requests homepage, you simply run pip install requests in a terminal, without manually downloading anything.
Packaging your product is a different story, and the way you do it depends on the target system. On windows, the easiest might be to create an installer using NSIS which will install all dependencies. You might also want to use cx-freeze to pull all the dependencies (including the python interpreter) into a single package.
On linux, many of the dependencies will already be including in most distributions. so you should just list them as requirements when creating your package (e.g. deb for ubuntu). Other dependencies might not be included in the distro's repo, but you can still list them as requirements in setup.py.
I can't really comment on Mac, since I've never used python on one, but I think that it would be similar to the linux approach.

Installing Libraries on a Server

I'm fairly noob at the using the terminal and doing server administration. I recently "inherited" a Twitter app, and I need to install a Python OAuth library:
http://dev.twitter.com/pages/oauth_libraries#python
Unfortunately, I'm pretty much clueless about how to:
download a library to the server
installing the library on the server so I can "import "
Can someone please walk me through this process? Or, provide me with resources that will?
Thanks!!
The easiest solution is probably to run 'easy_install' as root using the same python that runs the application (there may be different versions of python). You may need to install a package such as 'setuptools' first, or download and run the easy_install-installer as follows:
# wget http://peak.telecommunity.com/dist/ez_setup.py
# python ./ez_setup.py
But again, only do this if you don't have easy_install
The 'best' way to do it depends on how you run your application (mod_python, cgi or wsgi). With WSGI the best way would be to have a seperate virtualenv for each site in which you install things for that site only. This way you can have different versions of the same library installed which is also a big plus for development but even more so for websites where you might want to keep one website on the older version of a lib and another on the newer version.
A few links to get you started:
WSGI + virtualenv,
virtualenv
Any question you can add as a comment.

Categories

Resources