Use Python libraries without installing them - python-2.7

I recently completed a school project in Python, and I used some non-included libraries like numpy and nltk.
The problem is I am required to demo the projects on school computers (not my own laptop) and the school computers run Python but don't allow me to install any additional packages (so no pip install numpy)
Is there any way I can include these libraries on my USB and help Python find them so my program can still run?

You can do this by downloading to folders and setting the PYTHONPATH before executing.
https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH
Also, to get the dependencies on there in a convenient manner (other than copying files etc.), you can give pip install options to specify a prefix while installing. See Install a Python package into a different directory using pip? for more details on this.

Related

How to install Rasa Stack in Windows 10?

Maybe someone else asked the same question too. But this question is difficult. I tried everything. The place I am stuck is with installing dependencies. Some of the dependencies are old and not easily available. But I managed to install them.
The problems lies here.. There are dependencies that need to get the build from their source code. I already installed Visual C++ Build and MSMPI. Also installed HDF5 for H5PY but it doesn't let me build old versions of H5PY. So, I tried installing the latest version of H5PY but still, I am stuck at errors like file not found. Some of the files which the build process cannot find are "h5py/h5f.pyx", "mpi_c", "mpi.h". Solving error for one missing file leads to other and so on..
On trying hard to solve such errors and installing one or the other package to do the same task, I am tired up.. Something I found, at last, was that "mpi_c" file was replaced with some other file in newer versions of MPI4PY. But my dependencies depend on older version. I tried installing an older version of MPI4PY but HDF5 won't let me install that giving other errors. At last, I quit the task with my whole day wasted after this.
So can someone here please provide a step by step guideline for installing Rasa Stack on Windows Machine?
Windows 10 with Python 3.7.. Let me know if I need to downgrade python as well.. It was my first time building some project from source with python on windows. Thanks...
Please try the below steps to install Rasa:
Install Conda
Create a virtual environment:
conda create -n myenv python=3.5
Activate the virtual environment
conda activate myenv
pip install rasa_nlu rasa_core

Python site-packages and corresponding interpreter

