python-packaging

How to resolve “ValueError: attempted relative import beyond top-level package”

左心房为你撑大大i 提交于 2019-11-29 12:52:45
I have the following problem with my project, help me please! Here is the structure of my package: /pkg /pkg/__init__.py /pkg/sub1/__init__.py /pkg/sub2/__init__.py /pkg/sub1/foo1.py /pkg/sub2/foo2.py Here is implementation of foo1.py: from ..sub2 import foo2 def f(): print("Hello!") When I run foo1 I get error: ValueError: attempted relative import beyond top-level package . I can solve it doing the following adjustment: import sys import os sys.path.append(os.path.abspath(os.path.pardir)) from sub2 import foo2 def f(): print("Hello!") But I wonder if there is a way to do it without importing

Multilevel relative import

試著忘記壹切 提交于 2019-11-29 11:21:40
问题 Multilevel relative import I have following folder structure top\ __init__.py util\ __init__.py utiltest.py foo\ __init__.py foo.py bar\ __init__.py foobar.py I want to access from foobar.py the module utiltest.py . I tried following relative import, but this doesn't work: from ...util.utiltest import * I always get ValueError: Attempted relative import beyond toplevel package How to do such a multileve relative import? 回答1: You must import foobar from the parent folder of top : import top

Installing nltk data dependencies in setup.py script

耗尽温柔 提交于 2019-11-29 06:14:40
I use NLTK with wordnet in my project. I did the installation manually on my PC, with pip: pip3 install nltk --user in a terminal, then nltk.download() in a python shell to download wordnet. I want to automatize these with a setup.py file, but I don't know a good way to install wordnet. For the moment, I have this piece of code after the call to setup ( "nltk" is in the install_requires list of the call to setup ): import sys if 'install' in sys.argv: import nltk nltk.download("wordnet") Is there a better way to do this? asmaier I managed to install the NLTK data in setup.py by overriding

Importing from a Package in IDLE vs Shell

落花浮王杯 提交于 2019-11-28 14:28:10
Importing a whole package works in IDLE, but not in shell. The following works fine in IDLE: import tkinter as tk tk.filedialog.askopenfilename() In shell, I get this error: AttributeError: 'module' object has no attribute 'filedialog' I understand that I have to import tkinter.filedialog to make this work in shell. Why the difference between IDLE and shell? How can I make IDLE act like shell? It can be frustrating to have a script working in IDLE, and failing in shell. I am using Python 3.4. This is an IDLE bug which I fixed for future 3.5.3 and 3.6.0a4 releases. Tracker issue. For an

Prevent package from being installed on old Python versions

一笑奈何 提交于 2019-11-28 13:18:33
What can we put in a setup.py file to prevent pip from collecting and attempting to install a package when using an unsupported Python version? For example magicstack is a project listed with the trove classifier: Programming Language :: Python :: 3 :: Only So I expect the following behaviour if pip --version is tied to python 2.7: $ pip install magicstack Collecting magicstack Could not find a version that satisfies the requirement magicstack (from versions: ) No matching distribution found for magicstack But the actual behavior is that pip collects a release, downloads it, attempts to

Can't install textract on windows

狂风中的少年 提交于 2019-11-28 00:07:46
I've tried lots of things but still fail when I'm trying to install textract package on my Windows by using pip command. I'm getting the following error: I have no idea what to do, so I'll be really grateful for any advice. Thank you Stolen from here : Needed to first install swig from conda (miniconda) conda install swig Then downloaded the EbookLib 0.15 zip from the releases https://github.com/aerkalov/ebooklib/releases After unzipping it, I manually removed (I used notepad++) the unicode char in the README.md file. (unicode char is on Line 44) And then installed the module with pip. cd to

Importing from a Package in IDLE vs Shell

泄露秘密 提交于 2019-11-27 08:37:28
问题 Importing a whole package works in IDLE, but not in shell. The following works fine in IDLE: import tkinter as tk tk.filedialog.askopenfilename() In shell, I get this error: AttributeError: 'module' object has no attribute 'filedialog' I understand that I have to import tkinter.filedialog to make this work in shell. Why the difference between IDLE and shell? How can I make IDLE act like shell? It can be frustrating to have a script working in IDLE, and failing in shell. I am using Python 3.4.

Prevent package from being installed on old Python versions

蓝咒 提交于 2019-11-27 07:36:04
问题 What can we put in a setup.py file to prevent pip from collecting and attempting to install a package when using an unsupported Python version? For example magicstack is a project listed with the trove classifier: Programming Language :: Python :: 3 :: Only So I expect the following behaviour if pip --version is tied to python 2.7: $ pip install magicstack Collecting magicstack Could not find a version that satisfies the requirement magicstack (from versions: ) No matching distribution found

Can't install textract on windows

霸气de小男生 提交于 2019-11-27 04:42:24
问题 I've tried lots of things but still fail when I'm trying to install textract package on my Windows by using pip command. I'm getting the following error: I have no idea what to do, so I'll be really grateful for any advice. Thank you 回答1: Stolen from here: Needed to first install swig from conda (miniconda) conda install swig Then downloaded the EbookLib 0.15 zip from the releases https://github.com/aerkalov/ebooklib/releases After unzipping it, I manually removed (I used notepad++) the

How to accomplish relative import in python

柔情痞子 提交于 2019-11-26 22:23:20
问题 stuff/ __init__.py mylib.py Foo/ __init__.py main.py foo/ __init__.py script.py script.py wants to import mylib.py This is just an example, but really I just want to do a relative import of a module in a parent directory. I've tried various things and get this error... Attempted relative import beyond toplevel package I read somewhere that the script from where the program starts shouldn't in the package, and I tried modifying the structure for that like so... stuff/ mylib.py foo.py //