Python cannot find newly installed module - python

I've created a module using the following setup.py
# -*- coding: utf-8 -*-
# Learn more: https://github.com/kennethreitz/setup.py
from setuptools import setup, find_packages
with open('README.md') as f:
readme = f.read()
with open('LICENSE') as f:
license = f.read()
setup(
name='mymod',
version='1.0a1',
description='test',
long_description=readme,
long_description_content_type="text/markdown",
author='Ray Salemi',
author_email='ray#raysalemi.com',
url='https://rayboston#bitbucket.org/rayboston/mymod',
license=license,
packages=find_packages(exclude=('tests', 'docs', 'examples'))
)
But when I try to install it using
% python setup.py install
I see that it gets installed in my site packages:
Processing mymod-1.0a1-py3.8.egg
Copying mymod-1.0a1-py3.8.egg to /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages
Adding mymod 1.0a1 to easy-install.pth file
Installed /Users/raysalemi/PycharmProjects/testenv/lib/python3.8/site-packages/mymod-1.0a1-py3.8.egg
Processing dependencies for mymod==1.0a1
Finished processing dependencies for mymod==1.0a1
(testenv) (base) raysalemi#WriteNow mymod % cd ../testenv
(testenv) (base) raysalemi#WriteNow testenv % python
Python 3.8.3 (default, Jul 2 2020, 11:26:31)
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymod'
How do I debug this? I can't see an error.
I'm running Big Sur 11.0.1 and
Python 3.8.3 from Anaconda
Pip shows the module is there
Package Version
---------- -------
pip 20.3.1
mymod 1.0a1
setuptools 41.2.0
The problem is that the package is being misnamed:
(testenv) (base) raysalemi#WriteNow site-packages % ls
__pycache__ mymod-1.0a0-py3.8.egg
easy-install.pth mymod-1.0a0.dist-info
easy_install.py setuptools
pip setuptools-41.2.0.dist-info
pip-20.3.1.dist-info src
pkg_resources
It is mymod-1.0a0-py3.8.egg instead of mymod

To debug you can run the setup:
python setup.py sdist --formats=gztar
and unzip the resulting .tar.gz file and check if all your source code files are in it.
(or use --formats=zip instead of gztar to get a simpler file to extract)
The resulting package is always of the form package_name-package_version, so the name you received is not incorrect. (In case you are wondering, you can find the valid package_version formatting rules here.)
You can later use this package by adding it to the requirements.txt file of the project you want to be dependent on it. E.g.
my-package>=1.2.0,<2.0.0
In your case, since the version is a pre-release (mymod-1.0a0-py3.8.egg ==> version is 1.0a0-py3.8.egg which means version 1.0 pre-relase version alpha0-py3.8).
The version 1.0a0-py3.8.egg < than version 1.0 (pre-release always < release with same number), so you will need something like >0,<2.0.
Personally, I put the source code in the repo under src/ and then select these files in setup.py using:
packages=find_namespace_packages(where="src")
There are other parameters I recommend using e.g. make sure environment has a new enough setuptools to recognize find_namespace_packages, take list of dependencies from requirements.txt files etc.:
from setuptools import setup, find_namespace_packages
with open('requirements.txt') as f:
required = f.read().splitlines()
setup(
name='your_project_name',
version='1.0.0',
description='your project description',
url='your repo url',
author='your username',
author_email='your email',
license='your license type',
package_dir={'': 'src'},
setup_requires='setuptools>=45.2.0',
packages=find_namespace_packages(where="src"),
install_requires=required,
data_files=['requirements.txt'],
include_package_data=True
)
See the full list of options and what they are for in the documentation.

I found my problem.
My source directory was named src not mymod. So there was a src directory in site-packages instead of a mymod directory. This is a surprise since the package is named in setup.py.

Related

pip: module installed as editable not found

