Is Activate.ps1 required to add packages in venv? vscode - python

New to utilizing virtual environments. I understand to concept but not the complete execution within vscode.
I have read the vsCode guide on using Python venvs and the vscode guide on getting started with python
I'm not understanding the link between how the Terminal window displays its in the venv and how vscode shows its using the selected virtual interpreter. I want to make sure I know which one I am working in so that I can effetely manage packages.
These two images demonstrate my point. They both show me that vscode is using the .venv interpreter based on the blue region pointed by the red arrow. But the first image shows the Terminal is in the venv while the second image shows the Terminal is in the venv. This is confusing to me because when I use "pip" to install via the terminal I want to make sure its downloading the package to the right venv and not to global. I would assume if vscode is using the venv interpreter the Terminal should also default to the venv.
Do you have to just run the .\venv\Scripts\Activate.ps1 in the Terminal anytime you want to run "pip" to add a new package in your venv?

When the virtual environment name is displayed in front of your terminal directory, it means that your current terminal is using the activated virtual environment. At this time, directly using pip command will use the pip.exe in the virtual environment, and the installed package will also be installed in the virtual environment.
If the virtual environment is not activated, if your current terminal does not display the name of the virtual environment, directly using the pip command may use the pip.exe in the global environment, that is, the python version configured by the computer environment variable, and the package will also be installed under it.
If you're worried, you can use the full install command. For example, the following command uses the complete python interpreter path to ensure that the sklearn package is installed under the virtual environment .venv under folder py12.
e:\workspace\py12\.venv\Scripts\python.exe -m pip install sklearn
PS: If you choose a virtual environment interpreter, then creat a new terminal will automatically activate the environment. This is controlled by the setting below, which defaults to true.
"python.terminal.activateEnvironment": true,

Related

PyCharm doesn't recognize packages installed via Terminal when environment is used in PyCharm in Pop_OS Linux

I'm using Pop_OS! Linux. Whenever I make a Python Virtual Environment using mkvirtualenv and then pip install packages into it, that environment does not work when I reference it inside pycharm. I set up virtual environments using virtualenv and virtualenvwrapper.
The workaround I've found is to build any virtual environment you need to use inside of PyCharm inside of pycharm, or using the terminal inside of PyCharm. I'm annoyed it doesn't seem compatible with my native terminal, and can't figure out why. I even get slightly different python versions by running python --version both inside the PyCharm terminal and in the native linux terminal, while inside the same virtual environment! It's like Pycharm isn't reading the same information out of the python environment directory as my terminal is.
What is going on here, and how do I harmonize PyCharm with the rest of my linux machine? I'm also new to Pop_OS! Trying it out for the first time is an adventure.
IDK if that will work, but maybe if you install your packages via the "Python packages" tab in PyCharm (down left -> add package next to the research bar that appeared), PyCharm will see them while it installed them itself.

Powershell venv only use envirnmental variables python, not python in venv directory (Windows)

