29
loading...
This website collects cookies to deliver better user experience
virtualenv
and poetry
, and software such as IntelliJ IDE via Python plugin, PyCharm Community Edition, VSCode, Windows system, and Git Bash terminal.Indexing is a core JetBrains features: code completion, inspections, finding usages, navigation, syntax highlighting, refactoring, and more.
serpapi
library which is installed globally (system-wide) with a 1.15 version. import
a serpapi
library.../site-packages
directory, this leads to the problem when you have two projects that require different versions of the same library and globally installed library have a completely different version. virtualenv
and poetry
modules, and deactivating virtual environment when done.virtualenv
:$ pip install virtualenv
$ virtualenv env
virtualenv
module. # On Windows
source env/Scripts/activate
(env)
# On Linux
$ source env/bin/activate
(env) $
(env)
indicates that you're in the virtual environment.
(env) $ pip install google-search-results
==
sign:(env) $ pip install 'google-search-results==1.3.0'
"
when specifying versions:pip install 'google-search-results==1.3.0'
ERROR: Invalid requirement: "'google-search-results==1.3.0'"
(env) $ pip install -I 'google-search-results==1.3.0'
virtualenv
) and create a virtual environment for a specific Python version:# install package for specific Python version (https://bit.ly/3pXtHng)
# For Windows:
$ py -3.6 -m pip install virtualenv
# For Linux
$ python3.6 -m pip install virtualenv
# create venv for specific Python version (https://bit.ly/3EjH0ni)
$ virtualenv -p python3.6 test_env
poetry
examples for PyCharm, IntelliJ, and VSCode. poetry
cache folder via command line to find a path to python.exe
file because the env
folder is already in your project directory that was created earlier above.(env) $ deactivate
$
poetry
:$ pip install poetry
poetry
) for a specific Python version:# For Windows:
$ py -3.6 -m pip install poetry
# For Linux:
$ python3.6 -m pip install poetry
poetry
inside current package/project directory:$ poetry init
The init
command will ‘initialize’ an existing directory and create a pyproject.toml
which will manage your project and its dependencies:
# pyproject.toml file
[tool.poetry]
name = "virtual environments"
version = "0.1.0"
description = ""
authors = ["Dimitry Zub <[email protected]>"]
[tool.poetry.dependencies]
python = "^3.9"
google-search-results = "^2.4.0"
# other site-packages will appear here..
[tool.poetry.dev-dependencies]
# development dependencies will appear here..
[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
What the heck is pyproject.toml
?
In short, pyproject.toml
is the new unified Python project settings file that contains build system requirements and information, which are used by pip
to build the package/project, and it is almost a replacement for setup.py
.
pyproject.toml
creation, $ poetry init
will interactively ask you to fill the fields about your package/project:--name
: Name of the package/package.--description
: Description of the package.--author
: Author of the package.--python
Compatible Python versions.--dependency
: Package to require with a version constraint. Should be in format package:1.0.0
(version).--dev-dependency
: Development requirements, see --require
.$ poetry add google-search-results
...
Creating virtualenv PROJECT-9SrbZw5z-py3.9 in C:\Users\USER\AppData\Local\pypoetry\Cache\virtualenvs
Using version ^2.4.0 for google-search-results
Updating dependencies
Resolving dependencies...
Writing lock file
Package operations: 1 install, 0 updates, 0 removals
• Installing google-search-results (2.26.0)
add
command adds dependencies to pyproject.toml
and poetry.lock
, and installs them.Creating virtualenv
will create a virtual environment with the showed path. Environment creation will be done once.Writing lock file
will write dependencies to poetry.lock
file.poetry.lock
prevents from automatically getting the latest versions of your dependencies.
You can explicitly write lock
command to lock dependencies listed in the pyproject.toml
# multiple ways
# double quotes ("foo") for Windows CMD
$ poetry add google-search-results@^2.1.0
$ poetry add 'google-search-results>=1.8.5'
$ poetry add 'google-search-results==1.8.5'
$ poetry add google-search-results@latest
If you specify a constraint (@
or >=
), the dependency will be updated by using the specified constraint.
Otherwise, if you try to add a package that is already present, you will get an error.
poetry.lock
or pyproject.toml
files, you can install those dependencies to the virtual environment:$ poetry install
The install
command read pyproject.toml
or poetry.lock
file and installs all listed dependencies.
If there's a poetry.lock
file:
poetry.lock
.If there is no poetry.lock
file:
pyproject.toml
file and downloads the latest version of their files. --no-dev
argument:$ poetry install --no-dev
poetry
, find a location of the initialized environment first via config --list
command. Look for virtualenvs.path
in the output:$ poetry config --list
cache-dir = "C:\\Users\\USER\\AppData\\Local\\pypoetry\\Cache"
experimental.new-installer = true
installer.parallel = true
virtualenvs.create = true
virtualenvs.in-project = null
👉virtualenvs.path = "{cache-dir}\\virtualenvs" 👉👉👉# C:\Users\USER\AppData\Local\pypoetry\Cache\virtualenvs
Go to the virtualenvs.path
folder and open created environment folder (in my case its: PROJECT-9SrbZw5z-py3.9
).
Go to Scripts
(Windows) or bin
(Linux) folder, copy the full path and add python.exe
at the end of the path:
C:\Users\USER\AppData\Local\pypoetry\Cache\virtualenvs\PROJECT-9SrbZw5z-py3.9\Scripts\python.exe
virtualenvs.path
is needed to find a path to python.exe
inside created virtual environment which will let JetBrains or VSCode to index installed site-packages.
virtualenv
, go to env\Scripts\python.exe
folder in your project and copy the full path to the python.exe
file and enter it as a System Interpreter inside IDE.serpapi
, for example) and will throw an error because globally there's no such library installed (it won't throw an error if it's installed):Traceback (most recent call last):
File "C:\Users\USER\PyCharmProjects\PROJECT\environment.py", line 1, in <module>
from serpapi import GoogleSearch
ModuleNotFoundError: No module named 'serpapi'
CTRL+SHIFT+P
and type: Python: System Interpreter (Python extension should be installed).virtualenv
and poetry
, VSCode should automatically detect a proper python.exe
file from the virtual environment.python.exe
from your virtual environment then you need to locate and enter it.python.exe
from the virtualenv
folder and set it as a PyCharm System Interpreter which will index all site-packages from the virtual environment:python.exe
from the virtualenv
folder as well and set it as a PyCharm System Interpreter with a few additional tweaks which will index all site-packages from the virtual environment:virtualenv
prefix for example: "Python 3.9 virtualenv.." ==> "Python 3.9", a reverse process of what's being shown above.