python-module

Python: sharing common code among a family of scripts

痴心易碎 提交于 2019-12-03 18:35:42
问题 I'm writing a family of Python scripts within a project; each script is within a subdirectory of the project, like so: projectroot | |- subproject1 | | | |- script1.main.py | `- script1.merger.py | |- subproject2 | | | |- script2.main.py | |- script2.matcher.py | `- script2.merger.py | `- subproject3 | |- script3.main.py |- script3.converter.py |- script3.matcher.py `- script3.merger.py Now several of the scripts share some code. The shared code is best considered part of the project itself,

How to import two versions of the same python module at the same time?

萝らか妹 提交于 2019-12-03 16:16:22
Suppose I have two versions of a python package, say "lib". One is in folder ~/version1/lib and the other is in ~/version2/lib . I'm trying to load both packages in one session by doing this: sys.path.insert(0, '~/version1') import lib as a sys.path.insert(0, '~/version2') import lib as b But it doesn't work, because of cache, b will be the same as a . Is there anyway to do it? Maybe use hook in sys.meta_path ? I didn't figure it out. Or is there anyway to delete cache of imported modules? You can consider using python virtual environment . Activate the required environment and run your code

What is the difference between `sys.meta_path` and `sys.path_hooks` importer objects?

会有一股神秘感。 提交于 2019-12-03 15:48:00
Using importlib , what is the difference between "Meta Path Finder" (found by traversing over sys.meta_path) and Path Entry Finder" (found by traversing over sys.path_hooks)? The first type is called upon begin of an import, but when is the second type used? Do both return a spec object? I want to implement a customized import, where a module can be imported from sources other than *.py or *.pyc, e.g. from a stream. How can this be done? Mr_and_Mrs_D sys.path_hooks returns a finder factory . Path hooks are called as part of sys.path (or package.__path__ ) processing as we read in PEP 302

python local modules

痴心易碎 提交于 2019-12-03 15:08:27
I have several project directories and want to have libraries/modules that are specific to them. For instance, I might have a directory structure like such: myproject/ mymodules/ __init__.py myfunctions.py myreports/ mycode.py Assuming there is a function called add in myfunctions.py , I can call it from mycode.py with the most naive routine: execfile('../mymodules/myfunctions.py') add(1,2) But to be more sophisticated about it, I can also do import sys sys.path.append('../mymodules') import myfunctions myfunctions.add(1,2) Is this the most idiomatic way to do this? There is also some mention

Relative importing python module from a subfolder from a different subfolder

。_饼干妹妹 提交于 2019-12-03 14:21:41
I'm trying to use alembic which is an sqlalchemy tool in python. You type a command and it generates a folder "alembic" with py files inside. The py file inside, needs to link to my application in a separate folder called "myapp". But I cannot link it. It says it doesn't exist and relative import doesn't work. so I need to import my config class from myapp/configs/config.py file. /apps +--/alembic |----env.py <--- the calling file +--/myapp |----configs/__init__.py <--- has "DefaultConfig" class imported |----configs/config.py <--- I want to import the class inside here. inside env.py: from

pip: inconsistent permissions issues

社会主义新天地 提交于 2019-12-03 10:20:13
When installing a package via sudo pip-python (CentOS 6 package: python-pip-0.8-1.el6.noarch ), I sometimes get permission issues with the installed packages being readable only by root. Re-installing again one or two times usually fixes the problem. Has anyone experienced this? Or can anyone suggest any troubleshooting steps to nail down the cause? When you run a command using sudo , it will preserve the users umask . pip just installs files, it doesn't change access rights, so you'll end up with the files having the access rights set conforming to the current user's umask, which may be owner

pyserial for Python 2.7.2

假如想象 提交于 2019-12-03 08:39:43
问题 I'm new to Python. According to the internets I was looking for the module pyserial after receiving this error: ImportError: No module named serial I first tried to install pywin32, it went well. But it seems not to contain pyserial. :-( Then I found a single module installer for pyserial, I was not able to install it, it says it did not found the path to python in the registry. :-( After that I found this module on python.org, but I don't know what to do, it does not come with an installer.

Easiest way to automatically download required modules in Python?

守給你的承諾、 提交于 2019-12-03 05:44:12
问题 I would like to release a python module I wrote which depends on several packages. What's the easiest way to make it so these packages are programmatically downloaded just in case they are not available on the system that's being run? Most of these modules should be available by easy_install or pip or something like that. I simply want to avoid having the user install each module separately. thanks. 回答1: pip uses requirements files, which have a very straightforward format. For more Python

Mapping module imports in Python for easy refactoring

我的未来我决定 提交于 2019-12-03 05:26:21
I have a bunch of Python modules I want to clean up, reorganize and refactor (there's some duplicate code, some unused code ...), and I'm wondering if there's a tool to make a map of which module uses which other module. Ideally, I'd like a map like this: main.py -> task_runner.py -> task_utils.py -> deserialization.py -> file_utils.py -> server.py -> (deserialization.py) -> db_access.py checkup_script.py re_test.py main_bkp0.py unit_tests.py ... so that I could tell which files I can start moving around first (file_utils.py, db_access.py), which files are not used by my main.py and so could

Difference between Module and Class in Python

旧街凉风 提交于 2019-12-03 04:15:23
问题 Can I assign value to a variable in the module. If yes, what is the difference between a class and module. PS: I'm a java guy. In case if its helps in way of explaining. Thanks. 回答1: Module : A module is a file containing Python definitions and statements. As the doc say. So a module in python is simply a way to organize the code, and it contains either python classes or just functions. If you need those classes or functions in your project, you just import them. For instance, the math module