How to import Jupyter notebook without automatically running it? - python

When looking at a solution to import an IPython file from another, I found the solution of:
import import_ipynb
from Genetic_Algorithm_Library_Code import ga
However, when I run the code, the program from the other file starts running immediately. Is it possible to only import functions and run them when I want on the second file?
Edit:
Problem solved: Used nbimporter instead.
import nbimporter
import gapro as gapro

Related

PHP exec cannot execute Python script

I use PHP in Windows 11. I cannot execute Python script in PHP exec.
The current situation is as follows, Commands that do not call Python scripts can be executed:
exec("cd E:/Python/WordFrequency && ipconfig", $output, $result_code);
exec("Python -V", $output, $result_code);
The above two lines of code return code 0.
However, the following code returns code 1:
exec("Python E:/Python/Mnist/main.py", $output, $result_code);
Run directly in Windows PowerShell:
Python E:/Python/Mnist/main.py
There is no problem.
However, calling in PHP returns code 1.
What's the matter, please?
After hard work, I have solved this problem a few days ago. Now, I put the solution here.
The reason why PHP cannot call Python files is that when executing the following command in exec():
Python E:/Python/Mnist/main.py
The following error was returned:
RuntimeError: Could not determine home directory.
After further testing, it was found that:
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
It is found that PHP can successfully call Python if main.py contains only the above five lines of code. This means that my entire PHP and Python program logic and operating system permissions are all right.
And once I add another line of code:
from matplotlib import pyplot as plt
PHP will not be able to call Python successfully, and will prompt the error message "Could not determine home directory". This means the error introduced by the code from matplotlib import pyplot as plt.
At the beginning, I thought there was a problem with the path or permissions of matplotlib installation. After inspection, there are no problems.
After inquiring more information, I gradually understood the reason. For the module matplotlib, it will ask to determine the user's home directory. Because usually only the users of the operating system call the matplotlib module, which is meaningful and won't cause trouble. However, unspecified visitors who call Python scripts through browsers will not own or determine the user's home directory. Therefore, in this case, it is very reasonable for the matplotlib module to throw an error that cannot determine the user's home directory.
How to solve the problem of Could not determine home directory?
The method is to add the following code before the line from matplotlib import pyplot as plt (or directly at the beginning of the Python file):
import os
os.environ['HOMEPATH'] = 'E:/Python/Mnist'
That is, the home directory of the unspecified user who accesses the Python script through the browser is manually specified. So as to provide matplotlib with a place where necessary files can be saved.

auto-py-to-exe created file does not execute properly

I used auto-py-to-exe to convert my .py to a .exe and why my py works as expected, my exe does not work. The console just springs up for a split second, before closing again and the code does not execute.
In case anyone wonders, these are the modules used in the code:
import os
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askdirectory
import cv2
import numpy as np
I do not understand where the error occurs...
EDIT:
"Open cmq and run the .exe on the terminal. In this way you can see the error message"
The Console throws no error, it looks like everything worked properly, but it clearly didn`t.

Converting python to exe gives an exe with black screen

I have made a python app in one python file with the following import statements:
import numpy as np
import PySimpleGUI as sg
import cv2
from PIL import Image, ImageTk
import sys
import os
import pydicom as dicom
from io import BytesIO
import matplotlib.pyplot as plt
from pydicom.pixel_data_handlers import convert_color_space
from scipy.optimize import curve_fit
When I run my app (in spyder), it gives simply a window telling the user to select a folder:
When the user chooses a folder and click ok, the app starts to open other window with buttons to click on etc. I have tested in spyder many times that my code does not contain error and in particular will not crush. My goal is to convert this one single python file into an exe such that the user can just double click on exe (without python environment) such that the code starts to run and they should see the window telling them to select folders immediately after double click.
I use pyinstaller for the task (with command pyinstaller --onefile main.py, where main.py is the my file. The process is slow (and produces a lot of text) but there does not seem to have errors. When I click on the final exe file, I get two empty windows:
What can there possibly go wrong? Is there any workaround to this? That is, is there anything other than pyinstaller to help me with the task?

Is there a way to check what you are running a python program in?

I want to create a script for just Terminal and IDLE, but I don't know how. Using if 'idlelib' in sys.modules: works for seeing if it is running in IDLE, but is there a way to use the same code to find if it is in Terminal by replacing 'idlelib'?
You can try using psutil and os
import psutil
import os
if psutil.Process(os.getpid()).parent().name() in ["cmd.exe","bash"]:
print("in cmd")
Using idle it returned 'pythonw.exe' which shows this works.

Importing from python interactive mode

I ran a script using "python -i" from the command line. The script ran as I expected and I end up in interactive mode, as expected.
Now, however, I want to use a command from the scipy.signal package, so I type:
>>> from scipy import signal
For some reason, this triggers the interpreter to run the whole script again from the start.
Why does this happen? And how should I avoid it?
When you import a file the whole file is read in and executed. This is the same whether you use from file import function or just import file.
You should place any code you don't want to run when it is imported in a block like this:
if __name__ = '__main__':
your code here
Your function definitions that you wish to import should be outside this block, as they need to be loaded and executed to be imported and available for use.
See this duplicate question which explains this in further detail.

Categories

Resources