python-import

php exec python script that imports numpy

帅比萌擦擦* 提交于 2020-01-06 08:27:06
问题 I need a php script to execute a python script. Now the python code looks like this: test.py import sys try: import numpy print 'Numpy was loaded' except: print 'Could not Load Numpy' All the following commands print 'Numpy was loaded': $python test.py $python2.7 test.py PHP Script: exec('python test.py',$output,$ret); var_dump($output); The PHP script works if the line 'import numpy' is removed. I have tried the following things: Use absolute path in php script : exec("/usr/bin/python /home

Importing issue with tensorflow in Python 3.5.3

人走茶凉 提交于 2020-01-06 05:42:25
问题 When I import tensorflow in Python 3.5.3 , it gives me following: ERROR: No module named _pywrap_tensorflow Unless you are using bazel, you should not try to import tensorflow from its source directory; please exit the tensorflow source tree, and relaunch your python interpreter from there. I installed tensorflow-0.12.0 using the following command: python -m pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl 回答1: Make sure that Python 3

Pycharm site-packages folder marked red and modules cannot be imported

懵懂的女人 提交于 2020-01-06 04:44:05
问题 I have installed some files ( pygame , python-docx ) through pip . They appear in pycharm in the site-packages folder, but I cannot import anything from that folder. I get a ModuleNotFoundError . The site-packages folder and sub-folders all appear as red. Any ideas what I should do? I have python 3.7 and pycharm community edition 2018.2.3 回答1: Try to check if your current project interpreter is correct: you should see the path to your python environment where all packages are installed as

Builtin python's __import__ vs imp.load_module: ValueError: Attempted relative import beyond toplevel package

喜你入骨 提交于 2020-01-05 07:02:24
问题 I have a piece of code that dynamically imports a bunch of subpackages - package structure: main_package/ code_below_is_here.py game/ __init__.py game1/ __init__.py constants.py records.py ... game2/ __init__.py constants.py records.py ... using the __import__ function (disclaimer: I did not write that code I 'm just the current maintainer): import pkgutil import game as game_init# <----- I import the game/ package # Detect the known games for importer,modname,ispkg in pkgutil.iter_modules

Importing deeply nested modules in Python

旧巷老猫 提交于 2020-01-05 03:51:07
问题 Consider the following case in Python 3.6: basepackage |---__init__.py |---package |---__init__.py |---subpackage |---__init__.py |---module.py Important detail : inside basepackage.package.__init__.py there's: from basepackage.package.subpackage.module import AClass as AliasedClass Now, let's say inside basepackage.package.subpackage.module.py we want to use: import basepackage.package.subpackage.module as aliased_module [1] The result is: AttributeError: module 'basepackage' has no

Add a single module to Python's import search path?

只谈情不闲聊 提交于 2020-01-05 02:55:13
问题 If I set PYTHONPATH to some path /path/to/modules/ , then the path is appended to sys.path and I can import modules/packages contained in /path/to/modules/ . However, if I only want access to a single module/package, then adding /path/to/modules/mymod.py or /path/to/modules/mypackage/ to sys.path does not work. So is there a way to add only a single module/package to the import search path, rather than adding the entire parent directory? I am asking because I need to import a single package

Is the import order of extensions in module filenames guaranteed in Python?

寵の児 提交于 2020-01-04 05:43:05
问题 Experimentally, I verified that when a compiled extension.pyd (or .so ) and plain extension.py both exist in the same directory, the .pyd file gets imported first ; the .py is only imported if the .pyd file is not found: In [1]: import extension In [2]: extension.__file__ Out[2]: 'extension.pyd' In [3]: import glob; glob.glob("extension.py*") Out[3]: ['extension.py', 'extension.pyd'] Is that guaranteed to be the same for all versions of Python, and can I rely on this to add logic to the .py

Difference between importing python library within function versus importing globally?

女生的网名这么多〃 提交于 2020-01-04 05:31:08
问题 Suppose I want to import a python library for use inside a function. Is it better to import the library within the function or import it globally? Do this def test_func: import pandas as pd # code implementation or have the line below at the top of the python file to import globally? import pandas as pd What are the pros and cons of each approach? Which is the best practice in python? I am using python v3.6 EDIT: Some clarifications to make. Suppose I have 2 functions. def func1: import

Import error? (PYTHON 3.2)

随声附和 提交于 2020-01-04 03:15:53
问题 I have my own module named v_systems, and I'm trying to import that module in another python file (which is also saved in the same directory as the file v_systems is saved) I need to import it as import v_systems as vs or even if I try to import as import v_systems . However it gives me an error saying no module v_systems exists. How may I fix this error? 回答1: It might not be in the system path. Do the following: It needs to be in the directory of the sys.path . What I did is I created a

Addressing python objects

╄→尐↘猪︶ㄣ 提交于 2020-01-03 16:58:53
问题 I'm trying to use ncclient for Python. If I do this it works: from ncclient import manager m = manager.connect() If I do this it fails: import ncclient m = ncclient.manager.connect() The error is AttributeError: 'module' object has no attribute 'manager' . I don't understand what the difference is. Shouldn't that be the same method either way? Why isn't it? 回答1: Importing a module (package) does not automatically import submodule. (Some modules do. For example, importing os module also import