In the PyCharm you can run Virtualenv Environment -> New Environment
What exactly does this command do? It creates new venv or virtualenv? And what is better to use for the project (Django)?
In PyCharm you create a project-specific isolated virtual environment and its purpose is to manage settings and dependencies of a particular project regardless of other Python projects.
According to its website, Python 3.3+ versions use the 'venv' tool, instead of the third-party 'virtualenv.
I would use virtual environment for any python project, because you can keep your dependencies seperate and contains everything needed for your project to execute successfully.
For my money, you're better of using Conda. The main reason being that Pip and Venv are Python specific package and environment management tools. Whereas Conda also looks after non-Python dependencies. Which can be very important for certain libraries.
Related
I seem to have some path issue while running Python(v3.9.1) in Visual studio code .The Azure libraries/SDK are not picked up while running the python file.
Do I need to set the Azure SDK in the Python path ?
from azure.core.exceptions import ResourceNotFoundError
ModuleNotFoundError: No module named 'azure'
Nope, that's not needed. But for every project, it is recommended that you always create and activate a python virtual environment using:
# This command runs the Python venv module and creates a virtual environment in a folder named .venv.
py -3 -m venv .venv
# Activate the virtual environment
.venv\scripts\activate
A virtual environment is a folder within a project that isolates a copy of a specific Python interpreter. Once you activate that environment (which Visual Studio Code does automatically), running pip install installs a library into that environment only.
When you then run your Python code, it runs in the environment's exact context with specific versions of every library. You can create a requirements.txt file for the libraries you need, then use pip install -r requirements.txt.
Here is a snippet from a sample requirements.txt file:
azure-cli-core==2.11.1
azure-cli-telemetry==1.0.6
azure-common==1.1.25
azure-core==1.8.1
azure-identity==1.4.0
azure-mgmt-compute==17.0.0
azure-mgmt-core==1.2.0
azure-mgmt-network==16.0.0
azure-mgmt-resource==10.2.0
If you don't use a virtual environment, then Python runs in its global environment that is shared by any number of projects.
Refer to the Azure SDK for Python Developer docs for more information on configuring your local Python dev environment for Azure.
I have a script (personal, not for distribution) that works on one of my computers. I want edit it on another computer.
On the first computer, the script was created without a virtual environment. I want to start using them on the second computer.
I have these set up on the second computer.
Windows 10
VS Code 1.45.1
Python 3.8.3
Paths:
Python - C:\Python38
Virtual environments - C:\Users\<User>\Envs
Projects folders - C:\Users\<User>\Documents\python-projects
Environmental variable WORKON_HOME is set to virtual environments path
I copied the script to the project folder. After activating the venv with workon, I pip installed the external modules on the second computer.
I can see the modules when the virtual environment is activated and not when it is deactivated. I believe this means the virtual environment is working and the modules were properly installed.
However, I keep getting the ModuleNotFound error for the external modules. I've tried uninstalling and reinstalling the venv and the modules but I keep getting the error. To ensure it's not the computer, I deactivated the venv and installed the modules and the script worked. I even tried creating a new script with only import requests and I still get ModuleNotFound.
What do I need to do to get the script to use the modules in the virtual environment?
You can do this multple ways. Some are listed here.
1.using IDLE
>>> help("modules")
2.using PIP
$ pip list
3.Using Anaconda
$ conda list
Before doing this, activate particular environment.
I am using ubuntu 18.04. I have Python-3.6.6 installed as a default version. But for practice I have installed Python-3.6.5 from source code to a directory where I wanted to install it and changed the .bashrc file accordingly. But it is showing me the default python3 path when I call "which" command.
How to use the python version which one I have installed recently?
If you only want to run some practice scripts you can select directly which python you want to use for your script. In the command line, e.g. type
PYTHONPATH=/foo/bar/python365 python somescript.py
where /foo/bar/python365 points to your python-3.6.5
You can also change your PYTHONPATH variable to look first in the python-3.6.5 folder before it looks to the default python (3.6.6) you had installed before. However, I do not recommend this, since it will make all python applications choose the python-3.6.5 as default. For extended practice with python-3.6.5 consider a virtual environment or an anaconda environment.
You should use virtualenv or something similar, like pipenv, I myself use virtualenv.
Here's a guide on how to do that: https://docs.python-guide.org/dev/virtualenvs/
virtualenv is a bit lower level and so not that intuitive to use, but since I used virtualenv before I found pipenv, I just stuck with it, while adding a few aliases to my .bashrc to make it easier.
I used two different python versions because discord.py would break on 3.7 (that doesn't seem to be the case anymore, but I still have it setup with 3.6 and 3.7), so here's what my .bashrc for two different versions looked like, this is on windows, but if you are on unix system or macOS (I want to make this answer as general as possible, it works the same on all operating systems really), just change the paths to lead to your interpreters (whichever you have) and don't use winpty:
alias python36="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe"
alias python37="winpty C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe"
alias pip36="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python36-32/python.exe -m pip"
alias pip37="C:/Users/Hevaesi/AppData/Local/Programs/Python/Python37-32/python.exe -m pip"
alias venv36="python36 -m virtualenv"
alias venv37="python37 -m virtualenv"
alias python="winpty python"
Now, to use Python 3.6, I use venv36 venv in my project folder, and it setups Python 3.6 environment, using venv37 venv would create Python 3.7 environment.
I also wrote a function to make activating less of a hassle (again, this is in bashrc)
activate() {
if [ "$#" == 1 ]; then
source "$1/Scripts/activate"
else
echo "Usage: activate <env_name>"
fi
}
So, after I created my env once, I go to project folder, type activate venv and it starts up whatever version I wanted the project to have all by itself, to run my project I just use python myfile.py, when virtual environment is activated, it will use the interpreter that's in active virtual environment, regardless of what you have in your PATH variable. Also, using pip will automatically install to your virtual environment, not your actual installation, meaning global package list will be clean, or in best case, completely empty.
You should only install what you need and keep it separate environments, normally you use a single environment per project. Well, unless your project is client/server at once (for whatever reason) and server uses different libraries than client, for example, more of them, client environment could have less modules since that's what the server is for, to provide something without having to do it yourself, so they could be two projects in one, hence two different environments, separating dependencies based on what the "unit" in the project really needs.
Note: you have to install virtualenv (or pipenv or whatever you want to use really) into all of your python installations, if you want to create virtual environments from them, that's what pip36 and pip37 here is for - for installing packages directly into actual python installations, for now, I only had to do pip36 install virtualenv and pip37 install virtualenv.
I use Pycharm to create and manage my virtualenvs in my projects.
The problem is that after adding a library with pycharm, when I type the command (pip3 freeze --user), the library does not appear in the command result.
I have to manually type the pip install command each time so that the library is visible.
What manipulation should I do in PyCharm to solve this problem?
For what you are saying, the first thing that comes to mind is that you should use:
pip freeze
And not
pip3 freeze
Because the command mapped to the pip version when you have virtualenv activated is the first. Note that for installing you seem to use pip, and not pip3
Moreover, the --user option afaik is related to the packages installed in the user folder:
--user Install to the Python user install directory for your platform. Typically
~/.local/, or %APPDATA%\Python on
Windows. (See the Python documentation for site.USER_BASE for full details.)
If your packages are installed in the virtualenv folder, I would tell you to not use that option.
Also please make sure you have your virtualenv activated. In linux you can do so by source path/to/virtualenv/activate
Edit
I understand that the reason you are using pip3 is because you may have different versions of Python in your machine. Let me explain you a bit further how it works, because version management is usually a headache for many programmers and it is common to find problems when doing so.
If you install different versions of Python in your linux machine, and you do that as root, then the installation will proceed for the whole system. Usually Python2 installation folder for Linux machines is /usr/bin/python. However, I am uncertain of which directory is used for Python3 installations. You can check that easily by doing whereis python3. You can serach the path to binary of any command by doing whereis command. Note that this works also for whereis python as far as you don't have virtualenv activated.
Aditionally, the link to the binary of a command (or the set of instructions to be exectued, more broadly) is defined in certain folders in Linux, depending on whether you created the command as root or as a user, and possibly also on the distro. This works differently in Windows, that uses the Registry Edit utility to handle command mappings. When you enable your virtualenv, what you are doing is creating an environment that enables mapping system commands such as python to the Python installation in your virtualenv folder.
When you disable the virtualenv, the command points again to the default installation path. Same happens with pip, so incorrect usage of this tool may result in different packages being installed in different locations, and therefore not appearing available for the right Python version at any given circumstance.
In Linux, environment variables are shell dependent, though you can write them out with echo $variable and set them with variable=value (from bash). The search path is simply called PATH and you can get yours by typing echo $PATH.
Source: https://askubuntu.com/a/262073/426469
I encourage you to check other questions in SE network such as this: https://unix.stackexchange.com/a/42211/96121, to learn more about this.
Addendum
Quick tip: it is common to use the pip freeze command as follows:
pip freeze > requirements.txt
It is a standard that leads to understanding that modules in such file are required for the correct functioning of your application. That lets you easily exclude the virtualenv folder when you install the program in another computer, since you can readily know the requriments for a fresh installation. However, you can use the command as you want.
Ok, I'm sorry if this has been asked before, I did found some info here but I'm still stuck.
I have Anaconda 3 (python 3.6) on Windows.
Created a new environment using
conda create --tf python=3.5
That works just fine and in cmd I can activate it. But I get this nice error when running python.
I kind of get the idea my issue is my win environment variables.
But I have tried to added my Anaconda path to the beginning of my path variable and my new tf environment path to the beginning. But no luck. Is this some sort of conflict between python 3.6 and 3.5.
Thanks
[Moved from comments]
Somehow or other you're trying to import from your root install even within your activated env, and that root install (being Python 3.6) uses async + yield in its stdlib, which isn't supported in the 3.5 you want to use.
When using Anaconda, you shouldn't have either PYTHONPATH or PYTHONHOME set (and if there are other PYTHON* environment variables set, might as well clear them too!)
These cause problems because these variables are very powerful and the interpreter winds up obeying them. In the case of multiple environments and/or multiple Python distributions on the same system, it's best to leave them alone.