w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Python: I can't import a module even though it's in site-packages
I extracted the package and placed it in site-packages: No, that's the wrong way of "installing" a package. Python packages come with a setup.py script that should be used to install them. Simply do: python setup.py install And the module will be installed correctly in the site-packages of the python interpreter you are using. If you want to install it for a specific python version use python2/python3 instead of python.

Categories : Python

python packages -- overriding built-in packages and the dreaded "Module xxxx was already imported" warning
Rename myprogramxlrd to myprogramfrozen_xlrd. Then import it with try: import xlrd except ImportError: import frozen_xlrd as xlrd Alternatively, you could tell Python to silently ignore this particular UserWarning: import warnings warnings.filterwarnings("ignore", message="Module xlrd was already imported", category=UserWarning) Place this early on in the program, before scikits gets imported.

Categories : Python

Import everything in a Python module inside a function
The trick is to use sys._getframe to go up the call stack 1 step, then retrieve the globals of the caller. The precise code is, def load_contents(path): """Load contents of another configuration file""" module = imp.load_source('', path) glob = sys._getframe(1).f_globals # globals of calling module for k in dir(module): if not '__' in k: # ignore __xxx__ private variables glob[k] = getattr(module, k) Note that this code will only work with CPython, as sys._getframe is an implementation-specific function.

Categories : Python

How to import a single function to my main.py in python from another module?
Try this: from two.ClassB import your_function This grabs your function alone Here, if you call any other function or try to access any other variable of the classB, it will throw error. Example, i tried your case with the math module. This is what happened: >>> from math import sqrt >>> sqrt(5) 2.23606797749979 >>> log(10) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'log' is not defined >>> math.log(10) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'math' is not defined Note that no other function is imported or even the math module is not imported

Categories : Python

Python Import Statement and Recursion- need function available in module namespace
The easy answer is move foo() to foo.py and import from there or move it to create.py and import it from there into main.py - if there are things in main.py that it needs the move the too. Your other option is to pass foo from main into create as a function parameter where it is needed.

Categories : Python

Python: How to import sub-modules, from packages with the same name?
You need to have _init_.py files within those directories to make python treat then as a package instead of plain directories. Refer to this discussion to learn more about init.py files. Note:I have edited my previous answer by removing the irrelevant content based on the discussion with the poster of the query.

Categories : Python

importing system in python - import packages
import dir1 Imports dir1's __init__.py. You can access whatever is there using dir1.my_var_from_dir1_init. You cannot access the modules, only what's on dir1's __init__. from dir1 import * Imports the modules specified on the __all__ variable defined on dir1's __init__.py. If there isn't such variable, then it imports all of dir1's modules. You can access them directly, like main_code1.myvar. from dir1 import maincode Assuming it's a typo and you actually have a maincode module or class, it imports the maincode module/class from dir1. You can access it directly like mentioned above. Note that each option imports dir1's __init__.py, implicitly or explicitly. If you import the modules on __init__.py, then using import dir1 will allow you to use dir1.module. To import dir3's mainc

Categories : Python

Import python module over the internet/multiple protocols or dynamically create module
In principle, yes, but all of the tools built-in which kinda support this go through the filesystem. To do this, you're going to have to load the source from wherever, compile it with compile, and exec it with the __dict__ of a new module. See below. I have left the actually grabbing text from the internet, and parsing uris etc as an exercise for the reader (for beginners: I suggest using requests) In pep 302 terms, this would be the implementation behind a loader.load_module function (the parameters are different). See that document for details on how to integrate this with the import statement. import imp modulesource = 'a=1;b=2' #load from internet or wherever def makemodule(modulesource,sourcestr='http://some/url/or/whatever',modname=None): #if loading from the internet, you'd

Categories : Python

How to structure python packages without repeating top level name for import
Follow following structure : bagoftricks ── bagoftricks │ ├── __init__.py │ └── bagoftricks.py ├── README.md └── setup.py and then you should be able to use it as : from bagoftricks.bagoftricks import levenshtein, geofind but after you make the change in folder structure do :- pip uninstall <your package name mostly mentioned in setup.py> and reinstall the package meanwhile check your setup.py #!/bin/env python import os.path from setuptools import setup, find_packages def current_path(file_name): return os.abspath(os.path.join(__file__, os.path.pardir, file_name)) setup( name = 'bagoftricks', version = '0.1', include_package_data = True, packages=find_packages(), ) setup might have some other

