How to set path variable inside Spyder on Linux? - python

I am using a package that requires me to add the following line to my bashrc
export PATH=${PATH}:~/cozmo/plataform-tools
It works perfectly when I call python3 or ipython3 from the command line. However, when I start Spyder by double clicking a python file in Nautilus, it cannot find and execute a required file that is inside that folder. So, my question is
How to make the spyder console to use the PATH set at my bashr? Or how to set the path inside spyder?
I am aware of Why do the environment variables set in command prompt have no effect when I start Spyder, but it don't solve my problem, since I need to call it from Nautilus (and not use sudo) and I do not want to add a line declaring the path in all my programs.

(Spyder maintainer here) For Spyder to take notice of any environment variable set in your .bashrc, you need to start it from a terminal.
This is because Spyder doesn't have the ability to read environment variables when started in a graphical way.

I found a solution to setting a variable in ipython console. Just include
import os, os.environ['PATH']+=':/home/bernardo/cozmo/platform-tools
in Tools → Preference → iPython console → Startup → Lines. I believe a similar solution can be applied if Python console is being used instead of iPython console.
By doing this I am able to use the package that requires this path even when spyder is not started from the terminal.

Related

VSCode not selecting the correct python interpreter

I have a script that I need to run using a very specific python interpreter in my hard drive.
I am using vscode as the IDE and trying to select the python interpreter using the command palette.
However, since the concerned interpreter is not present, I manually search it.
However, even after that, it doesn't work as typing python in the terminal then opens the windows store for installing a python IDE. I also tried adding the interpreter's path to the environment variables but the problem still persists.
How do I solve this issue.
Thank You for the help.
The Python version you selected on VS Code only effects the editor. Right click to any file on the sidebar and select Open in Integrated Terminal. If your selected Python version is created with virtualenv it will activated and your python command works.
If not worked and you want a global Python version, you need to add Python path to your system variables to work python command. Check this:
https://geek-university.com/python/add-python-to-the-windows-path/
Btw I don't think your python.exe in FreeCad/bin directory is proper Python version
At the bottom left you can select the Python interpreter.
Here it shows you on -how to do that: Python Interpreter
Reason:
Your system environment path contains this: %USERPROFILE%\AppData\Local\Microsoft\WindowsApps.
Solution:
Open Window Search(Win) to find Manage app execution aliases. To turn off App Installer of python3 like this:
You can refer to this answer for more information.

Using system environment variables in spyder

I have a program coded in Python 2 that I need to run and I would like to run it in spyder via anaconda software.
The issue is that to run the program via the terminal I had to previously add in my system environment variables two new variables:
1) one folder in the computer where some required packages are
2) a variable with the ip address for the required license.
Although I have these variables define and the program runs without issues in the terminal. When I run it in spyder there is an error because it is not using those variables.
How can I fix this? I head something about anaconda creates virtual environments that is not loading those variables but I could not understand anything else. For example if I use pycharm, outside anaconda, it also works flawlessly
Thank you!
Emanuel
I believe Anaconda offers instructions on setting environment variables but that approach seemed complicated.
Setting an environment variable no longer seems to be an option in Spyder.
As a workaround Spyder does allow you to specify a startup file:
Preferences -> IPython Console -> Startup
In the startup file you just run something like:
import os
os.environ['my_var'] = 'my_value'
os.environ['test'] = 'test_value'
Every time you start a new ipython console this code will run.
There's an option in the Tools menu that let's you check/insert/update/remove the currently active environment variables in Spyder (pic for reference).
Spyder Environment Variables Feature
Edit: Regarding your PS question, when you install packages (through pip?) you are doing so through the currently active Python installation, which you can check by running
python -V
Therefore, if you intend to install a package in multiple installations, you need to use the corresponding package manager executable.
That dropdown menu is on Spyder 5.1.5

Adding to PATH using Spyder via Anaconda

