How to get rid of library root mark in PyCharm - python

I've started working on a Pyramid application, which requires setup.py usage, but as soon as I built the app, my app folder was marked as library root.
It is not convenient, because when I open a file, it is also opened under External Libraries unfolding it. This can be "fixed" by removing check on Always Select Opened File, but I like this feature, so I don't want to disable it.
I've also tried to tweak Project Structure in settings, but it didn't help.
How to get rid of this _library_root_ mark?
UPD. Contents of setup.py:
setup(
name='app',
version=0.1,
description='Blog with CMS',
classifiers=[
"Programming Language :: Python",
"Framework :: Pylons",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application"
],
keywords="web services",
author='',
author_email='',
url='',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
install_requires=['cornice', 'waitress'],
entry_points="""\
[paste.app_factory]
main=app:main
""",
paster_plugins=['pyramid']
)

The reason why it happens is PyCharm adds sd-blog/app to the interpreter paths once you pip install -e in order to be able to provide completion for app objects. Possible workaround:
remove app from the interpreter paths
mark sd-blog/app as a source root to restore the code insight broken in the step 1

Related

Correct build python PIP package

How to process the correct build of my application to PIP?
I have done everything like a need in the documentation and it works, but after I have updates and my scripts changed from one to few (started from "main.py" script which imported others).
And my build process is broken now. How I able to fix this?
setup.py
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
setuptools.setup(
name='tests',
version='0.0.2',
scripts=['tests'] ,
author="Test",
author_email="test#test.com",
description="TEST",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://test.com",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: Unix"
],
)
Where "platops" is a directory with scripts.
Error
error: [Errno 21] Is a directory: 'tests'
How to correct build this?
It seems like you can't add a directory in scripts=[]. You can read up on it here. You will probably need to specify the relative path to each one.
From the docs:
Scripts are **files** containing Python source code, intended to be
started from the command line.
Edit: You could also try using globbing:
scripts=['scripts/*']

Make my pypi script executable without the need to specify every time the path to the script

Recently I created a python script for PyPI. That you can download with pip install. The problem is you can only execute the script, that you downloaded with pip install, when you are in the Scripts folder which is where you python is localized (your_python_location/Scripts/myscript.py).
But this would be a hassle for the users. So I wanted to ask, how can I make it that you can execute the script from everywhere? (like you can do with pip without specifying the pip location). I also don't want that every user needs to set the path to the script.
My Setup.py (maybe its helpful):
import setuptools
with open("README.md", "r") as fh:
long_description = fh.read()
with open('requirements.txt') as f:
requirements = f.read().splitlines()
setuptools.setup(
name="boston-housing-prediction",
version="0.2.0a0",
author="xx",
author_email="xxx#gmail.com",
py_modules=["misc_libary", "polynomial_regression_libary", "linear_regression_libary"],
scripts=["boston-housing-main.py"],
description="Predict housing prices in boston.",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/XXX",
packages=setuptools.find_packages(),
classifiers=[
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7'
],
keywords="regression meachine-learning housing_prices_boston learning_purpose",
license="MIT",
install_requires=requirements,
python_requires='>=3.5',
)
You can specify entry_points in setup.py, like
setuptools.setup(
# ...
entry_points = {
'console_scripts': [
'boston_housing = boston-housing-main:main'
]
},
)
This will cause pip install to install a wrapper somewhere like /usr/local/bin/boston_housing which basically pulls in the module boston-housing-main and runs its main() function.
You probably want to replace the scripts entry with this, though there is no reason per se you could not have both.
One approach to making a globally accessible Python script is to have your users call the module itself. If your package is called 'boston-housing-prediction', your users will be able to call your script from anywhere using the following command:
python -m boston-housing-prediction
What this does is calls a __main__.py file inside your package. This is just like any other Python script, so it can take arguments normally. All you have to do is rename your script to __main__.py and drop it into the package directory (not the folder including setup.py, rather into the folder including the package scripts), or create a new __main__.py that calls your script (you can just import the script if they are in the same folder).
The benefit of this approach is that it is platform independent, relying only on the proper installation of the packages. It doesn't rely on the OS at all.

Flake8 failed to load plugin "N8" on custom Formatter

I want to build a custom formatter for class and function names.
According to this doc it says that the naming convention falls under the N8** warning code.
After following this tutorial with the help of a sublink this is the resultant code
setup.py
from __future__ import with_statement
import setuptools
requires = [
"flake8 > 3.0.0",
]
setuptools.setup(
name="flake8_example",
license="MIT",
version="0.1.0",
description="our extension to flake8",
author="Me",
author_email="example#example.com",
url="https://gitlab.com/me/flake8_example",
packages=[
"flake8_example",
],
install_requires=requires,
entry_points={
'flake8.extension': [
'N8 = flake8_example:Example',
],
},
classifiers=[
"Framework :: Flake8",
"Environment :: Console",
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python",
"Programming Language :: Python :: 2",
"Programming Language :: Python :: 3",
"Topic :: Software Development :: Libraries :: Python Modules",
"Topic :: Software Development :: Quality Assurance",
],
)
flake8_example.py
from flake8.formatting import base
class Example(base.BaseFormatter):
"""Flake8's example formatter."""
def format(self, error):
return 'Example formatter: {0!r}'.format(error)
I setup it up by running pip install --editable .
Then to test it out I ran flake8 --format=example main.py
It throws this error:
flake8.exceptions.FailedToLoadPlugin: Flake8 failed to load plugin "N8" due to 'module' object has no attribute 'Example'.
So if you're trying to write a formatter you need to read the documentation a bit more closely. In the registering section of the documentation it says:
Flake8 presently looks at three groups:
flake8.extension
flake8.listen
flake8.report
If your plugin is one that adds checks to Flake8, you will use
flake8.extension. If your plugin automatically fixes errors in code,
you will use flake8.listen. Finally, if your plugin performs extra
report handling (formatting, filtering, etc.) it will use
flake8.report.
(Emphasis mine.)
That means your setup.py should look like this:
entry_points = {
'flake8.report': [
'example = flake8_example:Example',
],
}
If your setup.py properly installs your package then running
flake8 --format=example ...
Should work just fine. The exception you're seeing, however, is due to the module either not having a class named Example. You should investigate whether packages will pick up a single file module or if you need to restructure your plugin so that it looks like:
flake8_example/
__init__.py
...
As that may be why your setup.py is not working appropriately.

