python-module

Python Module Imports - Explicit vs Implicit Relative Imports

三世轮回 提交于 2019-12-05 00:13:24
Last night, when working on my mac, I set up some module imports in my __init__.py 's from MongoProvider import MongoProvider from Settings import Settings etc. I'm unsure of what version of Python is on that machine. I'll edit the question later with that info once I have it. Today, working on a different machine, which is Windows and using Python 3.3.3, my module imports were breaking. By adding an explicit relative import (adding a leading dot), I was able to fix the issue. from .MongoProvider import MongoProvider from .Settings import Settings The trace I'm receiving is: Traceback (most

Relative importing python module from a subfolder from a different subfolder

扶醉桌前 提交于 2019-12-04 20:06:34
问题 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"

Python internals - How do objects know about global variables?

不问归期 提交于 2019-12-04 16:23:49
I recently discovered some interesting behavior that left me wondering about how an object knows about which global variables exist. For example, suppose I have a file "test.py": globalVar = 1 toDelete = 2 class Test(object): classVar = 3 def runTest1(self): print globalVar print toDelete print missingVar def runTest2(self): print self.classVar print toCreate print missingVar Then in an interactive shell I do this: >>> import test >>> tester = test.Test() >>> tester.runTest1() 1 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "test.py", line 10, in runTest1 print

How to use sys.path_hooks for customized loading of modules?

廉价感情. 提交于 2019-12-04 11:34:56
I hope the following question is not too long. But otherwise I cannot explain by problem and what I want: Learned from How to use importlib to import modules from arbitrary sources? (my question of yesterday) I have written a specfic loader for a new file type (.xxx). (In fact the xxx is an encrypted version of a pyc to protect code from being stolen). I would like just to add an import hook for the new file type "xxx" without affecting the other types (.py, .pyc, .pyd) in any way. Now, the loader is ModuleLoader , inheriting from mportlib.machinery.SourcelessFileLoader . Using sys.path_hooks

Python import modules in another file

為{幸葍}努か 提交于 2019-12-04 09:11:46
I'm currently re-factoring a project (formerly big one file) into several seperate python files, each of which runs a specific part of my application. Eg, GUIthread.py runs the GUI, Computethread.py does some maths, etc etc. Each thread includes the use of functions from imported modules like math , time , numpy , etc etc. I already have a file globalClasses.py containing class definitions for my datatypes etc, which each .py file imports at the start, as per recomendation here: http://effbot.org/pyfaq/how-do-i-share-global-variables-across-modules.htm . This is working well. What I would like

Is there anything like Python export?

爱⌒轻易说出口 提交于 2019-12-04 04:18:57
We use all the time python's import mechanism to import modules and variables and other stuff..but, is there anything that works as export? like: we import stuff from a module: from abc import * so can we export like?: to xyz export * or export a,b,c to program.py I know this question isn't a typical type of question to be asked here..but just in curiosity..I checked over the python console and there is nothing that exists as 'export'..maybe it exists with some different name..? First, import the module you want to export stuff into, so you have a reference to it. Then assign the things you

Python import modules, folder structures

99封情书 提交于 2019-12-04 03:24:30
I have been looking for a way to solve this. I have a python project, and this is the folder structure I want: /project/main.py /project/src/models.py /project/test/tests.py I want to be able to run the tests by executing the tests.py in terminal. tests.py imports modules in /project/src/ for testing. First I solved this by adding sys.path.insert(0, '..') in tests.py. But then the paths used in models.py for opening text files had to be relative to the tests.py , etc. Which means the program wouldn't run when excecuted from main.py , cause of the paths. I also tried with dots when importing

Sphinx document module properties

陌路散爱 提交于 2019-12-04 01:50:18
I have a module that should have a @property , I solved this by setting a class as the module. I got the idea from this answer: Lazy module variables--can it be done? I wanted this to be repeatable and easy to use so I made a metaclass for it. This works like a charm. The problem is that when using Sphinx to generate documentation properties don't get documented. Everything else is documented as expected. I have no idea how to fix this, maybe this is a problem with Sphinx? The module: import sys import types class ClassAsModule(type): def __new__(cls, name, bases, attrs): # Make sure the name

Abort execution of a module in Python

夙愿已清 提交于 2019-12-04 00:52:11
问题 I'd like to stop evaluation of a module that is being imported, without stopping the whole program. Here's an example of what I want to achieve: main.py print('main1') import testmodule print('main2') testmodule.py print(' module1') some_condition=True if some_condition: exit_from_module() # How to do this? print(' module2') # This line is not executed. Expected output: main1 module1 main2 回答1: There is no good way to stop execution of a module. You can raise an exception, but then your

How to import custom python package by name

早过忘川 提交于 2019-12-03 22:22:47
问题 I have created a folder named "custom_module" and I have the __init__.py inside the folder which contains: __all__ = [ 'Submodule1', 'Submodule2' ] From what documentation I read I should be able to call import custom_module and get access to the package, however this isn't happening. How can I make python recognize my package? I am using python 3.2 Update: The package is not located in the python folder. How does the python environment find it, so I can successfully import it by name. 回答1: