python-import

Relative imports with unittest in Python

浪子不回头ぞ 提交于 2019-12-20 16:21:07
问题 I am trying to use Python unittest and relative imports, and I can't seem to figure it out. I know there are a lot of related questions, but none of them have helped so far. Sorry if this is repetitive, but I would really appreciate any help. I was trying to use the syntax from PEP 328 http://www.python.org/dev/peps/pep-0328/ but I must have something wrong. My directory structure is: project/ __init__.py main_program.py lib/ __init__.py lib_a lib_b tests/ __init__.py test_a test_b I run my

Python relative-import script two levels up

ぐ巨炮叔叔 提交于 2019-12-20 10:23:31
问题 I've been struggling with imports in my package for the last hour. I've got a directory structure like so: main_package | | __init__.py | folder_1 | | __init__.py | | folder_2 | | | __init__.py | | | script_a.py | | | script_b.py | | folder_3 | | __init__.py | | script_c.py I want to access code in script_b.py as well as code from script_c.py from script_a.py . How can I do this? If I put a simple import script_b inside script_a.py , when I run from main_package.folder_1.folder_2 import

Python relative-import script two levels up

流过昼夜 提交于 2019-12-20 10:20:03
问题 I've been struggling with imports in my package for the last hour. I've got a directory structure like so: main_package | | __init__.py | folder_1 | | __init__.py | | folder_2 | | | __init__.py | | | script_a.py | | | script_b.py | | folder_3 | | __init__.py | | script_c.py I want to access code in script_b.py as well as code from script_c.py from script_a.py . How can I do this? If I put a simple import script_b inside script_a.py , when I run from main_package.folder_1.folder_2 import

python import path: packages with the same name in different folders

时光毁灭记忆、已成空白 提交于 2019-12-20 09:33:37
问题 I am developing several Python projects for several customers at the same time. A simplified version of my project folder structure looks something like this: /path/ to/ projects/ cust1/ proj1/ pack1/ __init__.py mod1.py proj2/ pack2/ __init__.py mod2.py cust2/ proj3/ pack3/ __init__.py mod3.py When I for example want to use functionality from proj1 , I extend sys.path by /path/to/projects/cust1/proj1 (e.g. by setting PYTHONPATH or adding a .pth file to the site_packages folder or even

python import path: packages with the same name in different folders

感情迁移 提交于 2019-12-20 09:31:26
问题 I am developing several Python projects for several customers at the same time. A simplified version of my project folder structure looks something like this: /path/ to/ projects/ cust1/ proj1/ pack1/ __init__.py mod1.py proj2/ pack2/ __init__.py mod2.py cust2/ proj3/ pack3/ __init__.py mod3.py When I for example want to use functionality from proj1 , I extend sys.path by /path/to/projects/cust1/proj1 (e.g. by setting PYTHONPATH or adding a .pth file to the site_packages folder or even

module import: NameError: name is not defined

蹲街弑〆低调 提交于 2019-12-20 07:48:36
问题 How do I define the function in the importer so that it is visible inside imported? I tried this importer.py is def build(): print "building" build() import imported Whereby, imported.py is simply build() Yet, this fails building Traceback (most recent call last): File "C:\Users\valentin\Desktop\projects\maxim\miniGP\b01\evaluator\importer.py", line 6, in <module> import imported File "C:\Users\valentin\Desktop\projects\maxim\miniGP\b01\evaluator\imported.py", line 1, in <module> build()

Subpackages and relative imports in PyCharm

南楼画角 提交于 2019-12-20 02:09:22
问题 I am using python 2: python --version Python 2.7.13 :: Continuum Analytics, Inc. I have the following project structure: . └── foo ├── bar1 │ ├── __init__.py │ └── mod1.py ├── bar2 │ ├── __init__.py │ └── mod2.py ├── __init__.py └── start.py start.py from foo.bar2.mod2 import mod2_f mod2_f() mod1.py def mod1_f(): print "mod1_f" mod2.py from foo.bar1.mod1 import mod1_f def mod2_f(): mod1_f() print "mod2_f" If I run start.py from an IDE things work ok. However using something like this: python

Subpackages and relative imports in PyCharm

妖精的绣舞 提交于 2019-12-20 02:07:10
问题 I am using python 2: python --version Python 2.7.13 :: Continuum Analytics, Inc. I have the following project structure: . └── foo ├── bar1 │ ├── __init__.py │ └── mod1.py ├── bar2 │ ├── __init__.py │ └── mod2.py ├── __init__.py └── start.py start.py from foo.bar2.mod2 import mod2_f mod2_f() mod1.py def mod1_f(): print "mod1_f" mod2.py from foo.bar1.mod1 import mod1_f def mod2_f(): mod1_f() print "mod2_f" If I run start.py from an IDE things work ok. However using something like this: python

Why do you have to 'import' Python Standard Library functions? [closed]

a 夏天 提交于 2019-12-20 01:39:50
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 6 years ago . I'm new to Python coding and I'm coming from a PHP background. I'm curious why you have to 'import' functions at the top of you python script. In PHP you can simply use the function such as: sleep(10); The above would cause the script to sleep for 10 seconds. However, to do the

ImportError: cannot import name choice when importing sklearn.mixture

假装没事ソ 提交于 2019-12-19 18:27:48
问题 I am using scikit learn 0.15.0. When I try to import sklearn.mixture I get ImportError: cannot import name choice Any ideas? =================================================================== In [1]: **from sklearn import mixture** ImportError Traceback (most recent call last) <ipython-input-1-05bc76cab98d> in <module>() ----> 1 from sklearn import mixture /home/f/anaconda/lib/python2.7/site-packages/sklearn/mixture/__init__.py in <module>() 3 """ 4 ----> 5 from .gmm import sample_gaussian,