I'm trying to install the source checkout of my own package as an editable package using pip, but it doesn't get picked up as installed.
Yes, there are a whole lot of related questions, but they're mostly about non-standard directory structures. This here seems like it should "just work" and partially even does.
# starting from an empty venv:
(env) PS C:\test> pip install -e C:\path\to\my\PySymCircuit
Obtaining file:///C:/path/to/my/PySymCircuit
Preparing metadata (setup.py) ... done
Collecting sympy
Using cached sympy-1.9-py3-none-any.whl (6.2 MB)
Collecting mpmath>=0.19
Using cached mpmath-1.2.1-py3-none-any.whl (532 kB)
Installing collected packages: mpmath, sympy, SymCircuit
Looks good, but:
(env) PS C:\test> python -c 'import symcircuit'
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'symcircuit'
(env) PS C:\test> pip show SymCircuit
WARNING: Package(s) not found: SymCircuit
The link file that is created is correct (as well as the egg-info in the source directory), and when I try to uninstall the package, suddenly it is recognized. Just not when it matters...
(env) PS C:\test> cat .\env\Lib\site-packages\SymCircuit.egg-link
C:\path\to\my\PySymCircuit
.
(env) PS C:\test> pip uninstall SymCircuit
Found existing installation: SymCircuit 0.1.0
Uninstalling SymCircuit-0.1.0:
Would remove:
c:\test\env\lib\site-packages\symcircuit.egg-link
Proceed (Y/n)? Y
WARNING: Cannot remove entries from nonexistent file c:\test\env\lib\site-packages\easy-install.pth
Successfully uninstalled SymCircuit-0.1.0
Plain setup.py develop produces the same link, also not usable.
What am I missing?
A non-editable install from the same source tree works as intended, so I believe it has something to do with the linking mechanism?
Update: so apparently the link is just for pip, the important part is that setuptools should add the path to easy-install.pth. It doesn't, hence the error message when uninstalling.
Now the question is, why...
Update as requested:
Local directory structure: git clone https://github.com/martok/py-symcircuit.git
py-symcircuit
│ LICENSE
│ README.md
│ setup.py
├───.git
├ ...
├───symcircuit
│ __init__.py
│ bode.py
│ spice.py
│ system.py
└───SymCircuit.egg-info
dependency_links.txt
PKG-INFO
requires.txt
SOURCES.txt
top_level.txt
Setup.py:
#!/usr/bin/env python
from setuptools import setup, find_packages
setup(
name="SymCircuit",
version="0.1.0",
author="Martok",
author_email="martok#martoks-place.de",
description="Symbolic electronic circuit analysis",
long_description=open("README.md","rt").read(),
long_description_content_type="text/markdown",
url="https://github.com/martok/py-symcircuit",
project_urls={
"Bug Tracker": "https://github.com/martok/py-symcircuit/issues",
},
license="MIT",
classifiers=[...],
packages=find_packages(),
python_requires='>=3.6',
install_requires=[
"sympy",
],
extras_require={
"EE": [
"networkx",
"numpy",
"mplotkit"
],
},
)
Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 23 2018, 23:31:17) [MSC v.1916 32 bit (Intel)] on win32
Package Version
---------------- ---------
pip 21.3.1
setuptools 59.4.0

How to create a shareable python distribution package that can be installed using pip?

I want to share my python code with my colleague in a way that he will get the distribution package and using pip the package can be installed on his/her machine.
I created .whl file which i thought can be directly installed through the pip install command.
Though it was installed successfully, when i start using it shows the error.
Is this possible like i give .whl file and it can be used in other's machine once installed through pip install command ?
I'm trying to do it on windows machine.
Here is the setup.py :
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name='dokr',
version='0.1',
scripts=['dokr'] ,
author="Debapritam Chakra",
author_email="debapritam22#gmail.com",
description="A Sample package",
long_description=long_description,
long_description_content_type="text/markdown",
url="",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 2.7",
"",
"Operating System :: OS Independent",
],
)
package that i am trying to create is dokr and have file named dokr in the same directory which has the content shown below.
#!/usr/bin/env python
echo "hey there, this is a pip package"
used the command python setup.py sdist bdist_wheel to generate the distribution package.
To install package on my machine, I used the command :
python -m pip install dist/name-of-wheel-file.whl
It showed it is installed successfully(even checked using the pip list).
It throws the error when i try to import the package as
import dokr
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named dokr
Additional observation :
Python on my machine is installed under C:\Python27.
After installing the package from whl file using pip, I could see there is a directory created under the path : C:\Python27\Lib\site-packages\
named dokr-0.1.dist-info.For which pip list shows that the module is present.
But there is no such folder having the python file dokr itself, which i want to import in other python file. which shows error during importing.
I am new to python and this platform as well. Any lead would be helpful.
Ofcourse, you can create a python distribution package.
Can you clearly show what is the error?
However, you can look into the following link!
: Packaging Python Projects
Hope this helps!
Fortunately found the cause of the problem.
I did not pass the argument( packages which takes the list of packages to be included ) in the setup function, for which packages could not be copied.
Following article and an awesome answer helped to sort out the issue.
https://setuptools.readthedocs.io/en/latest/setuptools.html#developer-s-guide
pip install . creates only the dist-info not the package
And I thank everyone who came here to help. :)

Python Setuptools CLI building

