Custom package installed but not found in another project - python

I created a Python folder/project and published the code on Github. The folder has the following structure:
/modulename/__init__.py
/modulename/setup.py
/modulename/somefunctions.py
/modulename/README.md
The name of my package is module_helloworld and setup.py looks as follows:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="module_helloworld",
version="0.0.1",
author="Hello World",
author_email="hello#world.com",
description="Hello world module",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://www.website.com",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)
In another project, I installed it in Pycharm using the command
pip install git+https://github.com/Username/module-helloworld.git
That worked fine and in my Project Settings I see the package installed (noticed that it was installed with the name module-helloworld however) .
Now when I open the python console (or a new Python file) and I type
import module_helloworld
Then I get the error:
ModuleNotFoundError: No module named 'module_helloworld'
What did I do wrong?

In my case I had to restructure the folder structure as follows:
root/module_helloworld/__init__.py
root/module_helloworld/somefunctions.py
root/setup.py
And then in the other project I could call it in the normal way.
Inside the function __init__.py to import the functions I had to change it to the following:
# import somefunctions changed to:
from module_helloworld import somefunctions

Related

Problems with pip und Command line

I am trying to create a Python pip package. This works also well. I can successfully upload and download the package and use it in the Python code. What I can't do is to use the Python package via the command line. In another StackOverflow post I found the link to a tutorial. I tried to follow it. Obviously I made a mistake. Can you guys help me please ?
Installation of the package via pip
here you can see that the installation worked. Unfortunately, not the whole script fit on the image.
Pip does not find the package.
Unfortunately, I can't embed the images directly, so I'll just embed them as links.
I have created a simple Python package. It represents here only an example. Here you can see the structure of the folder
Riffecs
| .gitignore
| .pylintrc
| LICENSE
| README.md
| requirements.txt
| setup.py
|
|
\---riffecs
__init__.py
__main__.py
Here are the basic files shown.
main.py
from . import hello_world
if __name__ == '__main__':
hello_world()
and init.py
def hello_world():
print("Hello world")
In the following you can see the "setup.py". I am of the opinion that I have followed the instructions. But obviously I made a mistake somewhere. Can you please help me to correct this mistake.
import io
import os
import setuptools
def read_description():
url = "README.md"
""" Read and Return the description """
return io.open(os.path.join(os.path.dirname(__file__), url), encoding="utf-8").read()
def def_requirements():
""" Check PIP Requirements """
with open('requirements.txt', encoding='utf-8') as file_content:
pip_lines = file_content.read().splitlines()
return pip_lines
setuptools.setup(
name="riffecs",
version='0.0.3',
description='test',
entry_points={'console_scripts': ['hello-world=riffecs:hello_world',]},
long_description=read_description(),
long_description_content_type="text/markdown",
license="MIT",
keywords="test - riffecs",
url="https://github.com/Riffecs/riffecs",
packages=["riffecs"],
install_requires=def_requirements(),
python_requires=">=3.6",
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
],
)
In your setup.py file you have this line...
entry_points={'console_scripts': ['hello-world=riffecs:hello_world',]},
This is the entry point to calling you package via command line. This configuration is asking the entry point to be hello-world, which I tried and it runs fine.
In your image however you run riffecx which is not configured as an entrypoint to the package.
If you wanted the entrypoint to be riffecx. change the line to:
entry_points={'console_scripts': ['riffecx=riffecs:hello_world']},
Hope this helped.

python package not packaging files under a subdirectory

Here is my setup.py
from setuptools import setup, find_packages
import versioneer
REQUIRES = [
"requests",
"subprocess32",
"systemd"
]
CLASSIFIERS = [
...
"Operating System :: OS Independent",
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.6"
]
setup(
author="IBM",
cmdclass=versioneer.get_cmdclass(),
description="A collections of utilities",
entry_points={},
install_requires=REQUIRES,
keywords=["management", "segment_sqls"],
license="XXX",
long_description="""Utilities for instance management and house keeping""",
packages=find_packages(),
tests_require=["pytest"],
url="https://github.ibm.com/myrepo/management",
name="management",
version=versioneer.get_version(),
classifiers=CLASSIFIERS
)
I am trying to run python3 setup.py sdist upload -r local to package and upload to my artifactory.The package's tar.gz is uploaded to artifactory with all files except the ones under segment_sqls. Here is my directory structure. init.py under segment_sqls is packaged but not the actual sql files. Is there any additional param that needs to be specified in setup.py to include all files?

How to package my python program so the user can install it using setup.py