I need to add the latex folder to my python PATH in order to render plots in latex font. I cannot figure out how to permanently add the latex file path to the python PATH. I can get it to add by running:
import os
os.environ["PATH"] = '/anaconda3/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/texlive/2018/bin/x86_64-darwin'
but as soon as I restart Spyder it everts back to the old path.
How can I permanently add
/usr/local/texlive/2018/bin/x86_64-darwin'
to the python path?
Also, I have tried to add it to the .bach_profile by modifying it as follows:
export PATH="/usr/local/texlive/2018/bin/x86_64-darwin:/anaconda3/bin:$PATH":
but it seems that python isn't looking here when it looks at its PATH.
Running print(os.getenv("PATH")) returns:
/anaconda3/bin:/usr/bin:/bin:/usr/sbin:/sbin
no matter what I do to the .bash_profile file.
I'm using MacOS X and ran into the same issue.
How I got around this problem is launching Spyder from Terminal, just by typing in your Terminal (assuming that the path to spyder is given):
$ spyder
By doing this, the environment variables should be inherited from Terminal (i.e. /.bashrc and /.bash_profile, etc).
Hope this helps.
Atsushi

Choosing specific python interpreter in the shell

I've been developing a set of scripts in PyCharm using C:/Python27/python.exe interpreter...
I'm creating a batch file and then running all of the scripts through this file from the cmd shell however the shell is not recognizing most modules because it is using the wrong interpreter (path for anaconda instead)...
How do I change the shell to use the C:/Python27/python.exe interpreter as default all the time instead?
I've tried looking this up but it all points to just adding the interpreter path, which I have... but the shell still uses the anaconda interpreter.
Any help appreicated
Take a look at : https://docs.python.org/3.3/using/windows.html#shebang-lines
Shebang lines at the beginning of scripts tells how a script should be executed.
Found a solution to my problem...
I needed to use:
set path=%path%;C:\Python27
This switched interpreter.
Cheers

How can I execute Python scripts using Anaconda's version of Python?