Categories : Python

python c api not able to import any module into the newly created module
Resolved this in a different way : The problem is that the __dict__ attribute of a module is read-only. I am using the python/c api for 2.7.5. After using PyModule_New there was no provisioning to execute any code into the __dict__ for imports in the api. So I used a different approach. I created a module using python code rather than the python/c api. Which has the provision to to execute some code into a modules dictionary exec 'import sys' in mymod.__dict__. The sys import provides the newly created module to have access to sys.modules which has all the available modules. So when I do another import, the program knows where to look up the path for the import. Here is the code. PyRun_SimpleString("import types,sys"); //create the new module in python PyRun_SimpleString("mymod = t

Categories : Python

Why does it take longer to import a function from a module than the entire module itself?
The entire module still has to be imported to get the name you want from it...You'll also find that the OS is caching the module so subsequent access to the .pyc file will be quicker.

Categories : Python

How can I use the yum Python module to get a list of packages that will be downloaded before accepting the transaction?
You want to use the postresolve_hook(), and walk the transaction list. To see a fairly simple copy and paste example look at the changelog plugin (displays the rpm changelog for everything to be installed/upgraded in the transaction).

Categories : Python

Python : how to import module in other module
Take a minute to look at what you're trying to achieve: you want to import the module abc.py which is a part of the package lib, so in order to import it correctly you need to specify in which package it is: from lib import abc or import lib.abc as my_import On a side note, have a look at the Python Tutorial chapter on modules. Given what @Noelkd commented, I forgot to tell you about PYTHONPATH which is an environment variable that contains the folder where Python will look for the module (like the Java CLASSPATH). You need to put your root folder inside the PYTHONPATH to avoid doing some fiddling with sys.path.append. In Bash: export PYTHONPATH=<abs. path to outdir> For example, I've put outdir in my Desktop: export PYTHONPATH=~/Desktop/outdir And the import works lik

Categories : Python

Is there a way to import an embedded python script into IronPython as a python module?
It is possible. You just need to add search paths to ScriptEngine object like this: var paths = engine.GetSearchPaths(); paths.Add(yourLibsPath); // add directory to search or engine.SetSearchPaths(paths); Then you could use any module in directories, which you add: import pyFileName # without extension .py Update OK. If you want to use embedded resource strings like module, you may use this code: var scope = engine.CreateScope(); // Create ScriptScope to use it like a module engine.Execute("import clr " + "clr.AddReference("System.Windows.Forms") " + "import System.Windows.Forms " + "def Hello(): " + " System.Windows.Forms.MessageBox.Show("Hello World!")", scope); // Execute code from string in scope. Now you ha

Categories : C#

python: how to import a module a.b as a_.b?
You'll have to do it in two lines: import a as a_ from a import b print a_.b # <module 'a.b' from '.\a\b.py'> Or: import a as a_ import a.b print a_.b # <module 'a.b' from '.\a\b.py'> The first has the disadvantage that it puts b into your namespace and the second has the disadvantage that it puts a into your namespace. If you want to, you can fix that by using del b and del a, respectively. Alternatively, you can also write the second line as import a.b as ignore or from a import b as ignore, which will prevent a and b from appearing in your namespace.

Categories : Python

Python import value if module fails
A python import statement is an expression just like any other Python code. You can wrap your module import in a try...except block, like so: import somemodule try: from someothermodule import Temperature except ImportError,e: Temperature = 20

Categories : Python

import a python module from a user-specified dir?
my_path = "some/path/blah" os.path.insert(0,my_path) import my_custom_module you just need to add it to the path before importing it...or maybe I dont understand what you are asking

Categories : Python

How to import module PyImgur Python
For modules on the Google App Engine servers, other than those in official third party libraries you have to upload them with your application code. For local dev_appserver, they have to reside in your PYTHONPATH. The safe way to resolve both is to place the desired module in your application directory, as the dev_appserver extends PYTHONPATH by it.