pip missing dependencies in setup.py

I'm trying to use pip to get an environment set up, and running pip install -e ./ from the root directory of my project isn't picking everything up. I've got a setup.py file with a requires section that looks like so:
requires = [
'phonenumbers',
'inflect',
'repoze.sendmail==4.1',
'pyramid',
'pyramid_chameleon',
'pyramid_debugtoolbar',
'pyramid_mailer',
'pyramid_tm',
'transaction',
'zope.sqlalchemy',
'waitress',
'pyramid_beaker',
'cryptacular',
'pycrypto',
'webtest',
'alembic',
'psycopg2',
'python-dateutil',
'sqlalchemy-utils',
'cryptacular',
'arrow',
'jsonpickle',
'sqlalchemy',
'pyramid_storage',
'boto',
'requests'
]
When the command is run, some libraries, such as boto, won't be installed. Does anyone know why these packages would be missed?
Edit: Here's the call to setup in setup.py, with some irrelevent bits ommited:
dependency_links = [
'git+https://github.com/benthor/inflect.py#egg=inflect',
]
setup(
classifiers=[
"Programming Language :: Python",
"Framework :: Pyramid",
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
],
author='',
author_email='',
url='',
keywords='web wsgi bfg pylons pyramid',
packages=find_packages(),
include_package_data=True,
zip_safe=False,
test_suite='test',
install_requires=requires,
dependency_links=dependency_links
)
The requires argument to setup() doesn't actually do anything, and for all intents and purposes should be considered deprecated and useless.
Use install_requires instead.

How to specify install location of additional files during the creation of module

I have published a module to pypi called 'surrealism' that generates surreal sentences and error messages. It contains a SQLite3 database containing all of the words and sentences needed for my module.
All of the following install methods work fine:
python setup.py install
pip install surrealism
easy_install surrealism
and the module works fine.
However, when installing into a virtualenv, things go wrong. surrealism.py get installed into C:\Users\me\virtualenvs\surrealism\Lib\site-packages, but surrealism.sqlite doesn't get installed?
If I run python and try and import the module, my module creates a new sqlite3 database called surrealism.sqlite at C:\Users\me\virtualenvs\surrealism
The contents of my setup.py follows:
#!/usr/bin/env python
from setuptools import setup
long_desc = open('readme.rst').read()
setup(name = 'surrealism',
version = '0.5.2',
py_modules = ['surrealism'],
author = 'Morrolan',
author_email = 'morrolan#icloud.com',
url = 'https://github.com/Morrolan/surrealism',
license = 'GNU General Public License (GPL)',
description = 'Surreal sentence and error message generator.',
long_description = long_desc,
platforms = ['Windows','Unix','OS X'],
download_url = "https://pypi.python.org/pypi/surrealism/",
keywords = ["surreal", "surrealism", "error message"],
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 2.6",
"Programming Language :: Python :: 2.7",
"Programming Language :: Python :: 3.3",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"Intended Audience :: Education",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Operating System :: OS Independent",
"Topic :: Education",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=['setuptools'],
)
In surrealism.py, I reference/connect to the SQLite3 database in a fairly rudimentary way:
CONN = sqlite3.connect('surrealism.sqlite')
But so far it hasn't caused me any problems.
is there a more explicit way to reference surrealism.sqlite, or do I have to specify something in setup.py to force the installation?
Kind Regards,
Morrolan
the crucial problem is exactly the way you are connecting to your sqlite database; That will refer to a file in the current directory;, anywhere the program that invokes it is trying to run. What you want to say is
... sqlite3.connect(where_this_python_lib_is_installed + '...sqlite')
So that it doesn't matter where it's installed. There is a fairly standard way to do that, using the pkg_resources library. Since we're trying to discover a sqlite database, that means we need a real file on disk, not a string or file-like object; so the right method to use here pkg_resources.resource_filename, we just need to change the connect call to:
from pkg_resources import resource_filename
CONN = sqlite3.connect(resource_filename(__name__, 'surrealism.sqlite'))
But wait... That only works if the package data is in a package, but you currently have a module. Not a big problem, though; we'll rename surrealism.py to surrealism/__init__.py, surrealism.sqlite to surrealism/surrealism.sqlite, and make the appropriate changes in MANIFEST.in. We'll also need to tell setuptools about this. Change py_modules=["surrealism"], in your setup.py to packages=["surrealism"].
Almost there, The last thing we need to do is get setuptools to actually install that file from source. The first is pretty obvious, we need to tell it which files to copy; Add
package_data={'surrealism': ['surrealism.sqlite']},
To your setup.py, the second change is more subtle. In most cases, setuptools tries to install packages as zip files. This is usually a good thing; but in our case, we need to pass the filename of a real file to sqlite.connect, so we have to tell it not to try to zip the package. For that, just add
zip_safe=False,
To your setup.py.

Categories