I downloaded an open source library that I am making adjustments to, and I am trying to import my local version like from ... import my_library but getting ImportError: attempted relative import with no known parent package
When I try just import my_library I get the version that I installed via pip
% pip install -e /Users/sam/Documents/library
Obtaining file:///Users/sam/Documents/library
ERROR: File "setup.py" not found for legacy project file:///Users/sam/Documents/library
% pip install -e library
ERROR: library is not a valid editable requirement. It should either be a path to a local
project or a VCS URL (beginning with bzr+http, bzr+https, bzr+ssh, bzr+sftp, bzr+ftp,
bzr+lp, bzr+file, git+http, git+https, git+ssh, git+git, git+file, hg+file, hg+http,
hg+https, hg+ssh, hg+static-http, svn+ssh, svn+http, svn+https, svn+svn, svn+file).
i uninstalled the pip module and am trying
sys.path.insert(
0, "/Users/sam/Documents/dev/trading/ccxt/python/ccxt/__init__.py")
ccxt = importlib.import_module("ccxt") # Equivalent to "import package"
but am getting
Traceback (most recent call last):
File "/Users/sam/Documents/dev/trading/ccxt-scratch/main.py", line 4, in <module>
from Exchange import Exchange
File "/Users/sam/Documents/dev/trading/ccxt-scratch/Exchange.py", line 14, in <module>
ccxt = importlib.import_module("ccxt") # Equivalent to "import package"
File "/usr/local/Cellar/python#3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'ccxt'
The best way to install a library from source files that you are modifying is to use pip's editable mode.
pip install -e /path/to/the/library
This way the library will be loaded from the source files ad any changes to it will reflect in your virtual environment (if you are using one).
Related
I have just uploaded a Python package into pip called hqc:
https://pypi.org/project/hqc/0.0.1/
then successfully pip installed the package using:
pip install hqc
but I can't seem to import the package in Python Interpreter. When I do this, I get the following error message:
>>> import hqc
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import hqc
ModuleNotFoundError: No module named 'hqc'
What am I missing? Do I need to set the environment variable PATH or is it something else?
In the folder where setup.py is, make a folder called hqc and put an empty file called __init__.py (thats two underscores on each side). Right now, there's no file to import so python fails when importing it.
hqc-0.0.1:
hqc:
You can test the package by going to the directory where setup.py is and running
pip install .
Before trying again, be sure to uninstall
pip uninstall hqc
I had a quick look at your github repository and found that the package name, i.e. your folder name is skltemplate.
So, your import will be: (verified it to be working)
import skltemplate
And found that skltemplate package exposes the following modeules:
TemplateClassifier, TemplateTransformer and TemplateEstimator.
You can import the above by doing:
from skltemplate import TemplateClassifier
Now, if you want your package to be named as hqc, you need to rename skltemplate directory to hqc AND run the setup.py again. (You can refer this stackoverflow answer for more details).
Ensure that you increment the package version inside setup.py before running it.
I have a virtualbox running centOS. I want to install WTForms and Flask_WTF. I have installed them using pip. However, there are import errors.
[vagrant#oclubs ~]$ pyshell
Traceback (most recent call last):
File "/srv/oclubs/repo/shell.py", line 12, in <module>
from oclubs import *
File "/srv/oclubs/repo/oclubs/__init__.py", line 12, in <module>
from oclubs import objs
File "/srv/oclubs/repo/oclubs/objs/__init__.py", line 15, in <module>
from oclubs.objs.classroom import Classroom
File "/srv/oclubs/repo/oclubs/objs/classroom.py", line 7, in <module>
from wtforms import BooleanField, SelectMultipleField
ImportError: No module named wtforms
Flask imports are fully functional. Any suggestions?
There are sometimes many python interpreters (which searches packages in different folders) and pips (which install packages to a different folders) installed. You can check where your interpreter searches packages by adding a code just before line which caused an error: import sys, os; print(sys.version); print(sys.executable); print(os.environ['PATH']); print(os.environ.get('PYTHONPATH')); print(os.getcwd()). Then run pip show wtforms and see where it was installed. If it's not in the PATH or PYTHONPATH, then pay attention to the pip, which you've used to install a package.
P.S. If you are using virtual env, ensure that you are installing a package with this env's pip, not your system's pip.
Please install again with user vagrant and try again. I assume you've installed in root user or anything.
[vagrant#oclubs ~]$ pip install WTForms
I am trying to install ruamel.yaml python library in a Centos 7 machine where I don't have internet connection. I tried this:
installed libyaml-devel, python-devel, python-pip using a local repository
downloaded ruamel.yaml-0.13.14.tar & ruamel.ordereddict-0.4.6.tar(dependency) from pypi.
now I am able to install ruamel.ordereddict using pip install .,
but when I tried to install ruamel.yaml in the same way, I got the following exception:
[root#limulus ruamel.yaml-0.13.14]# pip install .
Processing /tmp/python/ruamel.yaml-0.13.14
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/setuptools/__init__.py", line 2, in <module>
from setuptools.extension import Extension, Library
File "/usr/lib/python2.7/site-packages/setuptools/extension.py", line 5, in <module>
from setuptools.dist import _get_unpatched
File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line 7, in <module>
from setuptools.command.install import install
File "/usr/lib/python2.7/site-packages/setuptools/command/__init__.py", line 8, in <module>
from setuptools.command import install_scripts
File "/usr/lib/python2.7/site-packages/setuptools/command/install_scripts.py", line 3, in <module>
from pkg_resources import Distribution, PathMetadata, ensure_directory
File "/usr/lib/python2.7/site-packages/pkg_resources.py", line 74, in <module>
import parser
File "parser.py", line 77, in <module>
from ruamel.yaml.error import MarkedYAMLError # type: ignore
File "/usr/lib64/python2.7/site-packages/ruamel/__init__.py", line 5, in <module>
__import__('pkg_resources').declare_namespace(__name__)
AttributeError: 'module' object has no attribute 'declare_namespace'
----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-oOWX1x-build/
What is the correct way of installing a python library in offline mode?
It looks like you have been expanding the tar files, which is not necessary, but I am not sure if that is the reason why you get this error, you probably are also missing the typing dependency.
What you shouldn't do is use the system installed pip, it is almost out of date and on some systems actually causes problems because of unfixed bugs in those old versions.
What you also should not do is install packages using pip outside of a Python virtualenv, as this might break your system when it updates/overwrites things in the systems site-packages/dist-packages directory. The solution provided below only installs pip/wheels/virtualenv in the system directory, that is AFAIK safe, but there are ways around that as well if it would necessary.
You should also consider using the provided wheels to install packages. That way C extension don't need compilation.
On a machine that is online do:
mkdir /tmp/offline
cd /tmp/offline
# your python-pip installed pip might not support wheels
wget https://pypi.python.org/packages/11/b6/abcb525026a4be042b486df43905d6893fb04f05aac21c32c638e939e447/pip-9.0.1.tar.gz
wget https://pypi.python.org/packages/a7/37/947b4329c4a3c72093b6c8e9b4be8c7f10c32dbb78848d3a234ce01c059d/wheel-0.30.0a0.tar.gz
wget https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl
Now download the required wheels (.tar.gz if not available):
wget https://pypi.python.org/packages/96/4b/5251bf469f37e49e0c92f841b5b3ef01a1c9d652cc018df470e5ae235eb8/ruamel.ordereddict-0.4.9-cp27-cp27mu-manylinux1_x86_64.whl
wget https://pypi.python.org/packages/63/40/c38b857ec6a083e534ee9a9f655741120d4bfd9139232681f976cf44272b/ruamel.yaml-0.13.14-cp27-cp27mu-manylinux1_x86_64.whl
wget https://pypi.python.org/packages/b6/0c/53c42edca789378b8c05a5496e689f44e5dd82bc6861d1ae5a926ee51b84/typing-3.5.3.0.tar.gz
Copy the six files to a new directory your offline system, and there do
sudo pip install pip*.tar.gz wheel*.tar.gz
sudo pip install virtualenv*
virtualenv ~/ruamel
source ~/ruamel/bin/activate
pip install typing* ruamel*
That gives you a virtualenv with ruamel.yaml installed, just add your application in there as well.
The downloads used here are current as of 2017-01-07, you can get more up-to-date ones using your browser (on the on-line system) if necessary.
I have to use python-elasticsearch library on a machine where I could only execute programs. I am trying to use elasticsearch module by appending sys.path as mentioned below. I am facing below issue. It looks like the problem related to what is mentioned here
https://github.com/elastic/elasticsearch-py/issues/253 . But how do I resolve this when I dont have sudo access or any sort of upgrade access.
**Note :**I don't have sudo access on this machine so I cannot have venv, pip etc.
import sys
sys.path.append('/tmp/elasticpy/elasticsearch-2.3.0')
from elasticsearch import Elasticsearch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/tmp/elasticpy/elasticsearch-2.3.0/elasticsearch/__init__.py", line 17, in <module>
from .client import Elasticsearch
File "/tmp/elasticpy/elasticsearch-2.3.0/elasticsearch/client/__init__.py", line 5, in <module>
from ..transport import Transport
File "/tmp/elasticpy/elasticsearch-2.3.0/elasticsearch/transport.py", line 4, in <module>
from .connection import Urllib3HttpConnection
File "/tmp/elasticpy/elasticsearch-2.3.0/elasticsearch/connection/__init__.py", line 3, in <module>
from .http_urllib3 import Urllib3HttpConnection
File "/tmp/elasticpy/elasticsearch-2.3.0/elasticsearch/connection/http_urllib3.py", line 2, in <module>
import urllib3
ImportError: No module named urllib3
#nehal Thanks for your suggestion I was able to get it fixed. Please find the
series of steps I used to install
Used easy_install to install package pip
Appended PYTHONPATH
used pip to install virtualenv
Activated new virtualenv
Installed elasticsearch using pip
export PYTHONPATH="${PYTHONPATH}:/tmp/pytest/"
easy_install --install-dir /tmp/pytest/ pip
pip install virtualenv --prefix /tmp/pytest/
export PATH="${PATH}:/tmp/pytest:/tmp/pytest/bin"
export PYTHONPATH="${PYTHONPATH}:/tmp/pytest/lib/python2.7/site-packages"
cd /tmp/
virtualenv venv
source venv/bin/activate
pip install elasticsearch
I installed virtualenv using pip and now receive the following error whenever I actually try and use it:
% virtualenv
Traceback (most recent call last):
File "/bin/virtualenv", line 5, in <module>
from pkg_resources import load_entry_point
File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 2705, in <module>
File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 668, in require
File "build/bdist.macosx-10.9-x86_64/egg/pkg_resources.py", line 571, in resolve
pkg_resources.DistributionNotFound: virtualenv==1.11.2
%
Similarly, I installed django with pip, and when I try and import django in python I get: ImportError: No module named django.
I tried to install virtualenv again, this time using easy_install instead of pip. easy_install gave me an error saying that it was having issues with my PYTHONPATH environment variable. Lo and behold, after reading http://www.stereoplex.com/blog/understanding-imports-and-pythonpath, PYTHONPATH is what python uses to find modules, and my PYTHONPATH was unset.
After setting PYTHONPATH to /lib/python3.3/site-packages (where pip was installing my modules), both virtualenv and importing django functioned properly.
Note: because I have both python2.7 and python 3.3 installed, for me pip actually installs packages to /lib/python2.7/site-packages while pip3 installs packages to the aforementioned /lib/python3.3/site-packages.
For the unfamiliar, information on setting environment variables can be found here: https://help.ubuntu.com/community/EnvironmentVariables