I used brew to install python 2.7 and 3.5 on Mac. SOMEHOW I have this site-packages directory /usr/local/lib/python2.7/site-packages.
But every python interpreter on the system points to every other site-packages directory EXCEPT this one. How do I use THIS site-packages directory?
(This is all because I need Vips. I'd installed this before, but now I'm using a different machine and I can't figure out how on Earth I got it to work before.)
The vips docs have a checklist and an explanation of what happens when Python tried to import vips:
http://www.vips.ecs.soton.ac.uk/supported/current/doc/html/libvips/using-from-python.html
But briefly you need to:
You have several Pythons installed, make sure you are invoking the brew one.
Make sure that that Python has a gi repository containing the Vips.py overrides.
Make sure you have a Vips-8.0.typelib file in /usr/local/lib/lib/girepository-1.0/.
SOLVED.
I didn't want to have to resort to resetting my dev box to factory settings, but I did. I hope this helps somebody...
If Python already exists on your system, and you're planning on installing Python yourself or with Homebrew (because you only get python2.7 out of the box), make sure you install the new Python first. Then put the install location first in your PATH. Then install your modules. In that order. I knew something was wrong, so I uninstalled Python/3 and Vips. But when I reinstalled them, for whatever reason Vips still didn't know to bind itself to the Python in /usr/local/Cellar. Even though I had /usr/local/Cellar first in PATH.
So to recap -- first install Homebrew, then set the PATH, then install python/python3, and finally install Vips. And you're good to go.

Pyomo can't locate GLPK solver

I'm trying to use the GLPK solver with Pyomo. I have a working model that's been tested, but keep getting an error saying GLPK can't be found.
WARNING: Could not locate the 'glpsol' executable, which is required for solver 'glpk'
I've installed glpk sucessfully. I also added the directory to my path variable so the executed can be called globally. I tested this with glpsol --help from my command line, and see the help info printed.
The below thread says it should be working, but alas, it is not.
How do you install glpk-solver along with pyomo in Winpython
Any ideas?
This answer is late but I want to share the solution that worked for me.
solvername='glpk'
solverpath_folder='C:\\glpk\\w64' #does not need to be directly on c drive
solverpath_exe='C:\\glpk\\w64\\glpsol' #does not need to be directly on c drive
I used to do this:
sys.path.append(solverpath_folder)
solver=SolverFactory(solvername)
This works for the cbc solver in coin-or but it does not work for glpk. Then I tried something different:
solver=SolverFactory(solvername,executable=solverpath_exe)
This worked for both cbc and glpk. No idea why this works (I really didn't do anything else).
Version: Python 2.7 or Python 3.7 (tested both), glpk 4.65
You can install glpk solver using this command -
brew install glpk
Installing the glpk package worked for me. As I use Anaconda:
conda install -c conda-forge glpk
This was after already including the 'glpsol' exectuable's folder path in my PATH variables.
So it looks like the set path variable is not handled by your Python installation.
A normal Python installation is set up for a seperated "PYTHONPATH" environment variable to look up additional modules.
There is also the option to make an entry in the windows registry or (like you already mentioned) move the files to the Python home directory, which is recognized relative to your installation directory if "PYTHONHOME" is not set.
More information in the Python Documentary under 3.3.3.
https://docs.python.org/2/using/windows.html#finding-modules
I was having the same issue. I don't know if this is a solution but it definitely got the solver working.
After downloading the Windows installation. I copied all the files in the w64 folder and pasted them directly into my Python working directory.
After that my python code could locate the solver.
NOTE: this was for Python 3.4.3.4, Windows 8.1 64 bit
Reading the source code here suggests you try:
from pyutilib.services import register_executable, registered_executable
register_executable(name='glpsol')
maybe will it give a clue
I had the same issue on Windows 10 and it was down to glpk being installed in a different conda environment. Full steps for installing pyomo and glpk below.
Test the installation by running 'Repeated Solves' example from https://pyomo.readthedocs.io/en/latest/working_models.html
Instructions (run at an anaconda prompt)
conda create --name myenv
conda activate myenv
conda install -c conda-forge pyomo
conda install -c conda-forge pyomo.extras
conda install -c conda-forge glpk
Run spyder from myenv so that if finds everything
spyder activate myenv
Here is the relevant part where pyomo 6.2 searches for the glpsol executable
https://github.com/Pyomo/pyomo/blob/568c6595a56570c6ea69c3ae3198b73b9f473abd/pyomo/common/fileutils.py#L288
def _path():
return (os.environ.get('PATH','') or os.defpath).split(os.pathsep)
There are two options to solve the PATH problem:
Putting the executable in an available folder in PATH (recommended practice). The glpsol executable must be in one of the folders present in the PATH system environment variable. Use in your code print(os.environ['PATH']) to see the available folders and put it there.
Adding the folder to PATH at runtime. You can add it to the system PATH statically or use code to add it dynamically (only while your script is running):
GLPK_FOLDER_PATH = "path/to/glpk"
os.environ["PATH"] += os.pathsep + str(GLPK_FOLDER_PATH)
In my case, my Python project has a virtual environment .venv, and I have an installation process that pastes the files essential to the glpsol executable when I install the project inside the .venv/Scripts folder. Because that folder is added automatically to the system PATH when Python is called from the virtual environment, libraries like Pyomo can find it. And I don't have to remember to add the folder to PATH at runtime whenever I want to use Pyomo.
For anyone that has the same issue, I found a workaround (not a solution!). I copied all the glpk files into my C:/Python27 directory, and (Surprise!) Python can now find them.
I'll hold out for a real solution before accepting this one.

where packages are stored in python (trying to install dragnet)

I've been struggling to install a python package called dragnet and its dawned on me just how little I understand about where packages are stored and how they are accessed in python. When I do import X in python, what steps does python take to try to import the packages?
I have anaconda installed, and it looks like many are stored in ~/anaconda/pkgs with names like "argcomplete-0.8.4-py27_0".
A bunch of other packages are in /Library/Python/2.7/site-packages. Sometimes conda install X doesnt work, but pip install will (conda pip is depreciated), and in these cases packages will often end up here. Both planout and dragnet are examples of such packages.
Dragnet, however, didn't want to import even despite its presence in /Library/Python/2.7/site-packages. When I go directly into the folder /Library/Python/2.7/site-packages in ipython it will import ok. I could not figure out where to put it or how to make it accessible when not in the folder though? I'd appreciate a pointer to a good tutorial or overview about packaging, package locations and importing in python.
In cases in which you are unable to install with anaconda, and you need to install another way, and that other way ends up with the install being not in the anaconda packages folder (lets say you end up with a new install in /Library/Python/2.7/site-packages), a reasonable work around is the following:
import sys
sys.path.append('/Library/Python/2.7/site-packages')
import desired_package_found_in_that_folder
This will force python to look in that folder for packages.
Instead of installing packages globally use Virtualenv to create virtual environments. This will help you isolate different projects and will also make dependency and package management much easier.
Read the docs here: https://virtualenv.pypa.io/en/latest/

Unable to import lib tiff after installing Anaconda on Mac OS X 10.9.2

I have developed some software using Python under Windows 7.
I have given it to a colleague to run on a Mac (OS X 10.9.2). I have never used a Mac and am having trouble helping them to get started. I have downloaded and installed Anaconda 1.9.2 on the Mac. According to the continuum documentation, libtiff is included, but when I run my python file using the Spyder IDE I get the following error when it tries to import libtiff:
ImportError: No module named libtiff.
Following one of the answers on Stack Ooverflow, I tried:
conda install libtiff
This runs and returns:
All requested packages already installed.
However on Windows 7 I can see a libtiff folder under \python27\lib\site-packages. On the Mac there is no libtiff folder under /lib/python2.7/site-packages.
Can anyone tell me what I am missing?
This question is answered here:
Installing Python modules with Anaconda or Canopy
If pip install libtiff does not work, you can download the source for PyLibTiff as directed at https://code.google.com/p/pylibtiff/source/checkout and run setup.py with whichever interpreter you would like for PyLibTiff to be installed to.
Also, you do not have to have the the C libraries that Anaconda installs installed for PyLibTiff to work if you have libtiff libraries installed elsewhere.
Unclear on this. But what you can do to begin with is to type echo $PATH from the terminal and see what paths are set. Unclear on how Anaconda interacts with the system, but a good hunch is that if the library file is not in a path then that would cause this.
Also, looking at this thread on Google Groups it seems that Anaconda installs it’s own libraries that might need to be symbolically linked into the main /usr/local/lib directory. The user Denis Engemann—who made the post—posts this bash script in the last response in the thread:
for lib in ~/anaconda/lib/*;
do
ln -s $lib /usr/local/lib/$(basename $lib);
done
I would recommend checking those two directories first before linking to make sure all is as expected.