I use vevn with python 3.6.8, pip 18.1.
I set powershell executionPolicy RemoteSigned,
create virtual environment with venv, refed to venv official site : (https://docs.python.org/3.6/library/venv.html)
and also activated according to powershell, used .<venv>\Scripts\activate.ps1
But when I execute 'pip list' command, it shows all packages installed in global, not in venv.
And when execute python - import sys - sys.prefix, to check python directory,
I can find, powershell venv uses environmental variables python.
Of course, run it on cmd in same way, it works correctly.
pip list shows
The pip list shows only packages installed in venv, and sys.prefix is python in venv directory.
Does anyone know why it works like this?
Powershell works correctly like cmd
Maybe you activated the "--system-site-packages" switch by accident when installing the virtual env. This switch makes all python packages in the global system available to the virtual env.
Also check that after activating the environment your powershell indicates that you are actually using the virtual env. This is indicated by (env_name) at the beginning of the new power shell line.

How do I ensure python project in VSCODE is using virutal envirnoment and not defaulting to global?

Having an issue when using flask and flask-wtf. The issue is that for some reason vscode is defaulting to global rather than the newly created virtual environment. In vscode I will click open folder, create the folder and open it in vscode. Then I'll write my code out and then open terminal in vscode and write: python -m venv my_venv, then on the lower left-hand side I will select it as my virtual environment. I then re-open the terminal, write: pip -V and the pip it is directed to is the folder directory with my_venv in the path name.
Then I pip install flask and flask-wtf, check that they are installed with pip list. Everything looks good until I run and vscode says: ModuleNotFoundError: No module named 'flask-wtf'. But when I go to my global environment and where flask is installed too it runs fine. Something is happening to where the python code is recognizing the original flask and not the one in my virtual environment. How do I ensure that the version of flask is the one in the virtual environment and not others installed on my machine.
Thanks
If you are in your terminal in vscode, it should append your command line with your virtual environment name.
So, usually it would show
PS \path
But if your environment is running, it will show
(venv name) PS \path
If it is now showing, be sure to reactivate your virtual environment from your project directory

Configure a Pipenv environment in pycharm

I am trying to add pipenv in project interpreter in pycharm but it throws an error.
These are the step that I followed :
-1- First in the command prompt I installed pip install pipenv
-2- Go to the directory of my project in the command prompt and typed pipenv shell (This command will look for the pipenv environment and load it, otherwise if not existing will create a new one)
-3- To install the last successful environment recorded (all packages and dependencies) typed pipenv install --ignore-pipfile to install production environment
-4- In PyCharm File->Settings->Project: ProjectName->Project Interpreter click add
-5- select pipenv environment, pycharm automatically shows pipenv executable
-6- Press OK - What am I miising here?
I installed pipeline globally and on my project directory
Runing pipenv shell on project directory shows this
Shell for C:\Users\gh8001\.virtualenvs\churn_prediction-UB3VC30i already activated.
No action taken to avoid nested environments.
In step 3, once you used 'pipenv install --ignore-pipfile' you already created a virtual environment. You need to get pycharm to use this existing virtual environment (not create a new virtual env). So, in step 5, instead of selecting 'pipenv environment', which seeks to create a new virtual env (and which I find doesn't work well for specifying exact python versions well), do this instead:
select 'Virtualenv Environment' tab
then 'Existing Environment' button
then point to the interpreter you created in step 3. You can find this interpreter by using 'pipenv --venv' for the root env directory, and then add /bin/python to the end.
Select OK, and the new pipenv should be in use
I faced the same problem. I spend half a day and finally found that the problem is in the stage of installing python.
Just uncheck the Install launcher for all users(recommended)
This option saves some of the python files such as pipenv.exe in the root C:\Users\your_pc\AppData\Roaming\ and causes the problem.
Hope this works for you.
pipenv executable should point to a pipenv.exe file, not a python.exe file. I don't know why pycharm would incorrectly detect this.
I think you may install pipenv using pip in anaconda. So you can find you pipenv executable in your anaconda/bin directory.

Do I need to activate virtual environment when using venv

Using Python 3.7.0 on Mac. Trying to use venv module that was added post python 3.4.
I setup my virtual env using python3 venv -m path/to/my/dir - my question is do I need to activate this virtual env to use?
The documentation seem to imply I don't need to?
You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically.
If I don't have to activate, what is the benefit of prepending venv to binary directory? Wouldn't this have to happen regardless for a venv to work?
Activating the virtualenv gives you convenience. It is never required.
Even for scripts that are configured to run with #!/usr/bin/env python, (which looks up the python executable on your path), you could manually update the PATH environment variable:
$ PATH="/path/to/venv/bin" some_script
Activating makes the PATH update stick until you deactivate again, and that can be convenient.
For example, I regularly have several virtualenvs in use at any one time. Some of them are there only to install some command-line tools I have symlinked into my ~/bin/ directory, another is home to a Jupyter notebook, and 2 more are used to quickly verify code snippets in different Python versions with access to 3rd-party libraries. I don't activate any of those.
When you don’t activate a virtualenv, all that happens is that your terminal PATH variable is not updated to put the bin directory of the virtualenv first, so when you enter python or pip or other script without any path into the terminal, the shell will find a different Python binary to run. You can always use any of the commands in the virtualenv bin/ directory by giving the full path to that command.

Categories