Using system environment variables in spyder - python

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

Related

How can i add Anaconda Prompt to VScode terminal?

i'm trying to add anaconda prompt to vscode terminal.
i googled it, but i found that it's not possible method cuz
terminal.integrated.shell.windows
terminal.integrated.shellArgs.windows
is now unavailable on settings.
is there any way to solve this?
I'm sorry to tell you that it can't be realized at present.The Python extension automatically detects existing conda environments provided that the environment contains a Python interpreter. Although the Python extension for VS Code doesn't currently have direct integration with conda environment.yml files, VS Code itself is a great YAML editor.
The most I could think of is setting Conda as your interpreter (in the Command Palette type in Python Interpreter, select it, and click on the one that says "conda"). That should allow you to run your code in the conda enviornment.

Python Module Can Not be Found Even Though It Is Installed

I used python and pandas so many times in VS Code but It causes a problem right know : problem
The terminal looks like this : terminal and I checked if matplotlib is installed or not but seems like it is installed matplotlib check. I don't know why this is happening but It was working until now.
The reason is that the python environment you are currently using in VS Code is different from the python environment used by the VS Code terminal.
Solution: Please close the currently used VS Code terminal (click the icon "Kill Terminal"), then select the python environment that has installed the required module at the bottom left of VS Code, and then use the shortcut key Ctrl+Shift+` to open the new VS Code terminal.
In addition, I noticed that you are using a powershell terminal. If you are using a virtual environment or conda environment, we need to activate this environment in the terminal (Usually it will automatically activate when a new terminal is opened.):
Check module:
Run:
Reference: Using Python environments in VS Code.

VS Code Using Python 2.7.10 instead of Python 3.7.6 after activating virtual environment

I have had trouble for months using packages and libraries in VS Code. Everytime I create and activate a new python virtual environment in VS Code, it defaults back to using the MacOS pre-installed python 2.7.10. A picture below shows my issue. I would like to see "/usr/local/bin/python3" when I type the command "python --version", instead I get "/usr/bin/python". Does anyone know how to fix this? Thanks.
Picture of problem
I'm not a MacOS user and can't see your picture due to my network, but you can create the virtual environment with a specific version of Python.
On Windows this is:
python3.8 -m venv envname
If you are creating a virtual environment by using python -m venv envname the issue is the default interpreter on your PC is set to Python 2. A quick search shows you might be able to set a different global version on a Mac with something like pyenv global 3.8.6. I'm not sure if this would have other implications for you.
Please try to open a new VS Code terminal to let it reload the currently selected python environment. (Terminal; New Terminal; shortcut keys: Ctrl+Shift+`)
For the global python environment in VS Code, even though we have switched the python environment, sometimes its internal terminal still uses the first python of the python environment variable. Therefore, it is recommended that you move the python you need to use to the first: (Please reopen VS Code after setting.)

Anaconda Environment on VS Code

In general I am new to python so keep it easy. I have started my coding in Spyder since it was easy to install libraries with Anaconda.
Now we want to switch to VS Code for version control. I have switched interpreter and python path to Anaconda folder, but if I open python files I have created in Spyder I can't use in VS code, issue always is with importing libraries.
Screenshot
The first rule with Anaconda is to not manually mess around with the paths. Anaconda has a different philosophy: Before you can use Python, you need to activate a conda environment.
c:\> conda activate
(Anaconda3) c:\> python
>>>
VSCode detects conda environments and you just have to select one for your project (Python: Select interpreter). Now open a new terminal and watch VSCode activating the environment for you (like shown above).
However, when you start your script for the first time, VSCode sometimes invokes the interpreter even before the conda activation has finialized. Then simply restart your script.

How to set default interpreter and keep things in order?

I was required to install anaconda for a CS course and used spyder and Rstudio.
Then, for a different class I used pycharm.
When I type on the command line "python -V" I get:
Python 3.6.1 :: Anaconda 4.4.0 (x86_64)
and I have no idea why it relates the python version I have installed with Anaconda (and why not pycharm?). I understand that the OS runs python 2.7 (shouldn't I get that instead? and when I type python3 -V get which version of python 3 I have?) and when I use something like Pycharm or Spyder I can choose which version I want from the ones I have installed and use it within the program, not for the terminal.
I just want to have everything in order and under control. I don't think I understand what Anaconda really is (to me is like a program that has more programs in it...). How do I keep anaconda to itself ? 1313
Also, should the packages I installed through Terminal work on both pycharm and spyder/anaconda even though when I used pycharm I used python 3.5 and anaconda 3.6?
I think I need definitions and help to get everything in order in my head and the computer.
Pycharm is just an application to help you write code. Pycharm itself does not run python code. This is why in PyCharm, you need to set the interpreter for a project, which could be any python binary. In PyCharm, go to Preferences > Project > Project Interpreter to see where you would set the python environment being used for a given project. This could point to any python installation on your machine, whether that is the python 2.7 located at /usr/bin/python or a virtual environment in your project dir.
The industry standard way to "keep things in order" is to use what are called virtual environments. See here: https://docs.python.org/3/library/venv.html. A virtual environment is literally just a copy of a python environment (binaries and everything) so whatever directory you specify. This allows you to configure your environment to however you need in your project without interfering with other projects you might have. For example, say project A requires django 1.9.2 but project b requires 1.5.3. By having a virtual environment for each project, dependencies won't conflict.
Since you have python3.6, I would recommend going to you project directory in a terminal window. Running python -m venv .venv to create a hidden directory which contains a local python environment of whatever your 3.6 python installation. You could then set your project interpret to use that environment. to connect to it on the command line, run source .venv/bin/activate from where you created your virtual environment. run which python again and see that python is now referencing your virtual environment :)
If you are using a mac (which I believe you are from what you said about python2.7), what likely happened is that your anaconda installer put the Python bin directory on your PATH environment variable. Type in which python to see what the python alias is referencing. You can undo this if you want by editing your ~/.bash_profile file if you really want.
You are more or less correct about anaconda. It is itself another distribution of python and contains a load of common libraries/dependencies that tend to make life easier. For a lot of data analysis, you likely won't even need to install another dependency with pip after downloading anaconda.
I suspect this won't be all too helpful at first as it is a lot to learn, but hopefully this points you in the right direction.

Categories