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, …)
Related
Typically when using rubygems in the ruby ecosystem or npm/yarn in the JavaScript ecosystem packages are installed somewhere on the $PATH or at least you are instructed to add the package install location to your path $PATH.
It seems like with pip in the python ecosystem there is never an emphasis on this. Instead, you are encouraged to run modules via python -m <name>, etc.
Seems a bit odd to me, was this just a design decision? Is it good practice to put site-packages or whatever location pip is using into $PATH? Sometimes a binary is added to /usr/local/bin (and sometimes with a different name than the package itself e.g django-admin instead of django with pip install Django whereas the binary is usually the same as the package name in ruby/JavaScript ecosystems), for example, I see, but is that all of the time?
You're confusing $PATH and $PYTHONPATH. site-packages is for libraries, $PATH is for programs (binaries and scripts). pip installs libraries into its site-packages and scripts to the corresponding bin/ directory; e.g. if site-packages is /usr/local/lib/pythonX.Y/site-packages scripts are installed into /usr/local/bin/.
pip doesn't check if /usr/local/bin/is in $PATH. Well, I agree with you — it should check and remind user to add bin/ directory to $PATH if it's not already there.
The site-packages directory for a particular python installation is automatically added to sys.path when you run the binary for the installation. When that binary executes import modname, it looks in the directories on sys.path. So when you run pip install with a particular python binary, pip, by default, puts the package in the site-packages for that binary so that binary can import the package. Advanced used can do more complicated things.
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've installed pip on my computer(mac), and I tried these:
$export PYTHONPATH=/usr/local/lib/python2.7/site-packages/pip
$python pip freeze list
/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file 'pip': [Errno 2] No such file or directory
It doesn't work, but if I specify this full path into python command, it works:
$python /usr/local/lib/python2.7/site-packages/pip freeze list
ant==0.1.0
appnope==0.1.0
astroid==1.4.8
backports-abc==0.5
........
Why is that?
Running pip freeze directly should be sufficient. You shouldn't have to run it via python pip or tweak $PYTHONPATH at all.
The error in that first snippet has to do with how you are invoking python. Your command is interpreted as python <script-filename> [script-arguments ...]. The filename you are passing in is pip, so python looks for a file named "pip" in the current directory. That file does not exist, so python crashes with a "file not found" error.
python <full-path-to-script> works because... well, why wouldn't it? Python finds the script and executes it.
As pointed out in the comments you don't want to mess around with PYTHONPATH. If you want to have different versions of python on the same computer or python installations with different installed packages (or modules) what you need is to use virtualenv
Create a new virtualenv.
virtualenv /usr/local/custom-python/
and then whenever you want to use it or install packages into it, just do
source /usr/local/custom-python/bin/activate
try to use:
$export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages
Firstly, your code may cover the orignal PYTHONPATH which is deprecated. Secondly, do not include pip into the python path because pip is a package that should be included.
python /your/path/to/pip this pip is a folder in which there is a __init__.py . So python can read it. But if you directly define PYTHONPATH to pip folder, python will not find this __init__.py to represent pip (See document about python import packge)
Besides, I think you can include the binary pip (may located /usr/bin) in into your path so that you can call it directly with $ pip command
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!
I am using python2.7 and trying to import modules such as psycopg2. But I get the following error when I try to import the module:
import psycopg2
ImportError: No module named psycopg2
When I try pip to install the module it gives me the following message:
Requirement already satisfied (use --upgrade to upgrade): psycopg2 in /usr/local/lib/python2.7/dist-packages
Cleaning up...
Can anyone please tell me what I am doing wrong?
Is the module installed in your PYTHONPATH?
You can verify running this command line:
python -c "import sys; print '/usr/local/lib/python2.7/dist-packages' in sys.path"
Try to put psycopg2 module (or package, i don't know psycopg2) in the same directory of your script, and try to import it. Import searches first in the current directory.
import sys
print sys.path
Should display which are the search directories for the python interpreter, in order from the first to the last. The first is always the current directory, then there are the directories in PYTHONPATH and then python setup-dependent directories.
See:
https://docs.python.org/2.7/tutorial/modules.html#the-module-search-path
You can edit sys.path in order to reach your module, or put the module in one of its directories.
Check where you installed the package, for me it was into the python 32 bit folder c:\program files (x86)\python37-32\lib\site-packages.
The problem I was running VsCode in x64 bit mode and the packages live in the x86 folder.
See here how you can change the interpreter you're using - in my case - I needed to set it to Python 3.7.4(x86) 32 bit (image off internet):
Make sure that your PYTHONPATH and/or PYTHONHOME variables are set properly. These environment/command line variables get searched when Python looks for modules to import. So, if the module is properly installed, you should make sure a reference that location is in one of those variables.
Check out these links PYTHONHOME and PYTHONPATH
Make sure that you are running your program in same python version in which you have installed package
For example,you have installed package in python3 and you are running the code with python2..that might be the case to give the error