I recently downloaded the Anaconda distribution for Python. I noticed that if I write and execute a Python script (by double-clicking on its icon), my computer (running on Windows 8) will execute it using my old version of Python rather than Anaconda's version. So for example, if my script contains import matplotlib, I will receive an error. Is there a way to get my scripts to use Anaconda's version of Python instead?
I know that I can just open Anaconda's version of Python in the command prompt and manually import it, but I'd like to set things us so that I can just double-click on a .py file and Anaconda's version of Python is automatically used.
I know this is old, but none of the answers here is a real solution if you want to be able to double-click Python files and have the correct interpreter used without modifying your PYTHONPATH or PATH every time you want to use a different interpreter. Sure, from the command line, activate my-environment works, but OP specifically asked about double-clicking.
In this case, the correct thing to do is use the Python launcher for Windows. Then, all you have to do is add #! path\to\interpreter\python.exe to the top of your script. Unfortunately, although the launcher comes standard with Python 3.3+, it is not included with Anaconda (see Python & Windows: Where is the python launcher?), and the simplest thing to do is to install it separately from here.
Set your python path to the Anaconda version instead
Windows has a built-in dialog for changing environment variables (following guide applies to XP classical view): Right-click the icon for your machine (usually located on your Desktop and called “My Computer”) and choose Properties there. Then, open the Advanced tab and click the Environment Variables button.
In short, your path is:
My Computer ‣ Properties ‣ Advanced ‣ Environment Variables
In this dialog, you can add or modify User and System variables. To change System variables, you need non-restricted access to your machine (i.e. Administrator rights).
Find your PATH variable and add the location of your Anaconda directory.
Example of someone doing it here: How to add to the PYTHONPATH in Windows, so it finds my modules/packages? Make sure that you sub path out for the Anaconda file though.
The instructions in the official Python documentation worked for me: https://docs.python.org/2/using/windows.html#executing-scripts
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
The example shows how to associate the .py extension with the .pyw executable, but it works if you want to associate the .py extension with the Anaconda Python executable. You need administrative rights. The name "Python.File" could be anything, you just have to make sure is the same name in the ftype command. When you finish and before you try double-clicking the .py file, you must change the "Open with" in the file properties. The file type will be now ".py" and it is opened with the Anaconda python.exe.
I like to run a "bare-bones" version of Python 2 to verify scripts that I create for other people without an advanced python setup. But Anaconda and Python 3 have a lot of nice features. To enjoy both things on the same computer I do this on my Windows computer which allows me to easily switch.
C:\Users>python --version
Python 2.7.11
C:\Users>conda create --name p3 python=3
C:\Users>conda info --envs
Using Anaconda Cloud api site https://api.anaconda.org
# conda environments:
#
p3 C:\Anaconda3\envs\p3
root * C:\Anaconda3
C:\Users>activate p3
Deactivating environment "C:\Anaconda3"...
Activating environment "C:\Anaconda3\envs\p3"...
[p3] C:\Users>python --version
Python 3.5.1 :: Continuum Analytics, Inc.
For more info: http://conda.pydata.org/docs/test-drive.html
I know this is an old post, but I recently came across with the same problem. However, adding Anaconda to PYTHONPATH wasn't working for me. What got it fixed was the following:
Added Anaconda to the PYTHONPATH and remove any other distribution of Python from any paths.
Opened the command prompt and started python (Here I had to verify that it was indeed running under the Anaconda dist)
Ran the following lines inside anaconda
>>> import sys
>>> sys.path
['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
Copied the displayed path
Within the script that I'm trying to execute on double click, changed the path to the previously copied one.
import sys
sys.path =['','C:\\Anaconda','C:\\Anaconda\\Scripts','C:\\Anaconda\\python27.zip','C:\\Anaconda\\DLLs','C:\\Anaconda\\lib','C:\\Anaconda\\lib\\plat-win','C:\\Anaconda\\lib\\lib-tk','C:\\Anaconda\\lib\\site-packages','C:\\Anaconda\\lib\\site-packages\\PIL','C:\\Anaconda\\lib\\site-packages\\Sphinx-1.2.3-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\win32', 'C:\\Anaconda\\lib\\site-packages\\win32\\lib', 'C:\\Anaconda\\lib\\site-packages\\Pythonwin','C:\\Anaconda\\lib\\site-packages\\runipy-0.1.1-py2.7.egg','C:\\Anaconda\\lib\\site-packages\\setuptools-5.8-py2.7.egg']
Changed the default application for the script to 'python'
After doing this, my scripts are working on double click.
This solution is not perfectly answering the question because it modifies your .py file into a .bat file but it serves the save purpose, so I think it can help few people out there.
To launch a python script with anaconda's environment : change your .py extension into a .bat extension and add this command line in the beginning of your script :
#echo off & call conda activate <env name> & python -x "%~f0" %* & goto :eof
It will launch a .bat file that will activate your conda environment and execute your python script. This allows you to have a single file that you can copy/paste anywhere and just double click on it to launch it.
Here is an example of the content of the ".bat" file :
#echo off & call conda activate my_environment & python -x "%~f0" %* & goto :eof
print("Hello World!")
input()
import pandas as pd
print('succes')
input()
Hope this helps some people ! Cheers
Note:
If anaconda is not in yourr PATH environment variable, just replace :
conda activate with the path of anaconda activate .bat file, it should look like this C:\ProgramData\Anaconda3\Scripts\activate.bat.
You can also replace the environment name with it's directory, it often looks like that :
C:\Users\<user name>\.conda\envs\<environment name>
Many thanks to this post that helped me figure out this solution:
Python command line -x option
don't know windows 8 but you can probably set the default prog for a specific extension, for example on windows 7 you do right click => open with, then you select the prog you want and select 'use this prog as default', or you can remove your old version of python from your path and add the one of the anaconda
You can try to change the default .py program via policy management. Go to windows, search for regedit, right click it. And then run as administrator. Then, you can search the key word "python.exe" And change your Python27 path to you Anaconda path.
I know that this is an old question, but still there is no accepted answer.
Here is what I'm doing to start a python script with a double click in a specific anaconda environment.
I have found a very useful script in the anaconda3 folder cwp.py. This doesn't activate an environment, but it adds the environment prefix to the path.
You should use it as follow:
python cwp.py PREFIX ARGS
So actually, assuming that your anaconda installation is in C:\Users\User\anaconda3 and your environment is in C:\Users\User\anaconda3\env\myenv then what you have to do is to write the following line in a windows shortcut as a target:
C:\Users\User\anaconda3\pythonw.exe C:\Users\User\anaconda3\cwp.py C:\Users\User\anaconda3\env\myenv C:\Users\User\anaconda3\env\myenv\pythonw.exe path_to_your_script\your-script.py
A double click on the shortcut will start your script!

Categories