VS Code - Configure Run Python File In Terminal - python

I have a python environment called nem that I created using Conda, however in VS Code when I right click and select 'Run Python File in Terminal' I get the below error. BUT I know this package is not an issue as I can open up a new windows terminal outside of VS Code and run the commands below and everything works great.
activate nem
python index.py
I have also had issues where the output in the terminal is being suppressed even though the application is running. I'm not sure what the root cause of this is, and whether its directly related to Conda. However does anyone know of a solution to this? Or how to configure VS Code's 'Run Python File in Terminal' to just use the commands above.
Windows PowerShell Copyright (C) Microsoft Corporation. All rights
reserved.
PS C:\Users\nikhil\Git Repos\natatorium-energy-model> conda
activate nem PS C:\Users\nikhil\Git
Repos\natatorium-energy-model> &
C:/Users/nikhil/AppData/Local/Continuum/anaconda3/envs/nem/python.exe
"c:/Users/nikhil/Git Repos/natatorium-energy-model/index.py"
Traceback (most recent call last): File "c:/Users/nikhil/Git
Repos/natatorium-energy-model/index.py", line 8, in
from apps import analyse File "c:\Users\nikhil\Git Repos\natatorium-energy-model\apps\analyse.py", line 17, in
import pyarrow.parquet as pq File "C:\Users\nikhil\AppData\Local\Continuum\anaconda3\envs\nem\lib\site-packages\pyarrow__init__.py",
line 49, in
from pyarrow.lib import cpu_count, set_cpu_count ImportError: DLL load failed: The specified module could not be found. PS
C:\Users\nikhil\Git Repos\natatorium-energy-model>

In the bottom left corner, change the python interpreter to the one in your environment.

Related

VSCode Code Runner not activating virtual environment at the first run, but works for subsquent runs in the same terminal

Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
PS D:\Python_Workspace\Password Manager GUI> python -u "d:\Python_Workspace\Password Manager GUI\main.py"
Traceback (most recent call last):
File "d:\Python_Workspace\Password Manager GUI\main.py", line 4, in <module>
import pyperclip
ModuleNotFoundError: No module named 'pyperclip'
PS D:\Python_Workspace\Password Manager GUI> & "d:/Python_Workspace/Password Manager GUI/.venv/Scripts/Activate.ps1"
(.venv) PS D:\Python_Workspace\Password Manager GUI>
As you can see it runs activate.ps1 after the first run, I don't have this problem with python terminal in VS Code, but due to my reliance on Code Runner, its exhausting me. I tried executor map fix in settings.json, doesn't work.
It should be supported by the Code Runner, but the developer of Code Runner has not fixed this problem.
Some people have submitted this feature request in 2019 but without any response. refer to here
It seems like you need to bear on this inconvenience for now.

Conda environments in VScode

