I have a question regarding python modules.
For example, I have a script which uses netaddr/paramiko module and now I want to start the script on another linux os where I have no root access. Pip is not installed and the user has no homedir. Virtuelenv is no option.
So is there a possibility/way to 'make' python modules 'portable' and add it to a folder where I can load it in my script??
You could use pip install --no-install to list the dependencies of the library you need, then copy the modules / packages of all of these libs in your project directory, for example in an /ext subdirectory.
Related
Can't seem to get imports to work. I've installed using
pip install pyperclip
I can can confirm that it was successfully installed:
But then when attempt to confirm in in the Shell:
Is there another step to importing that I'm just missing?
Your problem is that pip is installing for the global (all users) version of python, and you're using a version of python installed for only your user c:\Users\bbarker\AppData\Local\Programs\Python\Python36. You'll want to either use the global install instead c:\program files (x86)\python36-32 or change your pip defaults as described here.
You'll notice the folder where pip told you where pyperclip was installed does not show up in sys.path. Therefore python does not know to search there for libraries. Those few file paths you did have in your sys.path are automatically generated defaults that are relative to the install directory of the particular instance of python you're currently using. If you use the instance in your \program files (x86)\ folder, the paths will be relative to that folder instead
tldr;
You have 2 instances of python installed, and you're installing libraries to one and using the other.
I have just started to use python (within Windows, 64bit) - and I have a basic question on how to install external packages within the anaconda / spyder environment. I understand that for most packages one can simply use “conda install bunnies”. However, certain packages are not in the anaconda repository, and might have be installed externally (e.g. from github). For those packages, in order to have spyder to recognize this package – does one only in addition have to update the PYTHONPATH manager in Spyder to include the directory (e.g. c:\users\bunnies) in which one has downloaded this package? Or should one take additional steps / is there a faster way?
You have several options to use packages that are not (yet) available via conda install:
1.) If the respective package is on PyPi you can build it as described in the manual.
2.) If building from scratch doesn't work and the package is on PyPi you can also try an installation via pip. Not that you have to use the pip in your Anaconda distribution and not the one of your systems Python installation.
3.) If you want to include external packages or local folders that contain Python-scripts you can do the following.
3.1.) Use the sys module and append the required package/folder to the path:
import sys
sys.path.append(r'/path/to/my/package')
3.2) Or put the modules into into site-packages, i.e. the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages which is always on sys.path. (Source)
3.3) Or add a .pth file to the directory $HOME/path/to/anaconda/lib/pythonX.X/site-packages. This can be named anything (it just must end with .pth). A .pth file is just a newline-separated listing of the full path-names of directories that will be added to your path on Python startup. (Source)
Good luck!
is there any method to create standalone .pyc program that will contain all modules inside, I just don't want to install all modules on every computer where I want to run this program. If this is not possible what else can I do?
You could install python packages locally inside your project, using command:
pip install -t <destination_folder> <package_name>
For example:
pip install -t . mock
Will install mock library into current directory. Then, when you do import mock in the files from that folder, you will be given local file.
You could also install all packages into subfolder of your project called lib or similarly, and than before you import that package call:
import sys; sys.path.insert(0, path_to_lib_folder)
You need to create virtual python environment.
There are two ways:
VirtualENV. It creates virtual environment. So you can install python modules in it and just copy to another server.
(RECOMMENDED) Buildout. It also creates virtual environment. However you don't need to install all things and update every time you need. You just need to write simple buildout config and buildout install everything for you and keeps it up to date. Also buildout can install software which may be non-Python-based, for example some kind of database and so on. And everything will be installed locally in virtual environment.
import nest gives the 'no module named nest' error when it is in the $PATH, which means that there is a /opt/nest/lib/python2.7/site-packages: in my system $PATH. At this location there is a directory named nest and the structure inside the nest directory is looks like:
, where there is an __init__.py obviously. So why can't python find nest?
more information:
I am sure that I installed nest with python2.7, and run it with the same python2.7.
According to the docs, there are several ways to install python packages:
using distutils - running python setup.py install installs the package to site-packages of your current distribution;
passing --user to setup.py install installs module to ~/.local/lib/python2.7/site-packages on Unix, and this directory in under normal conditions always included in sys.path;
passing --home=$HOME to setup.py install installs module under $HOME directory. This directory should be included to sys.path explicitly;
modifying python search path:
you can do either
import sys
sys.path.append('/opt/nest/lib/python2.7/site-packages')
in the beginning of your script; or your can add
PYTHONPATH=/opt/nest/lib/python2.7/site-packages
export PYTHONPATH
in the end of your ~/.bash_profile file.
UPDATE:
Just tried to install nest and found that it comes in two flavours - 32bit (under /opt/nest/lib) and 64bit (under /opt/nest/lib64). You might have tried to use 32-bit python package with 64-bit python distribution. Try to change the string in ./zshrc to
PYTHONPATH=/opt/nest/lib64/python2.7/site-packages and see if it works. It works for me at least.
To set relevant environment variables correctly, NEST installs have a config that you can just source
source <installpath>/bin/nest_vars.sh
That sets the PATH and PYTHONPATH correctly and points NEST to the right directories for dynamically loaded content (modules, help pages, …)
So I decided to install python packages (technically Django apps) directly from the downloaded tar file, by extracting it and using the following command:
python setup.py install
However, inside my site-packages directory, I find that the package was installed inside a .egg directory that also has version numbers. The directories look annoyingly like this:
site-packages/django_cms-2.1.3-py2.7.egg/cms
site-packages/django_cms-2.1.3-py2.7.egg/mptt
I need the packages to install as a directory with the package name with no .egg or version number, otherwise, Django can't find the package. It should be like this:
site-packages/cms
site-packages/mptt
Attempting to install the same package from pip, and it works fine. This is frustrating, so some help would be appreciated.
I tracked down a thread that discusses something similar, but it didn't give a solution that worked.
The Django project is unable to locate the packages I installed because the packages aren't installed at the root of site-packages dir. Instead, it resides inside the .egg directories. What I had to do is manually move the package to the root, but how do you correctly install python packages?
Also, I didn't suppress the easy-install.pth file, but it isn't picked up by PyDev.