Categories : Python

python unable to import module
at the top of test.py add import sys sys.path.append("..") base is not a folder on the path...once you change this it should work or put test.py in the same folder as base. or move base to somewhere that is on your path

Categories : Python

Import module into python not found
Does importing QSTK work? import QSTK How about QSTK.qstkutil? If not this is most likely a sys.path problem. Please post the result of: >>>import sys >>>sys.path It should look like: [ [...], 'C:Python27Libsite-packages', [...] ] Another thing you can check, is if 'C:Python27Libsite-packagesQSTKqstkutil' contains a file named '__init__.py'. From the module documentation: The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__

Categories : Python

import module or any component in python
In first place, any serious test requires more than 2 executions. But I assume you have performed many more with similar results. What is improved is not import's time, but general execution time. Specifically the time it takes the interpreter to resolve any kind of symbols. I'd suggest you to take the biggest module you have in production and replace all general imports with specific ones and time this. Another reason to prefer specific imports is documentation: having an idea of what a program does and how it does it by its imports. If the imports are specific, this information is also more specific.

Categories : Python

cannot import module using boost.python
Boost python requires the boost python so file. You can add it to your path when you run the python in a variety of ways. I am using export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH:../ThirdParty/boost_1_52_0/lib/linux64/

Categories : C++

