Install local package in anaconda environment - python

ISSUE: I would like to install local package in a specific conda environment. To do that, I read the current documentation (python-packaging).
package structure:
$ pwd
~/…/test
.
|- requirements.txt
|- my_package
| |-- __init__.py
| |-- base.py
|- setup.py
setup.py
# -*- coding: utf-8 -*-
import os
from setuptools import setup
with open('requirements.txt') as f:
requirements = f.read().splitlines()
setup(
name='my_package',
version='2.0.0',
author='B.Gees',
author_email='B.Gees#gmail.com',
license='MIT',
packages=['my_package'],
description='my package description',
long_description='my package long description',
keywords='chemistry machine learning cheminformatics',
classifiers=[
'Environment :: Console',
'Intended Audience :: Developers',
'Intended Audience :: Healthcare Industry',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.5.5',
'Topic :: Scientific/Engineering',
'Topic :: Scientific/Engineering :: Bio-Informatics',
'Topic :: Scientific/Engineering :: Chemistry',
'Topic :: Scientific/Engineering :: Pharmacokinetic',
'Topic :: Software Development :: Libraries :: Python Modules',
],
install_requires=requirements,
zip_safe=False
)
requirements.txt
pandas==0.19.2
dill==0.2.7.1
cython==0.23.4
__init__.py
# -*- coding: UTF-8 -*-
"""
my_package
~~~~~~~~~~
my package full description
:copyright: (c) 2018 by B.Gees.
:license: MIT, see LICENSE file for more details.
"""
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import logging
__title__ = 'my_package'
__version__ = '2.0.0'
__author__ = 'B.Gees'
__email__ = 'B.Gees#gmail.com'
__license__ = 'MIT'
__copyright__ = 'Copyright 2018 B.Gees'
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
base.py
# -*- coding: UTF-8 -*-
def titi(x):
return x**2
I install my package in a specific conda environment with the folowing code lines:
conda activate my_env
pip install . # In my package repository
Nevertheless, when I try to import my_package in jupyter notebook, I obtain the following error:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-9-daa52839320b> in <module>()
----> 1 import my_package
ImportError: No module named 'my_package'
This installation works fine when I used python pip outer conda environment.
QUESTION: I don't know how to install my package correctly in a specific conda environment. I need your lights to enlighten me.
CONFIGURATION: MacOSX with conda3 and python3.5 ; Need to be compatible with Linux 7

You are using MacOSX, so you should firstly use source activate yourenvname, then you can use what you did to install your package. for more information How to activate an Anaconda environment
so start with : conda create --name my_env python=3.5
then source activate my_env

Related

Custom package installed but not found in another project

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

Something is breaking my package deployment

I use setuptools and a requirements file to satisfy dependencies for my open source module.
Example setup.py:
from setuptools import setup, find_packages
from pip.req import parse_requirements
# parse_requirements() returns generator of
# pip.req.InstallRequirement objects
install_reqs = parse_requirements('requirements.txt',
session=False)
# reqs is a list of requirement
reqs = [str(ir.req) for ir in install_reqs]
setup(
name='python-symphony',
version='0.1.5',
description='python module for symphony chat',
author='Matt Joyce',
author_email='matt#joyce.nyc',
url='https://github.com/symphonyoss/python-symphony',
license='Apache 2.0',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
],
keywords='symphony chat api python module',
# install dependencies from requirements.txt
install_requires=reqs,
packages=find_packages(),
# bin files / python standalone executable scripts
include_package_data=True,
zip_safe=False,
)
This has worked for some time. However, recently my builds have been breaking. I believe there is a bug in setuptools, possibly related to pygments.
There are two (thus far) identified failure states:
It results in the package being installed as a binary wheel (despite no such package existing in PyPI).
It results in the package installing in site-packages seemingly correctly, but none of the subclasses can be called from the module.
I can get it to work manually by removing the package from site packages and running:
pip install --isolated --no-cache-dir python-symphony
but this results in pygments getting messed up somehow. (I noticed this when trying to debug the module in bpython).
Trying to run this stuff from a venv (wrapped with a Bash script) tends to still fail.
Is anyone aware of a new / recent issue in setuptools that could be responsible for this sort of breakage?
setuptools version: setuptools-33.1.1.dist-info and newer
Ref:
github: https://github.com/symphonyoss/python-symphony
pypi: https://pypi.python.org/pypi/python-symphony/

pip install ignoring child directories on install with django project

