Local Python Package Not Found - python

I have created a package named aTask on my local machine and successfully installed it (as there wasn't an error happened) using pip install -e ./tasks. To double check, pip list | grep aTask and I got aTask (0.2.0, /Users/WorkPlace/projects/tasks).
But when I have been trying to import to my interpreter, I have an error
In [1]: import aTask
ImportError Traceback (most recent call last)
<ipython-input-1-d76cc8326300> in <module>()
----> 1 import aTask
ImportError: No module named aTask
Would you please give me a hint to resolve my case? Below I put more details
setup.py consists of
from setuptools import setup, find_packages
package_name = 'aTask'
version = '0.2.0'
install_requires = ['pandas==0.21.1', 'numpy==1.13.3', 'scikit-learn==0.19.1', 'scipy==1.0.0']
CLASSIFIERS = [
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7'
]
description = 'an example to install a package'
setup(name=package_name,
description=description,
author='XXXX',
version=version,
packages=find_packages(exclude=['test', 'test.*']),
platforms=['Any'],
install_requires=install_requires,
classifiers=CLASSIFIERS)
my folder structure as
> tree .
.
├── DataPopulation
│   └── main.py
├── README.md
├── Strategies
│   ├── Exceptions
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── IStrategy.py
│   ├── IStrategy.pyc
│   ├── Algorithms
│   │   ├── SVM.py
│   │   ├── SVM.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── miscellaneous.py
│   │   └── miscellaneous.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── StrategyBasicContext.py
├── StrategyBasicContext.pyc
├── Ultils
│   ├── DataIO
│   │   └── __init__.py
│   ├── __init__.py
│   └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── requirements.txt
└── setup.py

Related

pytest plugins override each other

I have 2 similar plugins I wrote:
import pytest
#pytest.fixture
def pyplug<ID>_fixture():
pass
where <ID> is replaced with the number I gave the plugin, i.e. '0', '1'...
I created a setup.py for each plugin:
from setuptools import setup
setup(
name='pyplug<ID>',
packages=['pyplug<ID>'],
entry_points={'pytest11': ['pkg = pyplug<ID>.plugin']}
)
and inside each package, I created another dir (same name) with plugin.py file, containing the fixture described earlier.
This is my dir tree AFTER I run python setup.py bdist_wheel for each plugin:
src/
├── pyplug0
│   ├── build
│   │   ├── bdist.linux-x86_64
│   │   └── lib
│   │   └── pyplug0
│   │   └── plugin.py
│   ├── dist
│   │   └── pyplug0-0.0.0-py3-none-any.whl
│   ├── pyplug0
│   │   └── plugin.py
│   ├── pyplug0.egg-info
│   │   ├── dependency_links.txt
│   │   ├── entry_points.txt
│   │   ├── PKG-INFO
│   │   ├── SOURCES.txt
│   │   └── top_level.txt
│   └── setup.py
└── pyplug1
├── build
│   ├── bdist.linux-x86_64
│   └── lib
│   └── pyplug1
│   └── plugin.py
├── dist
│   └── pyplug1-0.0.0-py3-none-any.whl
├── pyplug1
│   └── plugin.py
├── pyplug1.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── PKG-INFO
│   ├── SOURCES.txt
│   └── top_level.txt
└── setup.py
This is my pip freeze:
attrs==19.3.0
importlib-metadata==1.6.0
more-itertools==8.2.0
packaging==20.3
pluggy==0.13.1
py==1.8.1
pyparsing==2.4.7
pyplug0==0.0.0
pyplug1==0.0.0
pytest==5.4.1
pytest-html==2.1.1
pytest-metadata==1.8.0
pytest-tldr==0.2.1
six==1.14.0
wcwidth==0.1.9
zipp==3.1.0
and this is my test file content:
def test_plugin(pyplug0_fixture):
pass
def test_plugin2(pyplug1_fixture):
pass
The problem is, the plugins are not registered OK.
When I install only one (doesn't matter which) and test it, it's OK. When I install both, one is overridden by the other, which makes no sense IMO, since they have different names (for both the fixture, and the plugin)
I run py3.6.10 on Ubuntu16.04
Found it.
Both setup.py had the same entry point:
entry_points={"pytest11": ["KEY = VAL"]}
the VAL was different, but I kept using the same KEY, so the latest always won

Python package with cython extension get import error

I want to make python package with C extensions. I want this to be done with cython. My structure is:
.
├── build
│   ├── lib.linux-i686-2.7
│   │   └── pyA13SOM
│   │   ├── cython
│   │   │   └── spi.so
│   │   └── __init__.py
│   └── temp.linux-i686-2.7
│   └── pyA13SOM
│   └── cython
│   ├── my_test.o
│   └── spi.o
├── CHANGES.txt
├── Makefile
├── MANIFEST
├── pyA13SOM
│   ├── cython
│   │   ├── clibraries
│   │   │   └── spi_test.c
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── spi.c
│   │   ├── spi.pyx
│   │   └── spi.so
│   ├── gpio
│   │   ├── gpio.c
│   │   ├── gpio_lib.c
│   │   ├── gpio_lib.h
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── i2c
│   │   ├── i2c.c
│   │   ├── i2c_lib.c
│   │   ├── i2c_lib.h
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── spi
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── spi.c
│   │   ├── spi_lib.c
│   │   └── spi_lib.h
│   └── utilities
│   └── color.h
├── README.txt
└── setup.py
My setup file is:
from distutils.core import setup
from distutils.core import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
module_gpio = Extension('pyA13SOM.gpio',
sources=['pyA13SOM/gpio/gpio_lib.c', 'pyA13SOM/gpio/gpio.c'])
module_i2c = Extension('pyA13SOM.i2c',
sources=['pyA13SOM/i2c/i2c_lib.c', 'pyA13SOM/i2c/i2c.c'])
module_spi = Extension('pyA13SOM.spi',
define_macros=[('CYTHON_IN_USE', '1')],
sources=['pyA13SOM/spi/spi_lib.c', 'pyA13SOM/spi/spi.c'])
setup(
name='pyA13SOM',
version='0.2.0',
packages=['pyA13SOM'],
# ext_modules=[module_gpio, module_i2c, module_spi],
cmdclass={'build_ext': build_ext},
ext_modules=cythonize("pyA13SOM/cython/*.pyx"),
)
The tree is in ~/mydir/. I go to ~/mydir/ and do: python setup.py install.
Everything in the build process is OK. Next I try to test import. When I import pyA13SOM.cython.spi, it should give me "Hello world" message. And it does.
~/mydir/$ **python -c "import pyA13SOM.cython.spi"**
Test:
Hellowwwwwwwwwww!
But when I do this from another directory:
~/someotherdir/$ **python -c "import pyA13SOM.cython.spi"**
ImportError: No module named cython.spi
Any idea why does this happen?
You might need to include the directory in which your newly built .spi file is located into your $PYTHONPATH. Otherwise python cannot find the file to import it. While you are in ~/mydir/, python searches the local path if I am not mistaken...
Depending on the shell you are using, you can include the ~/mydir/ directory into the pythonpath with the following:
for the bash and sh shells:
PYTHONPATH=$PYTHONPATH:~/mydir/
export $PYTHONPATH
for the csh/tcsh environment:
set PYTHONPATH = ($PYTHONPATH ~/mydir/)
These two commands add the ~/mydir/ temporarily to your $PYTHONPATH. If you want to add the path permanently, you will have to add the above commands to your ~/.bashrc or ~/.tcshrc, respectively.
Hope this helps...

How to fix "ImportError: No module named ..."

I have reviewed most of the similar question here.
I'm new to python and I'm using Ubuntu 13.10
The project structure is
├── projecttest
│   ├── api.py
│   ├── controller
│   │   ├── controller.py
│   │   ├── controller.pyc
│   │   ├── init_db.py
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── settings.py
│   │   ├── settings.pyc
│   │   └── extra
│   │   ├── extra.py
│   │   ├── extra.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── __init__.py
│   ├── lib
│   │   └── __init__.py
│   ├── models
│   │   ├── documents.py
│   │   ├── documents.pyc
│   │   └── __init__.py
All the __init__.py files are empty (no hidden characters) and when I'm trying
$ python init_db.py
that has:
from projecttest.models.documents import *
I'm getting:
Traceback (most recent call last):
File "controllers/init_db.py", line 1, in <module>
from projecttest.models.documents import *
ImportError: No module named projecttest.models.documents
You need to specify PYTHONPATH environment variable, it augments the default search path for module files.
It helps to think about PYTHONPATH as an absolute path. If you specify it you may import modules within your program relative to PYTHONPATH.
In your case it would be something like the following line:
PYTHONPATH=/<dir>/<folder>/projecttest/ python init_db.py
Then you may import modules without problems like:
from models.documents import *

How to import my modules in other projects installed by PIP

How to import my modules streaming_capture, streaming_information, report in in other project ?
I wrote a Python project called long_term_streaming_info_capture,
Then installed it in a new project under virtualenv environment.
If I want to import StreamingCapture I should called from long_term_streaming_info_capture.scripts.streaming_capture import StreamingCapture
Can I just use the long_term_streaming_info_capture.streaming_capture without the folder script in the import path ?
(develop+-)$ tree -L 3 -P "*.py"
.
├── helpers
│   ├── __init__.py
│   └── animation
│   ├── __init__.py
│   ├── animation_helper.py
│   ├── dqa_file_io.py
│   ├── dqa_telnet.py
│   ├── file_io_helper.py
│   ├── shm_controller.py
│   ├── telnet_helper.py
│   ├── test_dqa_file_io.py
│   └── test_dqa_telnet.py
├── log
├── main.py
├── report.py
├── sandbox
├── streaming_capture.py
└── streaming_information.py
Project skeleton
.
├── HACKING.txt
├── MANIFEST.in
├── NEWS.txt
├── README.rst
├── bootstrap.py
├── buildout.cfg
├── setup.py
└── src
├── long_term_streaming_info_capture
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── docs
│   ├── scripts
│   └── tests
└── long_term_streaming_info_capture.egg-info
├── PKG-INFO
├── SOURCES.txt
├── dependency_links.txt
├── entry_points.txt
├── not-zip-safe
└── top_level.txt
Here my setup.py
from setuptools import setup, find_packages
import sys, os
here = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(here, 'README.rst')).read()
NEWS = open(os.path.join(here, 'NEWS.txt')).read()
version = '0.1'
install_requires = [
# List your project dependencies here.
# For more details, see:
# http://packages.python.org/distribute/setuptools.html#declaring-dependencies
]
setup(name='long_term_streaming_info_capture',
version=version,
description="for capturing fps framerate",
long_description=README + '\n\n' + NEWS,
classifiers=[
# Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
],
keywords='',
license='',
packages=find_packages('src'),
package_dir = {'': 'src'},include_package_data=True,
zip_safe=False,
install_requires=install_requires,
entry_points={
'console_scripts':
['long_term_streaming_info_capture=long_term_streaming_info_capture:main']
}
)

ImportError under two identical folder

I run the same command under two identical Python project on 2 PCs
python ./main -xls nd.xls -xml nd.xml -t 1234
PC A : works greate
PC B throws the exception
Traceback (most recent call last):
File "./main", line 21, in <module>
from color_print import *
ImportError: No module named color_print
from color_print import *
from debug_tool import *
The both python edition are 2.7
I just curious why works on PC A and failed on PC B.
I must make some mistakes.
.
├── common
│   ├── Common
│   │   ├── __init__.py
│   │   ├── color_print.py
│   │   └── debug_tool.py
│   ├── Excel
│   │   ├── Xls.pyc
│   │   ├── XlsOperation.py
│   │   ├── XlsOperation.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── README.md
│   ├── __init__.py
│   ├── __init__.pyc
│   └── tmpl.py
├── main
├── nd.xls
├── nd.xml
├── nd_excel.py
├── nd_excel.pyc
├── nd_xml.py
├── nd_xml.pyc
└── origin_nd.xls

Categories