Python faster import - python

I have a question regarding the python import part.
Every time I am running my script python has to load up a bunch of import stuff.
Is there a way to skip this part in the way you can do that with jupyter notebook?
Thanks for your help!
I tried using Jupyter notebook but that is actually not my favorite coding environment.

When you run your script, add "python -O". It will perform an optimized build. However it will disable some features like the built-in debug var.
You can always use the "import time" library to measure the time taken by each import and figure out if what you're trying is effective.
Hope this will help you

Related

Why, suddenly, importing the exact same python module as before became so slow?

I am running a python script that calls local modules (with import), and since few days it became so slow.
I could not find the reason why it is so, neither on other Github posts or Google, that's why I am posting this.
Providing code won't be of much help, but here is the import that poses a problem:
import latplan
where latplan is this library
But again, this import did not pose any problem at all before...
import statements can be very slow in Python because they are allowed to execute arbitrary code as side effects; they don't just scoot for class and def keywords.
Now, as with any performance issue, it's hard to guess what exactly is taking so long without profiling. Luckily there's a builtin, specialised profiler for import time:
python -X importtime -c "import latplant"
I recommend using tuna to visualise the reports.

How do I create a program for a friend without him having to download any of the external libraries?

I am trying to create a program for a friend which requires I import libraries such as:
from lxml import html
import requests
These required me to install things to my computer in order to run the py file, but I am trying to avoid my friend having to do this. Is there any way that this can be done? I'm sure there is a simple method and just have never come across it before, thank you!
I've tried setting up a local environment or creating an executable file using PyInstaller. Are these effective options? Would I simply be able to create a virtualenv and send it over with the project file? How would that work?

VS Code randomly imports libraries in my Python Scripts

I feel like I am going crazy. Random libraries have started to become imported into my python scripts while working in visual studio code.
The other day, I pulled up some old code and noticed a very strange library import at the top.
from turtle import right
This had nothing to do with my program and I thought someone was pranking me - which would be quite odd to do this in this file I have not opened in months and the library import had no meaning to me.
A couple of weeks later, I start working on a different python script and within the same day, while at home, no one with me, another random library import occurs in my script.
from telnet import DO
Can someone please give me a reason why this could be happening?
VSCode has an auto import feature. If you press enter while it suggests you some method/object, it will be automatically added as an import if it is not in the namespace already.
Python auto-imports libraries from their keywords by default. You can disable this in the settings.

How to disable OpenCL with wand in python

So I'm using wand for a project and it's been working fine, except whenever I want to use a function that has been 'OpenCL-accelerated' (list of functions here), it stops working, no error or anything, but I'm pretty sure that OpenCL is what's causing it.
So my question is, how can I disable OpenCL in wand so that I can use those functions? Again, I've looked for solutions, but alas I couldn't find anything python specific, and the api module and the python module itself don't seem to mention anything about it either.
Any help would be greatly appreciated, thanks!
Just set the environment variable MAGICK_OCL_DEVICE=off to disable OpenCL.
Either before running the script.
export MAGICK_OCL_DEVICE=OFF
python myScript.py
Or within the python script before invoking any Wand code.
import os
os.environ['MAGICK_OCL_DEVICE'] = 'OFF'
# ... wand stuff ...
And the reason that it looks like your application stops running is that ImageMagick will need to run an OpenCL benchmark the first time and that can take a while. The result of the benchmark will be cached and used the next time an "OpenCL-accelerated" method is executed.

Optimizing Python scripts' running time from C#

I apologize in advance if my question is badly formulated, for I don't know if what I need makes any sense.
I'm currently working on a c# project where I need to run several time the same python script from inside the program, but with different arguments each time.
For this, I'm not using IronPython, but the ProcessStartInfo class, for I understood that IronPython has some problem with certain packages I use. But this can change.
My problem is that although the python script is small and fast, it needs to import first a lot of packages, and this takes a lot of time. And therefore, my code is very slow, while 90% of the time is used to import python packages.
I can't work around the problem by running this python script a single time with many arguments.
So is there a way to "open a permanent python console" from c#, where I could import everything once, then run the small script with my first argument, get the result back in c#, then run the script a second time etc .... ? Or any other way to optimize this ?
Thanks for your help,
Astrosias.

Categories