I am writing a Django application and am very new to the framework and python. I am following the tutorial here: https://docs.djangoproject.com/en/1.9/intro/reusable-apps/ and am currently at packaging and installing the application created from this tutorial: https://docs.djangoproject.com/en/1.9/intro/tutorial01/
This is my first Django application and my first time using Python.
Currently my setup.py file looks like this:
import os
from setuptools import find_packages, setup
with open(os.path.join(os.path.dirname(__file__), 'README.rst')) as readme:
README = readme.read()
# allow setup.py to be run from any path
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
setup(
name= 'django-polls',
version='0.1',
packages=find_packages(),
include_package_date=True,
license='BSD License', #example license
description = 'A simple Django app to conduct Web-based polls.',
long_description = README,
url='https://www.example.com',
author='Your Name',
author_email='yourname#example.com',
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Framework :: Django :: 1.9.6', # replace "X.Y" as appropriate
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License', # example license
'Operating System :: OS Independent',
'Programming Language :: Python',
# Replace these appropriately if you are stuck on Python 2.
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Topic :: Internet :: WWW/HTTP',
'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
],
)
my MANIFEST.in file looks like this:
include LICENSE
include README.rst
recursive-include polls/static *
recursive-include polls/templates *
recursive-include docs *
my file directory is as follows:
|polls
|__init.py__
|admin.py
|apps.py
|models.py
|tests.py
|urls.py
|views.py
|__pychache__
|# pycachestuff
|migrations
|# migrations stuff
|static
|polls
|images
|background.gif
|style.css
|templates
|polls
|detail.html
|index.html
|results.html
|docs
|
When I run the setup, the produced .zip works great, all the directories are included. However, when I run
pip install C:\Path\To\dist\django-polls-0.1.zip -t C:\Path\To\Package
It only installs what is immediately under the primary polls directory and migrations. It ignores the subdirectories static and templates. It also ignores docs, but the folder is empty and the tutorial has led me to believe empty directories are ignored.
Does anyone know how I can fix this? A few answers I have seen suggest using package_data or data_files, but I'm very new to python and I can't seem to get the syntax right (or those answers are wrong).
Thank you for your time

My setup.py is not working -- install fails

So, I've been trying this whole week to write a proper setup.py, and ended up with something like this:
#!/usr/bin/env python
from setuptools import setup
from codecs import open
from os import path
# Get the version number
from sql_tools2 import __version__
__author__ = 'Pandu POLUAN (pepoluan)'
__copyright__ = '(C) 2015, Pandu POLUAN'
__maintainer__ = 'pepoluan'
__email__ = 'pepoluan#gmail.com'
__status__ = 'Development'
__credits__ = ['pepoluan']
here = path.abspath(path.dirname(__file__))
# Get the long description from the README file
with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
long_description = f.read()
setup(
name='sql_tools2',
version=__version__,
description='Tools to manipulate and analyze SQL Queries before execution.',
long_description=long_description,
url='https://bitbucket.org/pepoluan/sqltools2',
author='Pandu POLUAN',
author_email='pepoluan#gmail.com',
license='MPL 2.0',
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Intended Audience :: System Administrators',
'Programming Language :: SQL',
'Topic :: Software Development :: Quality Assurance',
'Topic :: Database',
'License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7'
],
keywords='sql subquery',
packages=['sql_tools2'],
install_requires=['sqlparse'],
extras_require={
'dev': ['pytest'],
'test': ['pytest'],
},
zip_safe=False
)
Now if I execute setup.py install, I see the module being installed.
However, another Python program (same virtualenv) cannot import:
>>> import sql_tools2
ImportError: no module named sql_tools2
I am using PyCharm as the IDE. Interestingly, if I type import and wait for auto-completion, PyCharm indeed offered sql_tools2 as one of the modules it recognized.
Where did I go wrong?

How do I mark a Python package as Python 2 only?

I have a Python package that only runs on Python 2. It has the following classifiers in its setup.py:
setup(
# ...
classifiers=[
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2 :: Only',
])
However, if I create a virtualenv with Python 3, pip happily installs this package.
How do I prevent the package being installed? Should my setup.py throw an error based on sys.version_info? Can I stop pip even downloading the package?
In setup.py, add this:
import sys
if sys.version_info[0] != 2:
sys.stderr.write("This package only supports Python 2.\n")
sys.exit(1)
In newer versions of setuptools and pip, if you're using setup.py, here's how to specify a requirement of Python 2 only (specifically Python 2.7):
from setuptools import setup
setup(
name="my_package_name",
python_requires='>=2.7,<3.0',
# ...
)
It would also be good to include classifiers, like this:
setup(
name="my_package_name",
python_requires='>=2.7,<3.0',
classifiers=[
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 2 :: Only",
],
)

Categories

Resources