I have a little trouble getting my cli built & installed correctly with setuptools.
My project structure looks like this :
foobar/
cli/
src/
main
__init__.py
config.json
__init__.py
setup.py
requirements.txt
venv.sh
README.md
.gitignore
__init__.py
My setup.py looks like this :
from setuptools import setup, find_packages
# Package meta-data.
__version__ = '0.0.1'
# What packages are required for this module to be executed?
REQUIRED = ['requests', 'argparse']
# Where the magic happens:
setup(
name='foobar',
version=__version__,
description='foo bar',
long_description="https://github.com/foo/bar",
long_description_content_type='text/markdown',
author='foo bar',
author_email='foo#bar.com',
packages = find_packages(),
url='https://github.com/foo/bar',
entry_points={'console_scripts': ['foobar=cli.src.main:main']},
python_requires='>=3.6.0',
install_requires=REQUIRED,
include_package_data=True,
classifiers=[
"Topic :: Utilities"
]
)
All I am trying to achieve is this :
pip uninstall -y foobar # remove old installation
python setup.py build # build
python setup.py install # install
foobar # run the "cli.main:main"
This results in the following error :
bash: /Users/myuser/.pyenv/shims/foobar: No such file or directory
Update #1
My package is definitely getting built & installed. Because I see my packaged with I do a pip freeze | grep foobar. Its definitely there.
Update #2
My package can also be imported in a python shell as follows :
$ python
Python 3.6.8
Type "help", "copyright", "credits" or "license" for more information.
>>> import cli
>>> import cli.src
>>>
No errors here ^ indicate, my package imports are fine. But I am trying to make my package accessible directly from the cli. Typing foobar should directly trigger the cli.src.main:main as described in my entry_points section. That seems to NOT work.
Update #3
$ which foobar
$ echo $?
1
This tells me the mapping for the cli command is not defined. I am out of options now :|. I must have messed up somewhere in the setup.py .. any leads will be helpful.

Tox can't copy non-python file while installing the module

This is the tree structure of the module I'm writing the setup.py file for:
ls .
LICENSE
README.md
bin
examples
module
scratch
setup.py
tests
tox.ini
I configured my setup.py as follows:
from setuptools import setup, find_packages
setup(
name="package_name",
version="0.1",
packages=find_packages(),
install_requires=[
# [...]
],
extras_require={
# [...]
},
tests_require={
'pytest',
'doctest'
},
scripts=['bin/bootstrap'],
data_files=[
('license', ['LICENSE']),
],
# [...]
# could also include long_description, download_url, classifiers, etc.
)
If I install the package from my python environment (also a virtualenv)
pip install .
the LICENSE file gets correctly installed.
But running tox:
[tox]
envlist = py27, py35
[testenv]
deps =
pytest
git+https://github.com/djc/couchdb-python
docopt
commands = py.test \
{posargs}
I get this error:
running install_data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data/data
creating build/bdist.macosx-10.11-x86_64/wheel/leafline-0.1.data/data/license
error: can't copy 'LICENSE': doesn't exist or not a regular file
Removing the data_files part from the setup.py makes tox running correctly.
Your issue here is that setuptools is not able to find the 'LICENSE' file in the files that have been included for building the source distribution. You have 2 options, to tell setuptools to include that file (both have been pointed to here):
Add a MANIFEST.in file (like https://github.com/pypa/sampleproject/)
Use include_package_data=True in your setup.py file.
Using MANIFEST.in is often simpler and easier to verify due to https://pypi.org/project/check-manifest/, making it possible to use automation to verify that things are indeed correct (if you use a VCS like Git or SVN).
pip install . builds a wheel using python setup.py bdist_wheel which is installed by simply unpacking it appropriately, as defined in the Wheel Specification: https://www.python.org/dev/peps/pep-0427/
tox builds a source distribution using python setup.py sdist, which is then unpacked and installed using python setup.py install.
That might be a reason for the difference in behavior for you.
I have some resource files inside my packages which I use during the execution. To make setup store them in a package with python code, I use include_package_data=True and I access them using importlib.resources. You can use backport for an older Python version than 3.7 or another library.
Before each release I have a script which verifies, that all files I need are placed inside a bdist wheel to be sure that everything is on the place.

how to install and use open-tamil package?

I want to use the open-tamil package for processing tamil text.
I downloaded it as "open-tamil-master" package which has a setup.py file. but on running the file it says "error no commands supplied".
also available in https://github.com/arcturusannamalai/open-tamil
can some one say me how to install and use it??
To download and install (using setup.py) open-tamil you can do the following
git clone https://github.com/arcturusannamalai/open-tamil.git
cd open-tamil
sudo python setup.py install
it says "error no commands supplied"
That is because you need to tell setup.py what to do by supplying a command. In this case that is install.
Now you can import its modules etc:
tim#tim-N53SN:~$ python
Python 2.7.3 (default, Feb 27 2014, 19:58:35)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import transliterate
>>> import tamil
>>>
The full list of what commands you can supply to setup.py:
Standard commands:
build build everything needed to install
build_py "build" pure Python modules (copy to build directory)
build_ext build C/C++ extensions (compile/link to build directory)
build_clib build C/C++ libraries used by Python extensions
build_scripts "build" scripts (copy and fixup #! line)
clean clean up temporary files from 'build' command
install install everything from build directory
install_lib install all Python modules (extensions and pure Python)
install_headers install C/C++ header files
install_scripts install scripts (Python or otherwise)
install_data install data files
sdist create a source distribution (tarball, zip file, etc.)
register register the distribution with the Python package index
bdist create a built (binary) distribution
bdist_dumb create a "dumb" built distribution
bdist_rpm create an RPM distribution
bdist_wininst create an executable installer for MS Windows
upload upload binary package to PyPI
check perform some checks on the package

Categories