Run Anaconda script in Windows from C++/Qt program - python

Okay, weird thing. This works perfectly fine in Linux, but I cannot get it to work in Windows. Problem might be that I'm not working in Windows a lot these days...
I've got a Python script that I want to run from a C++/Qt program. So I installed Anaconda and within the "Anaconda Prompt" I installed the package containing the script via pip. I've got the full path to the Python script (defined as an entry_point in the packages's setup.py, so it's actually an exe file) and try to start it like this:
proc = new QProcess(this);
connect(proc, &QProcess::readyReadStandardError, this, &MainWindow::receivedText);
connect(proc, &QProcess::readyReadStandardOutput, this, &MainWindow::receivedText);
proc->start(python_script, arguments);
Where python_script and arguments are the full path to the python script and its arguments. When I run this from the "Anaconda Prompt", it works fine, but from my Qt program I get a "NumPy not installed" error, although it's definitely installed. Could it be that I have to run "conda activate" somehow first? Sorry, I'm not really familiar with Anaconda, only using it in Windows... How would I do that?
Thanks!

can you open a dos prompt shell from your code? If so, you can get anaconda to start within that shell and then run your conda activate commands, etc.
Try this command to see if this is an option, you may need to change the paths from miniconda3 depending on what anaconda app you are using:
%windir%\System32\cmd.exe "/K" C:\Users\user\miniconda3\Scripts\activate.bat C:\Users\user\miniconda3

Found a method that works: instead of calling the script directly as "script.exe arguments", I can run it through the Python interpreter as "python.exe script.exe arguments", which seems to work nicely.

I keep having the same issue when I switch servers, so posting a solution which always works for me:
conda install -c conda-forge implicit

Related

Can't run any Python files on Ubuntu(WSL)

