python-import

Python -m switch to run module inside package

戏子无情 提交于 2020-01-24 19:36:26
问题 I am reading this article. I am wondering why the below statement from the article is correct? As I can use the solution 1 - that is - python -m packA.a2 and it works fine. To give you some context, Solution 1 says: Use absolute imports rooted at the test/ directory (i.e. middle column in the table above). This guarantees that running start.py directly will always work. In order to run a2.py directly, run it as an imported module instead of as a script: change directories to test/ in the

Import image in python

一曲冷凌霜 提交于 2020-01-24 10:22:40
问题 hello I am a beginner in python and I have problems executing my code . how can i fix this error with python: import cgitb cgitb.enable() print('Content-type: text/html\r\n') print('\r\n') import Image, ImageDraw import sys import math, random from itertools import product from ufarray import * ModuleNotFoundError: No module named 'Image' args = ("No module named 'Image'",) msg = "No module named 'Image'" name = 'Image' path = None with_traceback = <built-in method with_traceback of

Python - fails importing package

喜夏-厌秋 提交于 2020-01-23 17:18:47
问题 I have trouble importing package. My file structure is like this: filelib/ __init__.py converters/ __init__.py cmp2locus.py modelmaker/ __init__.py command_file.py In module command_file.py I have a class named CommandFile which i want to call in the cmp2locus.py module. I have tried the following in cmp2locus.py module: import filelib.modelmaker.command_file import modelmaker.command_file from filelib.modelmaker.command_file import CommandFile All these options return ImportError: No modules

Getting “ImportError: attempted relative import with no known parent package” when running from Python Interpreter

狂风中的少年 提交于 2020-01-22 17:35:46
问题 I'm creating a modular app using Flask blueprints feature. As a result, my directory structure is like this: project __init__.py config.py mould.py modules __init__.py core __init__.py core.py db.py models.py The modules directory here is not be confused with Python modules, they are for giving a modular structure to my project (core module, foo module, bar module, etc.). Now each folder in the modules directory (and a module inside it with same name such as core.core ) is dynamically

Getting “ImportError: attempted relative import with no known parent package” when running from Python Interpreter

只愿长相守 提交于 2020-01-22 17:35:11
问题 I'm creating a modular app using Flask blueprints feature. As a result, my directory structure is like this: project __init__.py config.py mould.py modules __init__.py core __init__.py core.py db.py models.py The modules directory here is not be confused with Python modules, they are for giving a modular structure to my project (core module, foo module, bar module, etc.). Now each folder in the modules directory (and a module inside it with same name such as core.core ) is dynamically

Imports behave differently when in __init__.py that is imported

吃可爱长大的小学妹 提交于 2020-01-22 07:24:46
问题 Imports in an __init__.py seem to behave differently when the file is run, to when it is imported. If we have the following files: run.py : import test test/b.py : class B(object): pass test/__init__.py : from b import B print B print b If we run __init__.py we get an error as I expect: % python test/__init__.py <class 'b.B'> Traceback (most recent call last): File "test/__init__.py", line 6, in <module> print b NameError: name 'b' is not defined But if we run.py then we don't: % python run

UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to <undefined>

て烟熏妆下的殇ゞ 提交于 2020-01-21 04:55:27
问题 I am student doing my master thesis. As part of my thesis, I am working with python . I am reading a log file of .csv format and writing the extracted data to another .csv file in a well formatted way. However, when the file is read, I am getting this error: Traceback (most recent call last): File "C:\Users\SGADI\workspace\DAB_Trace\my_code\trace_parcer.py", line 19, in for row in reader: File "C:\Users\SGADI\Desktop\Python-32bit-3.4.3.2\python-3.4.3\lib\encodings\cp1252.py", line 23, in

Attempted relative import beyond toplevel package

半城伤御伤魂 提交于 2020-01-20 02:16:50
问题 Here is my folder structure: Mopy/ # no init.py ! bash/ __init__.py bash.py # <--- Edit: yep there is such a module too bass.py bosh/ __init__.py # contains from .. import bass bsa_files.py ... test_bash\ __init__.py # code below test_bosh\ __init__.py test_bsa_files.py In test_bash\__init__.py I have: import sys from os.path import dirname, abspath, join, sep mopy = dirname(dirname(abspath(__file__))) assert mopy.split(sep)[-1].lower() == 'mopy' sys.path.append(mopy) print 'Mopy folder

Attempted relative import beyond toplevel package

爷,独闯天下 提交于 2020-01-20 02:16:48
问题 Here is my folder structure: Mopy/ # no init.py ! bash/ __init__.py bash.py # <--- Edit: yep there is such a module too bass.py bosh/ __init__.py # contains from .. import bass bsa_files.py ... test_bash\ __init__.py # code below test_bosh\ __init__.py test_bsa_files.py In test_bash\__init__.py I have: import sys from os.path import dirname, abspath, join, sep mopy = dirname(dirname(abspath(__file__))) assert mopy.split(sep)[-1].lower() == 'mopy' sys.path.append(mopy) print 'Mopy folder

Attempted relative import beyond toplevel package

安稳与你 提交于 2020-01-20 02:16:00
问题 Here is my folder structure: Mopy/ # no init.py ! bash/ __init__.py bash.py # <--- Edit: yep there is such a module too bass.py bosh/ __init__.py # contains from .. import bass bsa_files.py ... test_bash\ __init__.py # code below test_bosh\ __init__.py test_bsa_files.py In test_bash\__init__.py I have: import sys from os.path import dirname, abspath, join, sep mopy = dirname(dirname(abspath(__file__))) assert mopy.split(sep)[-1].lower() == 'mopy' sys.path.append(mopy) print 'Mopy folder