I have two Anaconda environments I created, say A and B, plus the base env.
I have created environments A and B specific to what I do with them (different libraries).
Now I installed VScode. When I open VScode and set first the correct interpreter (A or B) in the lower part and run some python programs specific to A and B, the program does not run and error appears.
D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\__init__.py:138: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
from . import _distributor_init
Traceback (most recent call last):
File "D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\core\__init__.py", line 22, in <module>
from . import multiarray
File "D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\core\multiarray.py", line 12, in <module>
from . import overrides
File "D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\core\overrides.py", line 7, in <module>
from numpy.core._multiarray_umath import (
ImportError: DLL load failed while importing _multiarray_umath: The specified module could not be found.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "d:\Do\Python\ML practice\iris\own-knn.py", line 15, in <module>
import numpy as np
File "D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\__init__.py", line 140, in <module>
from . import core
File "D:\Anaconda\envs\practica-mldl\lib\site-packages\numpy\core\__init__.py", line 48, in <module>
raise ImportError(msg)
ImportError:
IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!
Importing the numpy C-extensions failed. This error can happen for
installed.
We have compiled some common reasons and troubleshooting tips at:
https://numpy.org/devdocs/user/troubleshooting-importerror.html
Please note and check the following:
* The Python version is: Python3.8 from "D:\Anaconda\envs\practica-mldl\python.exe"
* The NumPy version is: "1.19.2"
and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.
Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.
PS D:\Do\Python\ML practice\iris> conda activate practica-mldl
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.
To initialize your shell, run
$ conda init <SHELL_NAME>
Currently supported shells are:
- bash
- cmd.exe
- fish
- tcsh
- xonsh
- zsh
- powershell
See 'conda init --help' for more information and options.
IMPORTANT: You may need to close and restart your shell after running 'conda init'.
But when I open VScode through anaconda prompt by first activating the environment and opening VScode by typing code and running some programs, the program runs.
So from what I understand, simply changing the interpreter in VScode is not the same as opening VScode by activating first the environment. I thought by just changing the interpreter in VScode will work.
Is there a setup in VScode where I do not have to open VScode by activating the environments first so I can run python programs specific to that environment?
Thanks a lot!
Edit 1. Added error

naoqi 2.5.5.5 and PyCharm CE 2017.1

I have an annoying problem with PyCharm CE and naoqi. Following the installation instructions in Aldebaran Guide, I have configured my environment to work with this one.
I have installed the version of python (python 2.7.13) suggested by Aldebaran, I have written the .bash_profile using:
# Setting PATH for Python 2.7
# The original version is saved in .bash_profile.pysave
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
#NaoQI 2.5.5
export PYTHONPATH=${PYTHONPATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib/python2.7/site-packages
export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:/Users/francesco/Documents/PyLibs/pynaoqi-python2.7-2.5.5.5-mac64/lib
after that, I launch my code in python from Terminal (making sure that the right version of python is called when I type
$ python my_code.py
and all is working (I tried also with virtualenv and all works fine if I launch all from terminal).
So far so good but then comes the poison arrow. I use PyCharm CE like IDE, I have created a new project that use the same environment that works fine in terminal and the magic happens..
The first issue is:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
ImportError: No module named qi
(Ok seriuosly this is the same environment that in terminal works)
I try to bypass the problem, adding the content of naoqi sdk in the virtualenv's site-package. Ok, the problem is bypassed.. But there is another problem:
/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/bin/python /Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py
Traceback (most recent call last):
File "/Users/francesco/PycharmProjects/PepperRobot/PepperRobot.py", line 5, in
import qi
File "/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/qi/init.py", line 88, in
from _qi import Application as _Application
ImportError: dlopen(/Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so, 2): Library not loaded: #loader_path/libqipython.dylib
Referenced from: /Users/francesco/Documents/PyEnv/pynaoqi255_python2713/lib/python2.7/site-packages/_qi.so
Reason: image not found
Process finished with exit code 1
I did not understand why...
I have also include in
Preferences -> console -> Python Console -> Environment Variables
the same variables included in .bash_profile but to no avail.
How can I solve this tedious problem ?
I have solved my problem editing run/debug configuration and putting here the environment variable DYLD_LIBRARY_PATH. All works fine.

Why does eclipse have such a issue importing standard python libraries

I just moved from sublime to eclipse and ran a program which contained the time library 'Arrow'.
It first said the no module could be found, so I then I added the source folder to the PyDev python path, now it's giving me this error:
Traceback (most recent call last):
File "C:\Users\David\workspace\Loan_rates\master.py", line 8, in <module>
import arrow
File "C:\Python27\Lib\site-packages\arrow\arrow.py", line 16, in <module>
from arrow import util, locales, parser, formatter
ImportError: cannot import name util
I think the problem is that you added:
C:\Python27\Lib\site-packages\arrow to your Interpreter PYTHONPATH -- which makes it resolve C:\Python27\Lib\site-packages\arrow\arrow.py as the arrow module.
The solution is removing C:\Python27\Lib\site-packages\arrow from the PYTHONPATH -- C:\Python27\Lib\site-packages should be enough in this case.
Try comparing the command line that works (when launched successfully from your terminal) to what PyDev is constructing.
To see the command line PyDev actually uses, open the launch configuration (Run menu -> Run Configurations... -> Python run -> Select your launch on the left) and in the Interpreter tab press See resulting command-line for the given parameters.
This is a screenshot of what I am referring to:

Error when running a python script

I am trying to run a script using an interface created with tkinter. I have a button that executes a script which code is:
subprocess.call("python3 " + PATH_TO_SCRIPTS + "main.py 1 &", shell=True)
However, when this button is pressed I am getting the following error.
Traceback (most recent call last):
File "/home/m//PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/main.py", line 17, in <module>
from controller import *
File "/home/m/PycharmProjects/ROSAutonomousFlight/catkin_ws/src/ardrone_numeric_method_controller/scripts/controller.py", line 5, in <module>
import rospy
It says that the module rospy does not exist, but when I run
import rospy
using python or python3 it is imported successfully. What can I do to solve this issue? I am using Ubuntu.
The comments to your question are mostly about Python, but I guess it is more of a ROS issue.
You don't have to set-up your PYTHONPATH manually to find rospy but you have to source the setup.bash of your catkin workspace (otherwise none of the ROS tools is found).
Usually this is done by adding something like
source ~/catkin_ws/devel/setup.bash
to .bashrc. This works fine for everything that is run in a terminal.
I don't know how you start your script but as it provides a graphical interface you probably just run it by double-clicking it in the file browser? If you indeed do so, the script is not run in a terminal and therefore can't find the ROS modules. Run the script from a terminal (in which the setup.bash has been sourced) and it should work.

Categories

Resources