python-module

Prevent Python from caching the imported modules

旧巷老猫 提交于 2019-12-17 06:38:37
问题 While developing a largeish project (split in several files and folders) in Python with IPython, I run into the trouble of cached imported modules. The problem is that instructions import module only reads the module once, even if that module has changed! So each time I change something in my package, I have to quit and restart IPython. Painful. Is there any way to properly force reloading some modules? Or, better, to somehow prevent Python from caching them? I tried several approaches, but

How to write a Python module/package?

爷,独闯天下 提交于 2019-12-17 01:18:12
问题 I've been making Python scripts for simple tasks at work and never really bothered packaging them for others to use. Now I have been assigned to make a Python wrapper for a REST API. I have absolutely no idea on how to start and I need help. What I have: (Just want to be specific as possible) I have the virtualenv ready, it's also up in github, the .gitignore file for python is there as well, plus, the requests library for interacting with the REST API. That's it. Here's the current directory

IOError in imported python module

99封情书 提交于 2019-12-13 04:37:30
问题 I have a module with the following folder structure Module -__init__.py -analyzer.py -lib/ -lib/models -lib/data/ However when used from the parent directory I get an IOError for a file used in analyzer.py which is in lib/models. How can I get this fixed without copying the models and data to the parent directory 回答1: Every Python module must have own __init__.py file: Module -__init__.py -analyzer.py -lib/ -lib/__init__.py -lib/models/ -lib/models/__init__.py -lib/data/ -lib/data/__init__.py

Backward compatibility of Python 3.5 for external modules

…衆ロ難τιáo~ 提交于 2019-12-12 19:16:52
问题 I have built a Python C++ module based on Python 3.4.3. Later I have installed Python 3.5, and tried to load my Python module. When I tried to do so, I got the following error: ImportError: Module use of python34.dll conflicts with this version of Python. I try to import my module by running Python from the command prompt. So I wonder: Is there no backward compatibility in Python 3.5 for modules that were built with previous versions? Must I build my module again with Python 3.5? 回答1: By

Use case for “import as” in Python

百般思念 提交于 2019-12-12 16:09:41
问题 I am wondering if we can use " import as " for creating relatively compact or readable code. I am aware of its usual use cases based on PEP such as to void name clashes. Here is the situation (keeping it very simple for the demonstration purpose). Let's say I have a module name, process_words.py. process_words.py: def word_to_lower(word): return word.lower process_article.py (lets the main script): import process_words word = "HELLO" word_lower = process_words.word_to_lower(word) print(word

pytest module import ImportError: No module named

最后都变了- 提交于 2019-12-12 15:27:42
问题 This question has been asked DOZENS of times before, but every one I've come across doesn't have any working solutions for me. I'm using Python 2.7 and pytest to run tests. Structure is as follows (from the root of my project/git repo): myapp/ myapp/ __init__.py # def main(): # ... stuff # if __name__ == "__main__": # main() __main__.py # import __init__ as myapp # myapp.main() config.yaml # (app config file) myapplib/ __init__.py # (Empty file) config.py # Loads {projectroot}/config.yaml #

Sublime Text 2 add module for PlugIn (MySQLdb)

被刻印的时光 ゝ 提交于 2019-12-12 14:33:16
问题 Im developing a small plugin for Sublime Text 2, but i need to use MySQLdb module. The MySQLdb module works fine from my normal CLI. But SB2 uses its own version of Python. Any idea how i can install the MySQKdb module and where so SB2 can find it? Python installation used /System/Library/Frameworks/Python.framework/Versions/2.7/ Thanks! 回答1: I was working on a plug-in today, and the solution I chose was to place the module within my package directory. In otherwords: Packages | MyPackage | |

Why do python modules work in shell but not in the script?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 12:16:37
问题 I am trying to make two programs. I want one to print the current weather of my city of residence and I want the other one to take data from an online account and return it. For those scripts I import the yweather module and the requests module. When I import them in the shell there are no problems but when I run the script it says "ImportError: No module named yweather". What am I doing wrong? Shell: >>> import requests >>> Script: Traceback (most recent call last): File "/Users/tim/Desktop

python local modules

余生长醉 提交于 2019-12-12 07:58:28
问题 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

Exclude __builtin__ module

匆匆过客 提交于 2019-12-12 06:38:46
问题 The __builtin__ module in Python clutters a developers namespace with lots of functions and classes that have very generic names (e.g. max , sum , id , hash etc.) that often get in the way of naming variables and when outside of a context-aware IDE one can accidentally overwrite a name without noticing. Is there a way to stop this module from being implicitly accessed from a certain file and require explicit imports instead? Something along the lines of: from __builtins__ import hash as