python3 execute script without dependency - python-3.x

My python3 script currently requires pypdf2 for execution.
If I want to run it on another device I would have to install python3, pip and pypdf2.
Is there a way to include pypdf2 into my script so that I only need python itself to run my script?
EDIT:
The script runs on windows (10)

If you want to distribute your script you can do this using PyInstaller as a stand-alone application.
This is a python module that bundles the interpreter with all the required libraries.

Related

how to list all the packages required for a program in python?

can i list the python packages that are actually required for running exiting python program running in linux.
I tried running following commands.
pip3 freeze
pip3 list
To find the modules used by a single python script, you can try to use ModuleFinder:
Create a new python script to analyze the modules your script is using:
New Script:
from modulefinder import ModuleFinder
finder = ModuleFinder()
finder.run_script('MultiProcess.py')
print('Loaded modules:')
for name, mod in finder.modules.items():
print(('%s: ' % name))
print((','.join(list(mod.globalnames.keys())[:3])))
print(('-'*50))
print('Modules not imported:')
print(('\n'.join(iter(finder.badmodules.keys()))))
The output is very verbose and detailed
reference:
https://docs.python.org/2/library/modulefinder.html

Python script to launch pymol on windows

I have installed the opensource version of pymol on windows (https://github.com/schrodinger/pymol-open-source).
To run pymol, I can open a python3.8 command prompt on windows and type the following:
import pymol
pymol.finish_launching()
This launches the pymol gui.
I'd now like to create a simple python script so that I can click the script (or convert to a windows executible using for example pyinstaller), however if I put the above commands in a script pymol.py and then run with:
python pymol.py
where python points to python3.8 I get the following error:
AttributeError: partially initialize module 'pymol' has no attribute 'finish_launching' most likely due to a circular import.
Why does this work on the command line python but not in a script and how can I fix this so the script runs and can be converted to a windows executible?

How can I list all non-standard modules used by a Python program?

As some pundits say that the Python standard library is listed in the Python core documentation for your version, it is built-in by default, you don't have to install it separately from Python itself. For example, math is a standard module, you needn't install it with pip install math.
Non-standard modules are not built-in, you have to install them before you use them in a Python program. For example, lxml is a non-standard module. If not installed, a "no module named lxml" error pops up when you import lxml in the Python shell.
It is time to turn to the topic now, how can I list all non-standard modules used by a Python program?
test.py is a Python program, it is executed with python test.py. How many non-standard modules are called when python test.py is run?
pip freeze shows all installed modules, but some of them are not called by python test.py.
I was wondering the same thing, because I wanted to set up a conda environment for my project by a method other than trial-and-error. What I found worked was to make a bare environment with just python and pylint in it, using, e.g.,
conda create -n myenv python=3 pylint
and then from within that environment run:
pylint /path/to/module --disable=all --enable=import-error
This will nicely list, by file, all of the non-standard imports.

Getting python2.7 path in django app for subprocess call

I am using linux. I am trying to run daemon from function in django views. I want to run shell command from a view in Djangp app. I am using python 2.7. Command needs python2.7 path.
My app will be like plug n play. So on system on which it is going to install may have python installed on different location. So I want to make python path dynamic.
Command will be
usr/bin/python2.7 filename.py --start
On my system path is usr/bin/python2.7.
I found follwing using os.
On python shell I tried following code & I get what I want
import os
getPyPath = os.popen('which python2.7', 'r')
pyPath = getPyPath.read()
pyPath.rstrip()
I got o/p as which is expected as below
usr/bin/python2.7
So now how to get this code is django app view function & run it so that I can get python path in a variable.
I found pythons subprocess module call using which we can run command through shell using shell=True.
So can I get above code running in django view function using subprocess call??
If not what is the other ways to get python path in variable in function django views.
Thanks in advance.
To view the full path to the current Python interpreter, use sys.executable
import sys
print(sys.executable)

How can I make my script choose the right python interperter?

I have both 2.7 and 3.0 versions of the Python interpreter installed (on my Ubuntu 32 system), but one particular script uses 3.0.
Using
#!/usr/bin/python3 -B
will not work when the program is run with python myprogram.py.
And I also need a solution that works also in Windows where I also have both python versions installed.
How can I make the script to run only with the right python version?
Please use virtualenv, which makes isolated Python environments easy.
python = Python to use. # This has to be the absolute path to Python executable
os.execl(python, python, * sys.argv)
This way you can restart the script with the python you want to use. Not really stylish.
I don't know why you can't just launch the program with python3 foo.py, but it's possible to have a python2 program relaunch itself as python3 with something like this.
import sys
if sys.version_info.major != 3:
import os
# replace this process with a python3 process
os.execlp("python3", "python3", *sys.argv)
It's a bad solution though, because now your python3 program can't use anything that's not valid python2 syntax
Please take a look at The wrong python interpreter is called
You have to choose a correct interpreter based on where you installed the desired version of Python and your system variables.

Resources