I have a single python file right now and I am asked to convert it into a python module where the user can install it using python setup.py install. I am not sure how to do that. I have followed some instructions online and created the setup.py file and the init.py file. The setup.py file looks like this:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
I am not sure if this setup.py file is correct.Also I don't know what I am supposed to do next. Can anyone help me and tell me what am I supposed to do? Is there tutorial that teaches this? I can't really find anything related. Also is my setup.py correct? Thanks!
There are several ways to do packaging. packaging Python Projects on python.org and setuptools docs are a good start.
Unfortunately, examples tend to focus on package distributions, not single modules. Instead of packages, use the py_modules keyword. Assuming your module is called "test.py", this setup.py will work
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="test",
version="0.0.1",
author="",
author_email="",
description="",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/pypa/sampleproject",
py_modules = ["test"],
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires='>=3.6',
)
If you think this will expand to multiple modules, then you can go back to using
setuptools.find_packages(). In this case, you want a subdirectory named after your desired package and put the init in there
some_random_project_file
+-- setup.py
README.md
LICENCE
+-- test
+-- __init__.py
test.py

Python wheel: "ModuleNotFoundError" after installing package

OS: Windows 7
Python: 3.6
I'm trying to create and installing a python wheel package. The building works fine but when i import the module into project after installing it, i get a "ModuleNotFound" error. My project has the following structure:
my_lib/
__init__.py
phlayer/
__init___.py
uart.py
utils/
__init___.py
ctimer.py
My setup.py for creating the wheel package:
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name="my_lib",
version="0.0.1",
author="",
author_email="",
description="",
packages=setuptools.find_packages(),
classifiers=(
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
),
)
In uart.py i do:
from utils import ctimer
After installing i import the package into another project:
#Test.py
from my_lib.phlayer.uart import Uart
def main(args=None):
pass
if __name__ == "__main__":
main()
And i get the error:
File "C:/.../.../.../Test.py", line 9, in <module>
from my_lib.phlayer.uart import Uart
File "C:\...\...\...\...\...\...\test\env\lib\site-packages\my_lib\phlayer\uart.py", line 3, in <module>
from utils import ctimer
ModuleNotFoundError: No module named 'utils'
So it seems that python cannot find the correct module in the other package. Do i need to specify the correct paths in the setup.py
before creating the wheel package?
You have to specify full module names:
from my_lib.utils import ctimer

Creating a setup.py

I have been working on a simple GUI tool for PIP and have a working prototype. I'm facing issues with creating a setup for it. The program is intended for Debian users (for now) and hence it'd like that after the installation a 'pip-gui' command from the terminal starts it for the user.
The link for the repository with the code is:
https://github.com/ayushpriya10/PIP-GUI
My attempt at making a setup can be found at:
https://github.com/GDGVIT/pip-gui
(The setup I created worked but then it didn't when I tampered a bit with it and hence I would prefer to make a fresh one instead of editing the existing one. I would like to make the necessary changes for the new setup on my personal repository and hence please let me know if I should make any changes to the structure of the repository.)
The code that I have currently is:
import os
import re
import codecs
from setuptools import setup, find_packages
here = os.path.abspath(os.path.dirname(__file__))
def find_version(*file_paths):
try:
f = codecs.open(os.path.join(here, *file_paths), "r", "latin1")
version_file = f.read()
f.close()
except:
raise RuntimeError("Unable to find version string.")
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]",
version_file, re.M)
if version_match:
return version_match.group(1)
raise RuntimeError("Unable to find version string.")
try:
f = codecs.open("README.rst", encoding="utf-8")
long_description = f.read()
f.close()
except:
long_description = ""
setup(
name="pip-gui",
version=find_version("pip_gui/mainGUI.py"),
description="",
url="https://github.com/GDGVIT/pip-gui",
author="GDGVIT",
packages=find_packages(include=[
"pip_gui",
"pip_gui.*"
]),
include_package_data=True,
# py_modules=["pip_gui.mainGUI"],
entry_points={
"console_scripts": [
"pip-gui=pip_gui.mainGUI:main"
]
},
classifiers=[
"Development Status :: 4 - Beta",
"License :: OSI Approved :: Apache Software License",
"Environment :: X11 Applications :: Qt",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 2 :: Only",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: Implementation :: CPython"
],
install_requires=[
"beautifulsoup4>=4.5, <4.5.4"
]
)
The installation via 'pip install pip-gui' runs without any errors but the command 'pip-gui' in terminal shows the error 'command not found'.
Some changes that I want to make is changing the author to 'Ayush Priya ' and the URL pointing to my personal repository.
If a hyphen is appended to the package name (with no intervening space), the identified package will be removed if it is installed. This is just console formatting, so try removing the hyphen from pip-gui (make it pipgui).
Once that is done, if the program is in the directory your console is in, you should be able to just type the package name. Alternatively, use the full path - ie /path/to/program or cd to the directory and run ./program_name

Categories

Resources