python-import

Python C api PyImport_importmodule fail when the file has an import statement

夙愿已清 提交于 2019-12-24 12:09:29
问题 I tried to use the Python C api to call a function from python in C++, the test was successful. But if I intend to import a module already importing other module, the Pymodule_findmodule will return Null even though it's there and created a compiled file. Here is my code Py_Initialize(); PySys_SetPath("C:/Users/Mik/Documents/GitHub/youtube-dl"); PyObject * pythonFile = PyImport_ImportModule("test2"); Here is the python filed named test2.py at that directory with a file named test_dl.py and a

Unresolved reference “cv2” inside cv2 (cv2.cv2)

浪尽此生 提交于 2019-12-24 10:48:12
问题 I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all). Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it. So I went to the cv2 init file, which looks like this: import importlib from .cv2

Python: weird import behaviour

余生颓废 提交于 2019-12-24 09:19:05
问题 I have a package with the following structure projectX ├── data ├── results └── projectX ├── stats │ ├── __init__.py │ ├── stats_worker.py │ └── stats_wroker2.py ├── __init__.py ├── main_worker.py └── interface.py Notice that the project folder and the source folder share the same name (projectX). The problem is a bit complex and needs some premises to be done Premises: I always launch the interface and always have launched the interface. No other script is called My problem first arose when

How can I change the name of a group of imports in Python?

我的梦境 提交于 2019-12-24 07:46:53
问题 I would like to import all methods from a module with altered names. For instance, instead of from module import repetitive_methodA as methodA, \ repetitive_Class1 as Class1, \ repetitive_instance4 as instance4 I'd prefer something along the lines of from module import * as *-without-"repetitive_" this is a rephrasing of this clumsy unanswered question, I have not been able to find a solution or similar questions yet. 回答1: You can do it this way: import module import inspect for (k,v) in

ImportError: cannot import name MainClass

时光总嘲笑我的痴心妄想 提交于 2019-12-24 07:17:33
问题 society. I'm trying to understand the OOP programming and I'm facing some issues and asking for help. Here the example: I'm trying to create all objects under one class and then I want to inherit from this class. test_class/baseclass.py from test_class.first_class import FirstClass from test_class.second_class import SecondClass class MainClass: def __init__(self): self.firstclass = FirstClass() self.secondclass = SecondClass() test_class/first_class.py from test_class.baseclass import

How to Import Multiple Python Modules from Other Directories

家住魔仙堡 提交于 2019-12-24 06:13:27
问题 Python newbie here. Any and all help on python structure would be much appreciated! MAIN QUES: How to Import Multiple Python Modules from Other Directories I've reassembled the repository for a project so that modules for specific tasks are within individual folders according to type of task instead of just one huge folder. Because my modules were all in one folder, I used to be able to import easily in this fashion: import sys sys.path.insert(0,'/home/myname/folder_name/all_modules') Now, I

Python - How to PYTHONPATH with a complex directory structure?

谁说我不能喝 提交于 2019-12-24 06:12:58
问题 Consider the following file\directory structure: project\ | django_project\ | | __init__.py | | django_app1\ | | | __init__.py | | | utils\ | | | | __init__.py | | | | bar1.py | | | | ... | | | ... | | django_app2\ | | | __init__.py | | | bar2.py | | | ... | | ... | scripts\ | | __init__.py | | foo.py | | ... How should I use sys.path.append in foo.py so that I could use bar1.py and bar2.py ? How would the import look like? 回答1: Using relative paths would be much more desirable for

How and where to install a module in python from github

血红的双手。 提交于 2019-12-24 05:34:59
问题 i need to install a module (pyrise) from github. This is because i need to modify it and later on commit the changes and ask for a pull request. If i install with pip i get the usual pyrise folder with the __init__.py at root level. So i unistalled with pip and dis ( in the site-packages folder ) git clone git@github.com:nicoladj77/pyrise.git cd pyrise python setup.py install this installed correctly everything, but now i have a pyrise folder under site-packages and another pyrise folder

Python swapping out sys.modules does not work as intuited

淺唱寂寞╮ 提交于 2019-12-24 01:17:53
问题 I was experimenting with setting the dictionary sys.modules while working on an answer to another question and came across something interesting. The linked question deals with removing all the effects of importing a module. Based on another post, I came up with the idea of deleting all new modules from sys.modules after an import. My initial implementation was to do the following (testing with numpy as the module to load and unload): # Load the module import sys mod_copy = sys.modules.copy()

What is the cleanest way to add a directory of third-party packages to the beginning of the Python path?

烈酒焚心 提交于 2019-12-24 00:56:19
问题 My context is appengine_config.py, but this is really a general Python question. Given that we've cloned a repo of an app that has an empty directory lib in it, and that we populate lib with packages by using the command pip install -r requirements.txt --target lib , then: dirname ='lib' dirpath = os.path.join(os.path.dirname(__file__), dirname) For importing purposes, we can add such a filesystem path to the beginning of the Python path in the following way (we use index 1 because the first