I'm trying to get a Python package to install to my home directory because I don't have the privileges to install it system-wide.
The package is PyProj, and I am trying to install it using python setup.py install --home=~ (with Python 2.4.3), as recommended in the Python documentation. The package compiles successfully and copies itself to what I assume are the correct directories (the directory ~/lib64/python/pyproj appears during install).
But, when I load Python up and type import pyproj, I'm told ImportError: No module named pyproj.
Any thoughts on what might be going on?
You'll need to set PYTHONPATH to tell Python where to locate your locally installed packages.
For example:
[you#home]$ export PYTHONPATH="~/lib64/python"
Or, to do this within the interpreter (or script):
import sys, os
sys.path.append(os.path.expanduser("~/lib64/python"))
For more information on how Python locates installed modules, see section on The Module search Path in docs.
~/lib64/python/pyproj is not part of your PYTHONPATH. There are two or three ways around this, depending on your needs.
The first is to directly modify the path in your module, suitable if you're only going to use it from one module. As noted in the comments, this method does not do expansion on the '~' character.
import sys
sys.path.append('/home/username/lib64/python')
import pyproj
The second way is to add ~/lib64/python/pyproj to your system's PYTHONPATH, through whatever method your system suggests. A line in .bash_profile is shown below.
export PYTHONPATH=$PYTHONPATH:~/lib64/python/pyproj
See the Python Documentation for more details.
Related
SkyNet is publicly available as an open-source software at https://bitbucket.org/jlippuner/skynet. I am having problems installing this software. I am using Ubuntu, and I have downloaded all needed packages using apt. All tests pass (not at first, but after restarting), but the problems start when I try to run the example code.
As instructed at the bitbucket page, I have used CMake to try to install the package, and all files seem to be installed, and the tests work.
The first line in the code is from SkyNet import *, but this just returns No module named 'Skynet' found. I have tried to reinstall and using different versions of Python, but it doesn't seem to work. Can anyone help me?
Going through the Skynet documentation install page I found this,
For python to find the Skynet module the directory in which it exists has to be added to PYTHONPATH environment variable.
Replace install_dir with directory where the Skynet module has been installed to.
echo "export PYTHONPATH=<install_dir>/lib:\$PYTHONPATH" >> ~/.bashrc
An alternative approach would be to add the path where Skynet module exists to sys.path at the top of your script file.
Example
import sys
sys.path.append("/usr/lib/Skynet")
from Skynet import *
Here is a link to documentation on how python resolves module imports
I just install PyFoam using:
pip install PyFoam
Then I wrote a test.py file using:
import PyFoam
Now I got an error message:
import PyFoam
ImportError: No module named PyFoam
Then I tried install again, and I got this:
pip install PyFoam
Requirement already satisfied: PyFoam in ./.local/lib/python3.8/site-packages (2021.6)
Requirement already satisfied: numpy in ./.local/lib/python3.8/site-packages (from PyFoam) (1.23.0)
Could anyone help? Tks.
The issue is likely that you use one version of pip and another version of the Python interpreter. This is particularly common if you have several Python installations on your system. Check that these versions match with each other.
For example, run
python --version
pip --verion
You should see output similar to the following:
Python 3.10.2
pip 22.1.2 from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pip (python 3.10)
Notice the matching version numbers. I am verifying this on my system with Homebrew, under MacOS, but I assume that it's similar on other systems. Here, different instances of "python" and "pip" binaries are suffixed with the version number. So, if your version of "pip" says "3.8", try specifying "python3.8" as interpreter.
It might also be helpful for troubleshooting to know of the "-m" switch, in order to locate a module. In your example, you would run:
python -mPyFoam
Notice the capitalization for this module, which can differ between the one being used to install the module through pip and when importing it. On my system, as an example, it outputs:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: No module named PyFoam.__main__; 'PyFoam' is a package and cannot be directly executed
The error message is fine. It does still mean the module was found. That is in contrast to an output like the following:
/Library/Frameworks/Python.framework/Versions/3.10/bin/python3: No module named pyfoam
The -m switch is documented under https://peps.python.org/pep-0338/.
Another possibility might be that you need to check and configure your Python module search path, as described here https://docs.python.org/3/tutorial/modules.html.
When a module named spam is imported, the interpreter first searches
for a built-in module with that name. These module names are listed in
sys.builtin_module_names. If not found, it then searches for a file
named spam.py in a list of directories given by the variable sys.path.
sys.path is initialized from these locations:
The directory containing the input script (or the current directory
when no file is specified).
PYTHONPATH (a list of directory names, with the same syntax as the
shell variable PATH).
The installation-dependent default (by convention including a
site-packages directory, handled by the site module).
Note On file systems which support symlinks, the directory containing
the input script is calculated after the symlink is followed. In other
words the directory containing the symlink is not added to the module
search path. After initialization, Python programs can modify
sys.path. The directory containing the script being run is placed at
the beginning of the search path, ahead of the standard library path.
This means that scripts in that directory will be loaded instead of
modules of the same name in the library directory. This is an error
unless the replacement is intended. See section Standard Modules for
more information.
It's possible to try to think of other causes. But without any additional information about your setup it would probably be too speculative and become confusing for other people reading this answer, so I will leave it at these two causes, which I believe to be the most common for this type of issue.
I'm building vtk on Cent OS. I would like to install the python wrappers, and I've followed the build directions to do so.
I've installed under prefix ~/local. Things appear to have gone successfully:
vtk, vtkpython, and other vtk executables appear in my ~/local/bin/ directory. vtk libraries appear in ~/local/lib.
However, when I run python -c "import vtk", I see:
ImportError: No module named vtk
So apparently, something is not correctly pointing to something else, although I'm not sure what I'm missing since I've followed the build directions.
Here is the value of a particular shell variable that seems to show up on a related question:
echo $LD_LIBRARY_PATH
=> ~/local/lib
Check where is the python module. In my system (where VTK was not built following those instructions, but should not be too different) there is a folder in lib/python2.6/site-packages/vtk/ , which contains __init__.py and all the vtk classes (e.g vtkCommonCore.py , vktCommonCorePython.so ..).
If you find that folder, add its parent folder to the PYTHONPATH environment variable (or in your python distribution site-packages folder, create a file vtk.pth which contains that folder).
So, in my system, I have:
export PYTHONPATH=/mypath/INSTALL/lib/python2.6/site-packages/:$PYTHONPATH
See also http://www.vtk.org/Wiki/VTK/Tutorials/PythonEnvironmentSetup
I'm trying to use the lockfile module from PyPI. I do my development within Spyder. After installing the module from PyPI, I can't import it by doing import lockfile. I end up importing anaconda/lib/python2.7/site-packages/spyderlib/utils/external/lockfile.py instead. Spyder seems to want to have the spyderlib/utils/external directory at the beginning of sys.path, or at least none of the polite ways I can find to add my other paths get me in front of spyderlib/utils/external.
I'm using python2.7 but with from __future__ import absolute_import.
Here's what I've already tried:
Writing code that modifies sys.path before running import lockfile. This works, but it can't be the correct way of doing things.
Circumventing the normal mechanics of importing in Python using the imp module (I haven't gotten this to work yet, but I'm guessing it could be made to work)
Installing the package with something like pip install --install-option="--prefix=modules_with_name_collisions" package_name. I haven't gotten this to work yet either, but I'm guess it could be made to work. It looks like this option is intended to create an entirely separate lib tree, which is more than I need. Source
Using pip install --target=lockfile_from_pip. The files show up in the directory where I tell them to go, but import doesn't find them. And in fact pip uninstall can't find them either. I get Cannot uninstall requirement lockfile-from-pip, not installed and I guess I will just delete the directories and hope that's clean. Source
So what's the preferred way for me to get access to the PyPI lockfile module?
I want to import the paramiko module located in /usr/local/lib/python2.7/dist-packages. So, I imported it this way:
from usr.local.lib.python2.7.dist-packages import paramiko
I have an error syntax related to python2.7 (It considers 7 as a package located in python2 package)
I have both Python3.1.3 and Python2.7 installed. I program with Python3.1.3 only, however.
How can I resolve this problem ?
How about ?
import sys
sys.path.append('/usr/local/lib/python2.7/dist-packages')
import paramiko
UPDATED
The best solution is installing paramiko on Python3 env. Take a look at #DanielRoseman's answer. Or virtualenv is worth consideration. Here is a good tutorial. http://simononsoftware.com/virtualenv-tutorial/
I don't know why you think you need to include the full path. That directory will already be included in the Python path. You just need to do import paramiko.
Edit after comment Well you can't randomly import things that are installed for a different version. There are several backwards incompatibilities, and anything that has any compiled extensions will just not work at all.
You need to download and install paramiko for your 3.1 installation, rather than trying to use the 2.7 version. python3 pip install paramiko, as an example.
(Also, you shouldn't really be using 3.1. If you're using the Python 3 series you should upgrade to 3.4.)