I have just started working on my new pc and just to get a feel for it I wanted first to start working on python files, so I started first by just wanting to run WSL on windows and it installed correctly but when I want to run any python using the run python file on the top right on VS code, this is what gets executed $ C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe f:/Projects/hello.py
And this is the error: -bash: C:/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe: No such file or directory
I have no idea what's causing it but when I run the file using 'Shift + Enter' which is: Python: Run Selection/Line in Python Terminal it seems to run the single line correctly but it gives me this error instead:
print("Hello, world")
-bash: syntax error near unexpected token `"Hello, world"'
but when I run it using python3 hello.py, it works perfectly fine?! I'm so lost as to why this is happening and how could I fix it.
Might be relevant: I'm using windows 10, installed python 3.10.2 from windows store, all of that is in VS code and the python code is one line: print("Hello, world") and I changed the permissions of Local/Microsoft/WindowsApps so it's now accessible by all users to view/read/edit/run, made sure that python3.10.exe exists(on the WindowsApps and it works perfectly) and reinstalled it many times, tired python3.9, and tried to install python from the website instead of the windows store and still the same, manually added python to PATH and tried .venv and didn't work. when I launch python3.10.exe outside vs code it seems to run perfectly, I have worked with python before and it used to work fine now I don't know what's wrong.
I have seen other questions of the same problem I'm having here but none of them solve the problem.
No such file or directory C:/Users/...
For wsl, the Windows filesystem is accessible, but it has a different path. It is mounted under the /mnt folder. So you would find your python .exe under /mnt/c/Users/jaffe/AppData/Local/Microsoft/WindowsApps/python3.10.exe. This said, the executable file is meant to work on Windows, and it doesn't really makes sense to use it on Linux when you could run python within your wsl distro.
python3 works perfectly fine
This is because most Linux distributions come with python3 pre-installed, so you can use it already. To see where it is located, you can run the command which python3, or python3 --version to check its version.
If you want to change version, you may consider download it from you package manager, apt.
I also suggest to install python3-pip if you don't have it already to get the pip package manager for python.
In my case when I ran into this.. I discovered pyenv. This allows you to download more than one version of python. You can then go into a specific directory, such as your python project and issue a python local 3.10.0 (for example). Here's a link on how to install it as well as poetry which is a virtual environment manager that is become very popular. You can also create an alias for python that works off of this. I add this command to my alias file and source it from my .bashrc. alias python='pyenv exec python3'

Pyperclip module installed but not found when program runs from cmd

I wrote a program that use pyperclip module and it would work from Pycharm and python IDLE, would work as well if starting from Powershell but if I try to start the program from WIN+R, when launched, the program returns an error saying that pyperclip module is not installed. The same problem appears when I run it from the Anaconda Powershell Prompt.
PLEASE NOTICE:
The program was working perfectly before I installed Anaconda and Jupyterlab.
The error occurs when I run the program from the cmd using WIN+R AND when I run it from the Anaconda Command Prompt but it's fine when run from IDLE, Powershell, Pycharm.
I always used python 3 and only yesterday I installed Anaconda.
Thanks for the help!
I just spent couple of hours trying to solve exactly the same problem. What I found out is that, as couple of members have already pointed out, the main problem is the mismatch between the version of the python that runs in cmd and the versions of the python used in scripts/batch files.
The first line in the code, known as "shebang", in .py file indicates the version of the python that the script should use when it is executed. So, it must match the version run by default with cmd (or when executed with win+R). In my case, I also have a batch file (.bat) that calls specified version of the python, which should be the same version used with .py file.
The problem was that both of my files (.py and .bat) were calling python 3.8 while the cmd is running version 3.7. Initially, I used shebang #! python3 in my .py file, and command #py path/to/python/file.py %* (and I also tried #py.exe path/to/python/file.py %*) in my .bat file, and that did not work.
To solve the problem, I updated these two files to link to python version 3.7 with following:
changed shebang in .py file to #! python
changed command in .bat file to #python path/to/python/file.py %*
With these changes the system runs the program with win+R.
whew, taking course on python and instructor had py.exe instead of python.exe in .bat call. despite trying all of the other installation methods others have mentioned, just changing this to python.exe did that trick.

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:
python-3.5.4-amd64.exe /passive PrependPath=1
according this: 3.1.4. Installing Without UI I use the PrependPath parameter which should add paths into Path in Windows environment variables.
But it seems not to work. The variables does not take any changes.
If i start installation manually and select or deselect checkbox with add into Paths then everything works.
Works same with clear installation also on modify current installation. Unfortunately i do not have other PC with Win 10 Pro to test it.
I have also tried it with Python 3.6.3 with same results.
EDIT:
Also tried with PowerShell Start-Process python-3.5.4-amd64.exe -ArgumentList /passive , PretendPath=1 with same results.
Also tested on several PCs with Windows 10, same results, so the problem is not just on single PC
EDIT:
Of cource all attempts were run as administrator.
Ok, from my point of view it seems to be bug in Python Installer and I can not find any way how to make it works.
I have founds the following workaround:
Use py.exe which is wrapper for all version of Python on local machine located in C:\Windows so you can run it directly from CMD anywhere thanks to C:\Windows is standard content of Path variable.
py -3.5 -c "import sys; print(sys.executable[:-10])"
This gives me directory of python 3.5 installation.
And then i set it into Path manually by:
setx Path %UserProfile%";PythonLocFromPreviousCommand
try powershell to do that
Start-Process -NoNewWindow .\python.exe /passive
Make sure you are using an elevated command prompt (ie: run as administrator).
Have you tried to use the InstallAllUsers argument. By default it is set >to 0 so try to use it like this (which is the same example from [here][1]):
python-3.6.0.exe /quiet InstallAllUsers=1 PrependPath=1 Include_test=0
it migth make a difference to use the /quiet over /passive
[1]: https://docs.python.org
/3.6/using/windows.html#installing-without-ui "the link you supplied"
To answer Erik Šťastný comment i believe that a good solution to your problem is to package python with your program to make sure that all the required libaries is preinstalled.
I also tried the command line options for the python installer and noticed the same issue as you, and here's the solution I found:
Download the 64-bit installer from here: https://www.python.org/downloads/windows/
(the link is titled "Windows x86-64 executable installer")
Uninstall any current python installation.
You can use this command: START python-3.8.3-amd64.exe /uninstall
(replace python-3.8.3-amd64.exe with the name of the file you downloaded).
(run cmd or your batch file as administrator, by right-clicking, then Run As Administrator).
Install (as admin) python 64-bit for all users, with the START command:
START python-3.8.3-amd64.exe /passive PrependPath=1 Include_pip=1 InstallAllUsers=1
(replace python-3.8.3-amd64.exe with the name of the file you downloaded).
(run cmd or your batch file as administrator, by right-clicking, then Run As Administrator).
(More info on python installer command line options: https://docs.python.org/3/using/windows.html#installing-without-ui).
(Optional) Open a new cmd window to verify that python works from any location:
You can run this command:python --version
(If you don't see output like "Python 3.8.3", then Python has not been added to your PATH).
(Note: That command didn't work until I opened a new command prompt window).
For me, all of the details were important, so don't skip any.

pip command does nothing

I just installed Python 2.7.10 on windows 10.
I have added my python and pip directory to my PATH like so:
My Scripts folder looks like this:
My problem is, when I type in "pip" in command prompt and press enter absolutely nothing happens, even if I wait several minutes. If I remove the Scripts directory from the PATH variable I just get the error message like "pip not recognized as internal or external command". Python works fine. I have also tried to reinstall both pip and Python but the same problem occurs.
So, does anyone have any idea about why pip does not do anything?
**Edit: ** when I say it does not do anything, I mean the cmd "hangs", like if it is waiting for something to happen. The cursor just keeps on blinking.
One command that is bound to work is writing:
python -m pip install requests
This works because you hand off the script invocation to python, which you know works, instead of relying on the PATH environment variable of windows, which can be dodgy.
Packages like numpy that require c-extensions to be built, will not work with pip unless you have a C Compiler installed on your system. More information can be found in this question.
If you are, as you're saying, unfamiliar with the python environment, then let me assure you, you will have a better day by installing Anaconda.
Anaconda is a completely free Python distribution (including for
commercial use and redistribution). It includes more than 300 of the
most popular Python packages for science, math, engineering, and data
analysis.
Anaconda comes with numpy, of course.
After Python including pip at package, pip commands not work sometimes.
Then you can use pip through python like
python -m pip <pip commands that you want>
Try disabling your virus scanner. If this fixes it, exclude the C:\Python27\ folder from scanning (at your own risk).
I had this same issue: typing pip on the command line just puts the cursor on the next line, and nothing happens. I was sure my PATH system variable had C:\Python27\ and C:\Python27\Scripts\ in it, and I could verify it using echo %PATH% on the command line.
I found that I had to disable my virus scanner (Avast). I excluded the C:\Python27\ from virus scanning, and now everything works. Apparently the scanner is interfering with Python's ability to load the module.
Add the following path or you can also cd to the path and then try pip command, it will work fine.
C:\Python27\Lib\site-packages\pip
I had the same issue after uninstalling my antivirus, which was blocking the script. The issue was resolved.

Python console not launching properly from command line

I seem to have problem launching python from command line. I tried various things with no success.
Problem: When trying to run python from the command line, there is no response i.e. I do not get message about 'command not found' and console does not launch. Only option to open python console is to run C:\Python34\python.exe directly. Running using python command does not work even when in the python directory but python.exe launches. Issue with the launching this way is that python console is launched in new window. This whole problem is present only on one machine while on my other machine I am able to run python correctly and console launches in the command prompt window from which the python command was executed.
PATH is correctly set to
C:\Python34\;C:\Python34\Scripts;...
and where python correctly returns C:\Python34\python.exe. I verified that running other commands imported through PATH (such as javac) run correctly.
Things I tried:
Completely re-installing python both with x86 and x64 python installations with no success.
Copy installation from my second machine and manually set the path variables - again no success.
Can anyone hint how to resolve this behavior?
(Additional info: Win 8.1 x64, python 3.4.2)
Issue resolved. Since no feasible solution was found in 2 days, I decided to wipe all keys containing 'python' from registry as well as some files that were not parts of other programs. This resolved the issue after re-installing python.
If anyone finds the true cause of this misbehavior and other - less brutal - solution, please write it here for future reference.
Recent Python installer has option to add PATH.
If you didn't use it, you can register directory where python.exe is to PATH environment variable.
But I prefer py launcher. It may be installed via Python 3.3 or 3.4.
With it, you can start Python via py or py -3.4.
See https://docs.python.org/3/using/windows.html#python-launcher-for-windows

Categories

Resources