Questions tagged [python-import]

For questions about importing modules in Python

Importing modules with python:

  • import random

You can use functions from the random module by writing random. in front of the function, for example random.randint

  • import random as rand

The same as above but instead writing rand. before the function

  • from random import ...

imports specific functions, or * for all into your program, you can call the functions as if they were in the file without writing anything before them.

4828 questions
1
vote
0 answers

How do I structure the import statement for this Python file?

I have a project folder structure as below: CHATNETBOT lib bot __init__.py cogs hub.py db utils modules config.py launcher.py I am trying really hard to get…
ProfK
  • 44,292
  • 106
  • 358
  • 713
1
vote
1 answer

Disable logs from imported module using the root logger

One of Microsoft's presidio-analyzer modules logs with the root logger: logging.info( "Returning a total of %d recognizers (predefined + custom)", len(to_return) ) This particular line will be written millions of…
IanS
  • 13,182
  • 7
  • 49
  • 75
1
vote
1 answer

Second script does not recognize variables of first script when first script runs second script method

I have this script MainGUI.py that calls a method from another script Main2.py. The MainGUI.py script is linked with a .kv file named gui.kv, in which a method submitbutton() is called. This is a function defined in Main2.py that will eventually…
Tim Rood
  • 49
  • 5
1
vote
1 answer

Importing module from a directory above current working directory

First of all, there are a bunch of solutions on stackoverflow regarding this but from the ones I tried none of them is working. I am working on a remote machine (linux). I am prototyping within the dir-2/module_2.py file using an ipython…
Rio1210
  • 189
  • 1
  • 2
  • 11
1
vote
1 answer

What is the correct way to import modules for standalone app created using `python -m zipapp`?

I have a module which I'm going to distribute as standalone app. The module has the following structure: $ tree -L 2 ./ ./ ├── mymodule │ ├── __main__.py │ ├── fun.py └── mymodule.pyz mymodule/__main__.py contains next lines: #!/usr/bin/env…
Ihar Katkavets
  • 1,118
  • 12
  • 21
1
vote
1 answer

Python 3.8 module duplication and app object scope within imported functions

I'm confused about import of custom modules. As you can see in the code below, in main I first import all libraries needed by everything AND that I duplicated those imports in my i_setup_functions.py file. Leaving any of them out of either file…
ScotterMonkey
  • 880
  • 9
  • 21
1
vote
1 answer

Python 3.8 order of import modules and custom modules

I'm confused about import order and use. As you can see in the code below, in main I first import psycopg2. THEN I import data_connect (that has dependency on psycopg2), so two questions: (1) Why doesn't the interpreter see that I already imported…
ScotterMonkey
  • 880
  • 9
  • 21
1
vote
1 answer

Pytest fails when importing podpac module

Our tests fail when we run tests from the C:/rootdir, but when we run a test individually, in C:/rootdir/tests/our_module/, it fails with a long error trace below. It is definitely triggered on our line 24 by the import: from…
nobbyhawk
  • 21
  • 3
1
vote
1 answer

Python imports within a directory

I have a project that is structured thus: . |-dir | |-subdir | | |-subsubdir1 | | |-subsubdir2 | | |-subsubdir3 | | |-sub_sub_main.py | | |-other_relevant.py | |-sub_main1.py |-dir2 | |-sub_main2.py …
Ben Rei
  • 79
  • 1
  • 2
  • 10
1
vote
2 answers

Why does importing a file from a folder work, but calling a file from an imported folder doesn't?

Im currently working on a project with the main file being in a directory together with a subdirectory with some additional files: project |- folder | |- __init__.py | |- file1.py | |- file2.py | |- file3.py |- main.py With the init file being…
Thörni
  • 392
  • 2
  • 14
1
vote
1 answer

Import Error when using VSCode with conda interpreter but not when importing in Conda

I'm trying to run a Python program in VSCode and it always stops in the first line from sklearn.gaussian_process.kernels import WhiteKernel, RationalQuadratic, RBF, Matern, ExpSineSquared The error it gives me is pretty big, I'll paste it at the…
1
vote
0 answers

partially initialized module 'datetime' has no attribute 'now

import pandas as pd data = pd.read_excel(r,'C:\python Projects\python\Book1.xlsx') print(data) //error C:\python Projects\python>python sir.py Traceback (most recent call last): File "sir.py", line 1, in import pandas as pd File…
1
vote
1 answer

Project (AWS CDK) and Lambda specific python imports

Image you are writing an IaaC (Infrastructure as a Code) project with python programming language. There is a good library/sdk that makes it convenient: AWS CDK (Amazon web services cloud development kit). In the same project you can specify lambda…
1
vote
1 answer

Error importing cv2: 'libSM.so.6: cannot open shared object file: No such file or directory'

I'm deploying a Django app on AWS elastic beanstalk, and I run into the following error: ImportError: libSM.so.6: cannot open shared object file: No such file or directory. I've attached logs below, the one that describes the error is…
1
vote
1 answer

Is there a way to use import for a list of packages?

I would like to know if it is possible to use import in a list data structure or dict for example in python instead of repeating the import statement for every packages? An example: import { asyncio, asyncpg, datetime, os, …
t1m0th33
  • 117
  • 6
1 2 3
99
100