Can a python module be imported without installing - python-3.x

Is this possible that a module is imported without installing the same. If no then why is my spyder IDE is showing a warning on the line where it is written import nltk even when nltk is not installed

Spyder runs a static analysis on the code you have in the Editor to offer hints and errors about it.
Since the analysis is static, it means Spyder doesn't run your code to perform it. In your case it simply detects that you have a line like
import nltk
but that there's no other use or call to nltk (for example, with a code like nltk.pos_tag(tokens) or something like that).

Related

How to run GAMS from python?

I am trying to run a GAMS file from a python file. I am using vscode on a macbook.
So far, I have been able to open the desired GAMS file from Python using subprocess. To do this I have used the following block of code:
import subprocess
subprocess.run(['open', "/Users/tk/GAMS/sim.gms"], check=True)
However, it just opens the GAMS file but does not execute it. I need to execute the GAMS file and after the GAMS execution is complete, the python program should continue.
How do I do this? Any help is much appreciated.
PS. There is a tutorial on GAMS, but I could not understand it as it seemed too complex to me.
I've never used gams but after a quick look at www.gams.com it sounds like /Users/tk/GAMS/sim.gms is not the file to be executed but the input file to be given as argument to the gams executable.
UG_TutorialQuickstart_RunningTheJob
So you should try something like :
import subprocess
subprocess.run(['/opt/gams/gams24.3_linux_x64_64_sfx/gams', '/Users/tk/GAMS/sim.gms'], check=True)
Notice : there is a GAMS python API which could give you a more portable solution.

Spyder - disable warnings inside Spyder Console

I use Spyder (4.1.5) as my python editor. When I run the code it shows a million workings that make it impossible for me to really focus on the output of the code. I read a lot of posts here on stackoverflow (see for example: Hide all warnings in ipython ) and it seems they all suggest to add
warnings.filterwarnings("ignore")
This for sure does not work.
For example, one of the many warnings printed to console many times is this one:
C:\...\Utils.py:2831: FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be deprecated, use a list instead.
However, I also have this line of code at the very beginning of the script:
warnings.filterwarnings("ignore", category=FutureWarning)
Still, no luck in disabling it.
import warnings
warnings.filterwarnings("ignore")
worked for me using Spyder 5 and Python 3.8.10

Working backwards from Python namespace to package

I'm working from someone else's code, and they're using openCV, therefore they used "import cv2". I didn't have it installed in my environment at the time so I needed to install that module. Having moved from MATLAB I'm still remembering different packages, but I remembered cv2 is openCV. However, had I not known cv2 was openCV, is there a programmatic way I can find out the package name from the import? i.e: using cv2, and not having openCV installed, finding out what cv2 is?

plotnine: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail

I am trying to use plotnine to generate some graphs. I import the required libraries:
from plotnine import *
from plotnine.data import mpg
And then, if I run the following code in PyCharm I get a Warning message, the window plot
shows a "No answer" message and I am forced to restart the python terminal:
(ggplot(mpg) # defining what data to use
+ aes(x='class') # defining what variable to use
+ geom_bar(size=20) # defining the type of plot to use
)
<ggplot: (150517199824)>
C:\Users\alvaromc317\miniconda3\envs\general\lib\site-packages\plotnine\ggplot.py:363: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.
However, If I start a python terminal from windows cmd terminal and run the same script as before, I get no error message and I see the plot with no problem.
What is happening and how do I get to use plotnine in pycharm?
In case it is needed, I work using a Windows 10 machine and python 3x based on miniconda.
I'm having a similar problem, but on MacOS using the commercial addition of PyCharm.
From this github issue and this JetBrains issue, it looks like it might be related to a PyCharm bug.

I have tried many methods and I couldn't find solution. So how to convert Python file to executables?

Python : 3.5
PYinstaller : 3.5
Win64
cx_Freeze : 6.0
According to the above information, I have tried to convert Python project to exe but it does not work.
First I have tried pyinstaller but the process throw some error:
After that, I have tried cx_Freeze and it works, but the exe are working on some computer but not on every computer that have same platform.
I don't know what I can do. I looked for google and stackoverflow but there are unsolved problems or I couldn't see the solution.
Later I have tried to change python version but doesn't work again. Computers that have tried to running the exe, have same OS platform, I'm sure.
By the way, if you receive the following cx_Freeze error, resolving like this:
Build\exe.win-amd64-3.7\lib\scipy\spatial\cKDTree.cp37-win_amd64
change to
Build\exe.win-amd64-3.7\lib\scipy\spatial\ckdtree.cp37-win_amd64
Program uses the following modules : tkinter, pydicom, skimage, PIL, cv2, etc.
Primarily Program has 2 page that content code but I made single file for I came across this sentence "It's worth noting that pyinstaller can compile to a single .exe file"
What do you suggest I do? Thanks for your help.
Edit: I have been tried "Auto-py-to-exe" but I got an error (Fatal Error : Failed to execute script")
Edit2: I tried to run outside the anaconda. I think its work. But I'm still testing.
Edit3: I have tried to change python version, GUI was opened another computer but the program is not work properly. the program works on my computer but not on another computers
If you are not able to convert python file to executable format, you can try using auto py-to-exe library.
This library contains a GUI format to convert .py files to .exe
Here you can find auto py-to-exe with usage instructions,
https://pypi.org/project/auto-py-to-exe/

Resources