python-module

Python: 'Private' module in a package

梦想与她 提交于 2019-11-27 20:02:02
I have a package mypack with modules mod_a and mod_b in it. I intend the the package itself and mod_a to be imported freely: import mypack import mypack.mod_a However, I'd like to keep mod_b for the exclusive use of mypack . That's because it exists merely to organize the latter's internal code. My first question is, is it an accepted practice in Python programming to have 'private' modules like this? If yes, my second question is, what is the best way to convey this intention to the client? Do I prefix the name with an underscore (i.e. _mod_b )? Or would it be a good idea to declare a sub

Python: OSError: [Errno 2] No such file or directory: ''

随声附和 提交于 2019-11-27 19:49:56
I have a 100 lines, 3 years old python scraper that now bug. Starting lines are: import urllib, re, os, sys, time # line 1: import modules os.chdir(os.path.dirname(sys.argv[0])) # line 2: all works in script's folder > relative address # (rest of my script here!) When run, $cd /my/folder/ $python script.py I receive the error: python script.py Traceback (most recent call last): File "script.py", line 2, in <module> os.chdir(os.path.dirname(sys.argv[0])) OSError: [Errno 2] No such file or directory: '' How should I read this error and what to do ? Have you noticed that you don't get the error

Is it possible to list all functions in a module? [duplicate]

本秂侑毒 提交于 2019-11-27 18:44:55
This question already has an answer here: How to list all functions in a Python module? 14 answers I defined a .py file in this format: foo.py def foo1(): pass def foo2(): pass def foo3(): pass I import it from another file: main.py from foo import * # or import foo Is it possible list all functions name, e.g. ["foo1", "foo2", "foo3"] ? Thanks for your help, I made a class for what I want, pls comment if you have suggestion class GetFuncViaStr(object): def __init__(self): d = {} import foo for y in [getattr(foo, x) for x in dir(foo)]: if callable(y): d[y.__name__] = y def __getattr__(self, val

Import python module NOT on path

醉酒当歌 提交于 2019-11-27 18:06:39
I have a module foo, containing util.py and bar.py. I want to import it in IDLE or python session. How do I go about this? I could find no documentation on how to import modules not in the current directory or the default python PATH. After trying import "<full path>/foo/util.py" , and from "<full path>" import util The closest I could get was import imp imp.load_source('foo.util','C:/.../dir/dir2/foo') Which gave me Permission denied on windows 7. One way is to simply amend your path : import sys sys.path.append('C:/full/path') from foo import util,bar Note that this requires foo to be a

How to properly use relative or absolute imports in Python modules?

不问归期 提交于 2019-11-27 12:05:39
Usage of relative imports in Python has one drawback, you will not be able to run the modules as standalones anymore because you will get an exception: ValueError: Attempted relative import in non-package # /test.py: just a sample file importing foo module import foo ... # /foo/foo.py: from . import bar ... if __name__ == "__main__": pass # /foo/bar.py: a submodule of foo, used by foo.py from . import foo ... if __name__ == "__main__": pass How should I modify the sample code in order to be able to execute all: test.py , foo.py and bar.py I'm looking for a solution that works with python 2.6+

How to reload python module imported using `from module import *`

ⅰ亾dé卋堺 提交于 2019-11-27 11:39:37
I saw in this useful Q&A that one can use reload(whatever_module) or, in Python 3, imp.reload(whatever_module) . My question is, what if I had said from whatever_module import * to import? Then I have no whatever_module to refer to when I use reload() . Are you guys gonna yell at me for throwing a whole module into the global namespace? :) Catskul I agree with the "don't do this generally" consensus, but... The correct answer is: import X reload(X) from X import Y # or * for that matter Never use import * ; it destroys readability. Also, be aware that reloading modules is almost never useful.

How to get filename of the __main__ module in Python?

浪子不回头ぞ 提交于 2019-11-27 11:17:47
Suppose I have two modules: a.py: import b print __name__, __file__ b.py: print __name__, __file__ I run the "a.py" file. This prints: b C:\path\to\code\b.py __main__ C:\path\to\code\a.py Question : how do I obtain the path to the __main__ module ("a.py" in this case) from within the "b.py" library? import __main__ print __main__.__file__ Perhaps this will do the trick: import sys from os import path print path.abspath(sys.modules['__main__'].__file__) Note that, for safety, you should check whether the __main__ module has a __file__ attribute. If it's dynamically created, or is just being run

How to use non-installable modules from DAG code?

試著忘記壹切 提交于 2019-11-27 08:26:29
问题 I have a Git repository which (among other things) holds Airflow DAGs in airflow directory. I have a clone of the repository besides an install directory of Airflow. airflow directory in Git is pointed to by AIRFLOW_HOME configuration variable. I would like to allow imports from modules in the repository that are listed outside airflow folder (please see the structure below). <repo root> |_airflow |_dags |_dag.py |_module1 |_module2 |_... So that in dag.py I can do: from module1 import

How to make global imports from a function?

大兔子大兔子 提交于 2019-11-27 07:44:47
I fear that this is a messy way to approach the problem but... let's say that I want to make some imports in Python based on some conditions. For this reason I want to write a function: def conditional_import_modules(test): if test == 'foo': import onemodule, anothermodule elif test == 'bar': import thirdmodule, and_another_module else: import all_the_other_modules Now how can I have the imported modules globally available? For example: conditional_import_modules(test='bar') thirdmodule.myfunction() Imported modules are just variables - names bound to some values. So all you need is to import

A Python module and package loading confusion

我只是一个虾纸丫 提交于 2019-11-27 07:23:55
问题 Let's say I have something like this: . ├── run.py └── test ├── __init__.py ├── models │ ├── foo │ │ ├── baby.py │ │ └── __init__.py │ ├── __init__.py │ └── user.py └── start.py run.py from test import start start.py from .models import user user.py from . import foo print(foo.baby.Baby) baby.py Baby = "I am a baby" Now, when you run the run.py file... >>> python run.py ... traceback ... AttributeError: 'module' object has no attribute 'baby' But, if you change the start.py like this: from