Python import all module object
The lib.lib object is the nested lip.py file inside the lib package. Once you have imported the sub module, it also becomes available as an attribute of the package (unless you also included a lib name in the __init__.py file. The Python 3.3 module loader (which is implemented in Python) simply sets a nested module as an attribute on the parent: # [find a loader and load the module object, setting sys.modules[name]] module = sys.modules[name] if parent: # Set the module as an attribute on its parent. parent_module = sys.modules[parent] setattr(parent_module, name.rpartition('.')[2], module) This behaviour is more or less implied in the Python packages documentation: All modules have a name. Subpackage names are separated from their parent package name by dots, akin

Categories : Python

Import local copy of a module in Python
Python allows you to mess around with the internals of import. You can find more details here: http://docs.python.org/2/library/imp.html Alternatively, modifying sys.path seems to work: $ cat some_module.py def test(): return "one module" $ cat other/some_module.py def test(): return "the other module" $ python -c 'import sys; import some_module; print some_module.test()' one module $ python -c 'import sys; sys.path.insert(0, "other/"); import some_module; print some_module.test()' the other module

Categories : Python

Import Python Pyvisa Module in Sikuli
Sikuli uses Jython to run. Pure-Python code will (almost always) work normally, but Python code with native C code will not work. I'm not familiar with PyVISA, but it looks like pure Python, so I'd give it a try. You'll need to put PyVISA somewhere that Jython looks. Take a look at the Jython documentation.

Categories : Python

import module without executing whole script python
If the problem is arising from executing the LogInPage during the import... you can protect the code in LogInPage. Keep those codes in if __name__ == '__main__: block from being executed by import statements.

Categories : Python

why python can import a shared library as a module?
In the end, all programs execute machine code. When Python loads a shared library, it calls a function initlibraryName; this function calls back into Python to tell it what types, functions and modules it has, and what the machine addresses of the functions and the type descriptors are. Python adds them to its table (noting that they are in a C module, and not Python code), and when you call them, it looks them up, notes that they are in an external module, and calls them accordingly.

Categories : Python

unable to import lightblue module python
Check out this question ImportError: No module named Foundation Reinstalling pyobjc seemed to fix it too pip uninstall pyobjc pip instal pyobjc

Categories : Osx

module import in loop python, value not updating
import is not the tool you want here. As you discovered, once you've imported a module, you always get the same module; Python caches it in memory for performance. The problem, in other words, is not that pmlcode.py isn't getting updated, but rather that it is only actually read the first time import pmlcode is executed. Since you already have a string, why write it to a file and then import it? Just use exec directly on the string.

Categories : Python

How to install and import python module in the same script?
It will not work with setuptools, because setuptools will install pexpect as an egg, and then add it to easy-install.pth, which is checked only on startup. You can get around this in various ways, but it's easier to instead use pip to install pexpect: >>> import pexpect Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named pexpect >>> import os >>> os.system('bin/pip install pexpect') Downloading/unpacking pexpect Downloading pexpect-2.4.tar.gz (113kB): 113kB downloaded Running setup.py egg_info for package pexpect Installing collected packages: pexpect Running setup.py install for pexpect Successfully installed pexpect Cleaning up... 0 >>> import pexpect >>> pip will install

Categories : Python

Exception: Cannot import python-ntlm module
As @PauloAlmeida suggested in the comment, python-ntlm is missing. To install using pip just type into your OS shell: pip install ntlm On Linux you might need sudo before the command. You can also download the package from https://pypi.python.org/pypi/python-ntlm.

Categories : Python

update module on import to python interpreter
(Spyder dev here) I think at the moment you are not able to reload a module directly in the console (but we are considering to change this in the future). The idea about UMD is that it will reload your modules but only if you run a file from the editor that imports them. It doesn't work if you want to reload them directly in the console. Let's say you developed a module, then you are probably using it in a different script that (most likely) you'll be writing in our editor and send it to run to our console. UMD is a little bit of magic that reloads it for you when that happens.

Categories : Misc

python 2.7 module pandas not installing "cannot import name hashtable"
The recommended way to install pandas is via pip: pip install pandas This hashtables error arises from the cython files not being built. This error message will be more informative from 0.11.1.

Categories : Python

Python 3.3:cx_freeze & pyserial: cannot import traceback module
The first two lines are a bug that will be fixed in the next version of cx_Freeze. If you stick an import re in your script, you'll see the correct error message. The last line is your real problem - that means it didn't find the serial module when you froze it. Check where pyserial is installed on your computer.

Categories : Python

import python module/packge into remote machine
I am not in any way associated with the tool I am going to suggest. I have used Vagrant with VirtualBox and it has worked fine for me. The code is in a folder which is accessible from both Virtual as well as Base machine.

Categories : Python

Python module import : what about modules imported in the imported module
Your experiment can be conducted very easily from the shell: ╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ echo "import module1" > test.py ╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ touch module1.py ╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas› ╰─$ py Python 2.7.5 (default, May 17 2013, 07:55:04) [GCC 4.8.0 20130502 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import test >>> test.module1 <module 'module1' from 'module1.py'> >>> from test import * >>> module1 <module 'module1' from 'module1.py'>

Categories : Python

sql import export command error using pyodbc module python
db2 export is a command run in the shell, not through SQL via odbc. It's possible to write database query results to a file with python and pyodbc, but db2 export will almost certainly be faster and effortlessly handle file formatting if you need it for import.

Categories : Python

Python module organization to make import statement cleaner
Yes. You can import your modules inside of the __init__.py file in the B folder: __init__.py: __all__ = ('B_file_1',) from B_file_1 import B_file_1 Now, your second import statement will work.

Categories : Python

import classes from pyside inside of a boost python module?
PySide provides its Qt bindings with Shiboken. Shiboken generates Python C API bindings that supporting its own type conversion system. The knowledge of these conversions resides within the Shiboken generated bindings, and not the Python type system. Thus, PySide knows how to convert a QPointF object to/from C++/Python; Python's type system does not. When an object transitions through a function exposed with Boost.Python, then Boost.Python will check its registry for the appropriate type converter. These converters provide Boost.Python with knowledge on how to convert to/from C++/Python for types exposed through Boost.Python. Hence, when Boost.Python tries to return a QPointF C++ type to Python, it throws an exception as the conversion has not been registered with Boost.Python. Here

Categories : C++

ImportError: No module named ' ' while Import class in the __init__.py file Python
You are using Python 3. Do from .Courses import Courses from .Fridge import Fridge Python 2 would look for Courses module in the same dir, but Python 3 looks for Courses module in site packages - and, obviously, it's not there. P.S. "Phython" - sounds interesting ;)

Categories : Python



© Copyright 2017 w3hello.com Publishing Limited. All rights reserved.