Import * from python - python-import

I have a script named MyScript.py . I want to import all my script without running one/two rows, is it possible ?
for example:
in my new script :
import MyScript (will run all my rows)
from MyScript import * ( I want to run all excepting one row)
i want to make this condition in the second script without change anything on the first MyScript.py
Thanks !

No, what you are asking for is not possible using only import.
There is an option if you are willing to modify MyScript.py. Also consider packaging the code in MyScript in functions, so you can control exactly what code will be executed from your second script.
If you have code in your MyScript script that should not be executed when importing the script, surround that part with if \_\_name__ == '\_\_main__':
So for example:
# File MyScript
import numpy as np # will be executed on import
class MyClass:
def __init__(self, something):
self.something = something
def some_method():
return None
if __name__ == '__main__'
# everything in here will not be executed on import
print("Hello World")
When you import, all the code in the file will be executed, e.g. creating the class and importing numpy. The if statement if \_\_name__ = '\_\_main__' will also be executed, however, the condition will evaluate to false (as the special \_\_name__ variable is not set to \_\_main__ when you import) and therefore the code inside the if statement will not be executed. If you run the script directly from command line, the code will be executed since then \_\_name__ is set to \_\_main__.

I fond a method to do it.
MyScript.py
import pandas as pd
import os
print('yes')
if 'PROD' in os.environ:
print('all script')
When I want to run all the script I do :
Prod.py
import os
os.environ['PROD'] = 'PROD'
import MyScript
if I just want to test without run all script:
Test.py
import MyScript

Related

Path Problem In Vs Code for Almost every thing including Files in CWD or imported library

Whenever I need to use a file in Vscode whether if it is imported or in the CWD Vscode can not find it unless I enter the file path fully, is there anything that help to use files in current working directory. or when I import something like QIcon I have the same problem.
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtGui import QIcon
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQT6 Window')
self.setWindowIcon("qt.png") # this is not working I need to set a path

How to run more than 4 integration test files in one single dart file in flutter

When I try to add all main methods in single script and try running it using below command. It displays error as "did not complete"
flutter test -d emulator-xxxx integration_test\app_test.dart
When I try to include multiple dart files(more than 4) in single file as below. The integration scripts are not running and console displays "did not complete"
Suppose I have built my integration_tests folder with multiple dart files(more than 4 dart files) based on functionality of the mobile app
When I create one single file as app_test.dart as below:
`
import './test.login.successfull.dart' as LoginTest;
import './test.mainMenu.dart' as mainMenu;
import './test.homePage.dart' as homePage;
import './test.filters.dart' as filters;
import './test.streaming.dart' as streaming;
void main() {
testAll();
}
Future<void> testAll() async {
group('All TestCase at Once: ', () {
LoginTest.main();
mainMenu.main();
homePage.main();
filters.main();
streaming.main();
});
`
same code works with 4 files
Your structure is incorrect.
Create four separate test files using the convention:
xxxx_test.dart
Each has a main method with the nested test functions.
Then run
'''
dart test
'''
There is no need to create a script that calls the other tests as the dart test command runs all xxx_test.dart scripts found under the test folder.

Does Dart have import alias?

I found myself written tedious code when importing files into dart files like the following:
import '../../constants.dart';
I'm wondering if there is any way to create an alias to specific folder like:
import '#shared/constants.dart';
Thanks,
Javi.
Dart doesn't allow you to rename imported identifiers, but it allows you to specify an import prefix
import '../../constants.dart' as foo;
...
foo.ImportedClass foo = foo.ImportedClass();
It allows also to filter imported identifiers like
import '../../constants.dart' show foo hide bar;
See also
https://www.dartlang.org/guides/language/language-tour#libraries-and-visibility
What is the difference between "show" and "as" in an import statement?
Barrel files can also make importing easier like
lib/widgets/widgets.dart
export 'widget1.dart';
export 'widget2.dart';
export 'widget3.dart';
export 'widget4.dart';
lib/pages/page1.dart
import '../widgets/widgets.dart';
Widget build(BuildContext context) => Widget1();
No. Dart do not have import alias.
But you have absolute imports which makes up for it:
import 'package:my_lib/shared/constants.dart
This will import the file /lib/shared/constants.dart

Top level routines of one library are inaccessible when loaded through another library in Dart

Say, I load this script in the browser:
<script src='app.dart' type='application/dart'></script>
Now, in app.dart I have this:
import 'library1.dart';
unleashTheKraken();
Then in library1.dart you'll find this:
library library1;
import 'library2';
And finally in library2.dart we'll have:
library library2;
unleashTheKraken() => print('Unleashing the Kraken')
And the result is: Exception: No top-level method 'unleashTheKraken' declared. How so?
Because imports don't chain automatically. You have to use the export statement for that.
library library1;
import "library2.dart";
export "library2.dart";
And to avoid unnecessary code: import and export are completely independent. If you don't use unleashTheKraken in library1 itself, you can omit the import statement and just use export alone.

how to create exe of Python Code using opencv with or without py2exe?

i have some Python Code working properly its some kinda simple thing using opencv.
for example
import cv2
x = cv2.imread('Dog6.jpg')
cv2.imwrite('new2.jpg')
setup.py is
from distutils.core import setup
import py2exe
import cv2
setup(console=['name.py'])
but i am unable to create exe of this code with py2exe.
is there another method to create exe of such program ?
Error is
ImportError:numpy.core.multiarray failed to import
Put the line:
import numpy
Into your script, i.e the first file and try again.

Resources