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
2 answers

How to import function from a sub-file within a folder (without having to refer to the name of the sub-file)

I have written a bunch of functions stored in different python files over the past years that I would like to dump into a folder and just use 'import folder' to call the functions from those python files. So far, all the solutions I have read needs…
Xian Li
  • 13
  • 2
1
vote
2 answers

Import Error : attempted relative import with no known parent package

Can you please tell me how to do relative import correctly. Project Structure: p1 |- x1 | |- __init__.py | |- x1_module1.py |- x2 |- __init__.py |- x2_module1.py In x2_modules.py try: from p1.x1.x1_module import temp_func except…
unknown
  • 11
  • 1
1
vote
0 answers

VS Code Python: Perform Relative Imports Most Like PyCharm's

I'm in the middle of transferring my work away from PyCharm and into VS Code to minimize the number of IDE's open at the same time. As I've been doing that, I've noticed that importing is markedly different: My layout is as follows: Projects -…
Joshua Harwood
  • 181
  • 1
  • 12
1
vote
0 answers

Is it possible to import a built pyinstaller executable into a python script?

Is it possible to import a built pyinstaller package within a python script? For example I have run pyinstaller on the following script: count_to_ten.py def count(): for i in range(10): print(i) I've then packaged this with pyinstaller…
Aaron Ciuffo
  • 476
  • 3
  • 14
1
vote
0 answers

ModuleNotFoundError when running individual Python files with absolute imports

ModuleNotFoundError running Python 3.8.x I'm building a python package containing classes and functions each with verbose tests. Inside the package I'm trying to use these building blocks to provide end-to-end uses and examples by using absolute…
Joel Biffin
  • 278
  • 1
  • 2
  • 15
1
vote
0 answers

Erroneous red lines and "Unresolved reference" errors in PyCharm

I have the following folder structure: project/ ├── main.py ├── package/ ├── __init__.py └── module.py └── test.py In module.py, I have a function func1. In test.py, I have the following code: from module import func1 PyCharm shows me…
Erel Segal-Halevi
  • 26,318
  • 26
  • 92
  • 153
1
vote
2 answers

how can import correctly two variables from another Python file in the same folder?

I would like to read the values of 2 variables existent in another python file 'save_state_mod.py' I cannot understand what's wrong with my code here: from save_state_mod import event , save_missing_weight smw =…
MBENKI
  • 13
  • 5
1
vote
1 answer

How do I reference my Azure function from pytest?

I'm using Python 3.8 with Azure functions. I have the following project layout ... myproject __app__ __init__.py objects __init__.py tests __init__.py functions __init__.py …
Dave
  • 17,420
  • 96
  • 300
  • 582
1
vote
3 answers

Disambiguation error when working with wikipedia import module in python

While using the Wikipedia module in my code, I am getting a disambiguation error. **My code: import wikipedia print("Using Wikipedia") while True: input = input("Question: ") print(wikipedia.summary(input)) **Output: Using…
Ameya Uppina
  • 41
  • 1
  • 9
1
vote
1 answer

ModuleNotFoundError when importing another package

I'm trying to import a class from a script that is located in another package: project_folder | | package_1 | | __init__.py | |foo.py | | | package_2 | | __init__.py | | bar.py In the script: "bar.py", I have the following import: from…
Maarten
  • 37
  • 8
1
vote
2 answers

How can I have one copy of a variable shared between exactly two modules?

I want a single copy of var that behaves in a global way: i.e. its storage exists once and all references to it in both modules read and write the same storage. How can I get that behavior using two modules in total? Here is my attempt, but it fails…
All the Rage
  • 587
  • 4
  • 19
1
vote
0 answers

Access list of imported modules from collect_submodules in hiddenimports from PyInstaller

I have an application bundled using PyInstaller. One of the hooks I created added some hiddenimports from a nested package: hiddenimports = collect_submodules("mypkg.subpkg"). In another part of my code, at runtime I am trying to dynamically import…
AlyT
  • 221
  • 1
  • 9
1
vote
3 answers

Error importing a file: ModuleNotFoundError: No module named

I know there are a lot of questions asking the same I'm now asking, but I've tried most of their answers and they doesn't fix my problem. I have a file named fileA.py where I need to call and import fileB.py. Until now i was doing this: # in…
yoyoyo
  • 109
  • 6
1
vote
0 answers

Script a needs data from script b, but script b also needs data from script a. Can I import two-way?

I'm currently working on a script, script1.py, which uses a class defined in script2.py. However, that class in script2.py uses an instance of another class defined in script1.py, so essentially both scripts need to import variables from one another…
Tim Rood
  • 49
  • 5
1
vote
0 answers

Using the relative import mechanism, how to import a module from a parent directory in Python 3?

I have a file hierarchy of a python package, "try_from_import", like this: . ├── __init__.py ├── fibo.py └── test └── __init__.py Within the directory "test", I cannot import the module fibo.py: In [1]: from .. import…
zell
  • 8,226
  • 7
  • 41
  • 91
1 2 3
99
100