python-import

Yet another ImportError: attempted relative import with no known parent package

筅森魡賤 提交于 2021-01-18 05:24:24
问题 I have the following directory structure: py_test ├── __init__.py ├── dir1 │ ├── __init__.py │ └── script1.py └── dir2 ├── __init__.py └── script2.py In script2 I want to " import ..\script1 ". What I tried in script2 : Does not work from ..dir1 import script1 ImportError: attempted relative import with no known parent package` Works import sys, os path2add = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, 'dir1'))) if (not (path2add in sys.path)) :

Importing all functions from a package: “from .* import *”

我的梦境 提交于 2020-12-29 08:53:04
问题 Goal I want to be able to import (on the __init__.py ) all functions from every single file inside my package. Usage For example in this folder structure. manage.py - scripts/ -- __init__.py -- tests.py -- deploy.py I am currently doing the following: manage.py: from scripts import * script/ init .py: from .tests import * from .deploy import * But, every time I add another file to the package I have to add an import line on script/__init__.py , which is kind of annoying. 回答1: importlib allows

Importing all functions from a package: “from .* import *”

可紊 提交于 2020-12-29 08:49:21
问题 Goal I want to be able to import (on the __init__.py ) all functions from every single file inside my package. Usage For example in this folder structure. manage.py - scripts/ -- __init__.py -- tests.py -- deploy.py I am currently doing the following: manage.py: from scripts import * script/ init .py: from .tests import * from .deploy import * But, every time I add another file to the package I have to add an import line on script/__init__.py , which is kind of annoying. 回答1: importlib allows

How to locally develop a python package?

我怕爱的太早我们不能终老 提交于 2020-12-27 07:54:05
问题 I'm trying to make changes to an existing python module, and then test it locally. What's the best way to do this? I cloned the github module and made changes, but I'm not sure how to import the local package instead of the already installed one. 回答1: The easiest way to do such testing would be to create a virtual environment, and then installing the package in development mode. Assuming you are on Linux it would look something like this. $ virtualenv dev_env $ source dev_env/bin/activate $

How to locally develop a python package?

走远了吗. 提交于 2020-12-27 07:54:02
问题 I'm trying to make changes to an existing python module, and then test it locally. What's the best way to do this? I cloned the github module and made changes, but I'm not sure how to import the local package instead of the already installed one. 回答1: The easiest way to do such testing would be to create a virtual environment, and then installing the package in development mode. Assuming you are on Linux it would look something like this. $ virtualenv dev_env $ source dev_env/bin/activate $

Dynamically import module from memory in Python 3 using Hooks

混江龙づ霸主 提交于 2020-12-15 00:48:16
问题 What I want to achieve is exactly what this this answer proposes, however in Python 3. The code below works fine in Python 2: import sys import imp modules = { "my_module": """class Test: def __init__(self): self.x = 5 def print_number(self): print self.x"""} class StringImporter(object): def __init__(self, modules): self._modules = dict(modules) def find_module(self, fullname, path): if fullname in self._modules.keys(): return self return None def load_module(self, fullname): if not fullname

Transitive import error: ModuleNotFoundError: No module named '…'

拟墨画扇 提交于 2020-12-09 18:37:41
问题 I'm confused now. Here is the project tree: project - source - - lib - - - __init__.py - - - utils.py - - - stats.py - test - - lib - - - test_stats.py stats.py has import utils , which indeed works if one executes stats.py itself. Now test_stats.py has import lib.stats but that results in the ModuleNotFoundError: No module named 'utils' error if executed as PYTHONPATH=source pytest in the project directory: ==================================== ERRORS ==================================== ____

Importing variables from another file in Python

痴心易碎 提交于 2020-12-08 06:23:52
问题 I have declared a few variables and initialised them with some value in variables.py : flag = 0 j = 1 I want to use those values in another file main_file.py : import variables if(flag == 0) : j = j+1 However I get the following error, NameError: name 'flag' is not defined How can i solve this problem? 回答1: Everything you've done is right except when using the variables. In your main_file.py file: if(variables.flag == 0) : variables.j = variables.j + 1 (Or) Use the following header : from

Why customer python package can not be imported?

☆樱花仙子☆ 提交于 2020-12-06 04:45:20
问题 I created my own python package named jjnsegutils and upload it to Pypi website. But after I successfully install it by pip install jjnsegutils , I still can not import it. Error shows: ModuleNotFoundError: No module named 'jjnsegutils' . Details about the whole procedure are as follows. Package structure and details The structure of my package is: jjnsequtils ├─ __init__.py ├─ myutil ├─ __init__.py ├─ myutil.py ├─ LICENSE ├─ README.md ├─ setup.py Two __init__.py files are empty. In my setup

Import issue for setproctitle on Mac OS,

℡╲_俬逩灬. 提交于 2020-12-06 02:58:09
问题 In python, If I try to import setproctitle I get the following import error: ImportError: dlopen(/Users/xxx/.local/share/virtualenvs/airflow_gg-F_Vv1Po_/lib/python3.7/site-packages/setproctitle.cpython-37m-darwin.so, 2): Symbol not found: _Py_GetArgcArgv Referenced from: /Users/xxx/.local/share/virtualenvs/airflow_gg-F_Vv1Po_/lib/python3.7/site-packages/setproctitle.cpython-37m-darwin.so Expected in: flat namespace in /Users/xxx/.local/share/virtualenvs/airflow_gg-F_Vv1Po_/lib/python3.7/site