I work in a Devops team in-charge of a large development environment for a lot of engineers.
We have a git repository and use python virtual environment (pipenv) for the code.
Our way of setting up the dev environment on the engineers laptop is with a script that installs the necessary tools + the pipenv and all packages.
Now, when it comes to Pycharm, things get complicated a bit.
The .idea directory is off course not part of the repo and each user needs to configure the project himself.
The most encountered problems I have is when the user fail to configure the proper interpreter for the project. The env is installed outside the repo (~/.local/share/virtualenvs/...) and Pycharm doesn't properly detect it and needs to be configured manually by going to interpreter settings and creating a new interpreter.
I was wondering if there is a way to automate this? As the interpreter path for each user is different, I can't just add the .iml file to the repo.
Related
I have software products built in Python, each of which has its own git repository and each of which runs in its own Docker container.
The development container can run locally or remotely, and I use VisualStudio Code (VSCode v1.73.1) as my development environment, and it connects to the container and lets me develop and test the code in the container. I am able to use the VSCode environment to get a terminal into the Docker container, and I use Poetry for package management and the Python virtual environment, and Pytest and the VSCode plugins for Python for testing, including code coverage via Pylance and the Coverage Gutters extensions.
Setting up VSCode for this is tedious and very error-prone. There is a lot of "hit and miss" before I get the environment just right. I have to install extensions for VSCode for things like remote servers, Docker containers, extensions for the plugins, and configurations to choose the right version of Python for my testing environment, along with Pylance and Coverage Gutters. If I ever put away my VSCode environment for one product for a few months because I'm working on something else, I have to go through the pain of setting up everything from scratch and by hand.
On the flip side, if I'm working on a project for which its VSCode is configured properly, and my machine has to restart, the environment is recreated when I start up VSCode again. This tells me that there exists configuration data somewhere which tells VSCode the state to which it should restore itself.
I would like to be able to store this configuration data in the git repository itself so that as soon as I open the project folder in VSCode, my entire configuration comes up. This would include all local extensions, extensions for containers, and configuration for the local environment and extensions. Barring that, if I could go into my repository, edit a file or two (perhaps to point to a root directory, a remote machine and a container), and then start up VSCode, that would be good. I am currently able to save some settings which work. There is a file in ~project_root/.vscode/settings.json which looks like this:
{
"python.testing.pytestArgs": [
"tests",
"--cov-report=xml",
"--cov=src/project"
],
"python.testing.pytestEnabled": true,
"python.formatting.provider": "black",
"python.analysis.extraPaths": [
"./src/project"
],
}
There are no other files in ~project_root/.vscode/.
QUESTIONS:
How and where in my git repository can I store the full configuration for the environment besides ~project_root/.vscode/?
When VSCode starts up, how can I tell VSCode to get access to the full environment from the saved configuration?
I appreciate any guidance I can get.
Your problem is similar to here. You can try the vscode Insider version, where the Settings Profiles feature is provided.
For setting a specific python interpreter, you can use the following settings in settings.json to point to the default interpreter.
"python.defaultInterpreterPath": "<path_to_python.exe>"
In addition, vscode will use the interpreter last selected for this workspace by default every time the workspace is opened.
This is the massage that Pycharm gives me almost once a day, and I have to restart it. As I have multiple projects open, it gives this error for each virtualenv repeatedly until I force quit it.
Is there a way to prevent Pycharm from constantly invalidating cache?
#ps: I never had such issues with Pycharm on Windows
Invalid Python SDK
Cannot set up a python SDK at Python 3.9 (demographics-g5XoraTQ) (/Users/mamad/Library/Caches/pypoetry/virtualenvs/up-demographics-g5XoraTQ-py3.9/bin/python). The SDK seems invalid.
It turns out the issue was the number of git repositories each with a separate project SDK (i.e. venv interpreter) that I had simultaneously opened in my PyCharm instance (over 10).
The re-indexing of git caches and Python libraries created memory issues and eventually resulted in corruption of index files; this couldn't be solved unless I restarted my PyCharm instance once a day.
The solution was to:
Either re-use one virtual environment for all projects; which is not desirable at all.
Or, as soon as I am done with a project, remove the project along with its Python interpreter and git repository from PyCharm. To speed things up, I set my Poetry to create its .venv inside the project folder; so interpreter and project both can be removed in a single command.
One undesirable outcome of the second solution is that my shell now displays one venv name for all my virtual environments in all project folders.
I am fairly new to creating Python applications. I have fooling around with some small tutorials and applications using PyCharm and have always created a new project using the Virtualenv environment, ending up with a "venv" folder under my project folder. I have not had any problems with this, but then again I have not done any large projects.
However, I have been wanting to learn Flask want to try to create a new Flask project the proper way. I see in many tutorials that people are creating (and activating) the virtual environment from the (Windows/Linux) Command Line instead even though they are using PyCharm and I was just wondering what the difference is?
When I work on a project in PyCharm, created with Virtualenv, I do not activate the venv before working on it. Is this wrong or is this something that is handled by PyCharm? What if the venv is created from a Command Line? Is it still handled (activated) by PyCharm if working on the project there. And what about the folder structure? Is this affected by how the virtual environment is created? Is there somewhere I can find some "best practices" for the setup / folder structure when creating Flask project within a Virtual Environment?
PyCharm activates the VirtualEnv for you if it is configured to use one and told where it is (more specifically, where the respective Python binary in the VirtualEnv is).
There's no real difference between manually created VirtualEnvs and ones created by PyCharm. (Apart from the framework you select to create one in case this is different from what PyCharm is configured with.)
If you want, you can just create one manually and then point PyCharm to it. Either during creation of the project or later using the Settings dialog (see Settings -> Project -> Project Interpreter). It will then treat it no differently and also activate it for you when working inside the IDE.
A virtual environment is pretty much just a folder which stores installed Python packages and isolates them from the rest of your system. This is so you can work on different projects which may all have competing requirements for external packages, without getting into conflicts. "Activating" a virtual environment just sets certain environment variables in your current shell so it'll use packages from this environment. "Activating" an environment never has any impact beyond your current shell. So activating an environment on the command line won't do anything to PyCharm.
PyCharm integrates a Python interpreter to give you lots of extra functionality. You tell PyCharm which interpreter you want to use for your project and it'll figure out what packages it has available, what version it is, and automatically set everything up properly for running your code from PyCharm etc. You can tell PyCharm to use your system's Python interpreter or an existing virtual environment or even use it to create a new environment. You don't need to do anything special beyond just selecting the right interpreter/environment in the project settings.
There's no reason to activate the environment from the command line if you're not going to use it from the command line. Of course, using Flask and running its server from the command line and keeping it running in the background may be useful. Not sure if PyCharm would give you an easy or integrated option to have persistent processes run in the background. You could still select the same virtual environment in PyCharm and use it to run your tests in it directly from PyCharm, use its debugger etc.
I prefer to keep the venv out of the project folder and store all venvs in ~/.virtualenvs/ or such. It declutters the project folder and prevents accidentally checking those files into the version control system.
I was just wondering what the difference is?
There's many tools for creating and using virtual environments and there's no difference between them, the only difference between them is their commands syntax (or the way it interact with users, e.g. for Pycharm you set some settings via GUI).
Is this wrong or is this something that is handled by PyCharm?
There's nothing wrong with it. As long as you have a venv (or .venv) directory in the root of your project and it is executable for any user, Pycharm will use it and it activates this virtual environment for you (without telling you). If Pycharm was not able to do that, (because of trouble in finding venv or activating/executing it!) then it will show you messages to fix its problems and it can't run your project till you fix them.
It's better to create your virtual environment in .venv directory right into the root directory of your project. (It's kind of conventional)
See python virtual environments and configuring pycharm virtualenv as well.
been searching for this with no success, i don't know if i am missing something but i have a virtualenv already but how do i create a project to associate the virtualenv with, thanks
P.S. Am on windows
I could be wrong here, but I do not believe that a virtualenv is something that is by its very nature something that you associate with a project. When you use a virtualenv, you're basically saying, "I'm taking this Python interpreter, installing what I want on it, and setting it aside from the Python interpreter that the entire computer uses by default." Virtualenv does not have a concept of a Python "project"; it is just a custom version of a Python interpreter that you run code through. There are tools in IDEs like PyCharm that enable you to associate a project with a virtualenv, but those are another layer on top of the base software.
In order to use a virtualenv with a project, you will need to "activate" it every time you wish to use it. The documentation for activating a virtualenv on Windows is found here.
EDIT:
Saw that you had virtualenvwrapper tagged in your post, so I did a bit of hunting on that. It would appear that there is the mkproject command, which creates a project folder and then associates it with a virtualenv interpreter. Documentation on it can be found here.
Requirements:
Virtual Env
Pycharm
Go to Virtual env and type which python
Add remote project interpreter (File > Default Settings > Project Interpreter (cog) add remote)
You'll need to set up your file system so that PyCharm can also open the project.
NOTE:
do not turn off your virtual environment without saving your run configurations that will cause pycharm to see your run configurations as corrupt
There's a button on the top right that reads share enable this and your run configs will be saved to a .idea file and you'll have a lot less issues
If you already have your virtualenv installed you just need to start using it.
Create your projects virtual environment using virtualenv env_name on cmd. To associate a specific version of python with your environment use: virtualenv env_name -p pythonx.x;
Activate your environment by navigating into its Scripts folder and executing activate.
Your terminal now is using your virtual environment, that means every python package you install and the python version you run will be the ones you configured inside your env.
I like to create environments with the names similar to my projects, I always use one environment to each project, that helps keeping track of which packages my specific projects need to run.
If you haven't read much about venvs yet, try googling about requirements.txt along with pip freeze command those are pretty useful to keep track of your project's packages.
I like Pipenv: Python Dev Workflow for Humans to manage environments:
Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever-important Pipfile.lock, which is used to produce deterministic builds.
Pipenv is primarily meant to provide users and developers of applications with an easy method to setup a working environment.
I’m starting to learn Python using PyCharm IDE on windows. I have a specific question about where to start adding/ creating python files/ classes.
I installed Python 3.5 and installed PyCharm Community. Created a project first and then created a virtualenv. I can therefore switch between 'Python Interpreters'. But, when I open PyCharm project, I see a folder structure and I can’t make a decision where to start creating code files. I've added screenshot as reference.
Where to start adding creating code file packages? And, is this the right way of creating virtualenv?
I'd also appreciate if you could please suggest any related best practices, especially regarding using virtualenv in this scenario.
Create the virtualenv while creating the project. Click the gear icon to the right of the interpreter box at the new project dialog box. Choose "Create VirtuanEnv"
Then you can right click the project name and add a python file.
Typically when developing python code on windows using PyCharm, I create a subfolder in my User directory (C:/Users/Vasili/virtualenvs/), where I place all of my future virtual environments.
When creating a new project, I create a new virtualenv in that folder and set it as the interpreter for the project. PyCharm will use that venv every time you open that project. It will also activate it whenever you use the inline terminal within PyCharm so you can run executables such as nosetests, if you have it installed in the project virtualenv.
As for general project structure, python is fairly informal when it comes to this, but there are some standards that you could try to adhere to, such as:
creating a setup.py file with your project metadata and other data to allow uploading the project to pypi, or even building RPMS.
a requirements.txt file with packages that pip must install for the project to work
A tests folder with your unit tests, fixtures and so on
The actual package itself, named after your project, with a dunder init script (__init__.py) inside, to signify that it is a package
An open source license
A readme, or documentation that can generate HTML docs with sphinx
Anything else you think would help people use your software.
As you are using community edition, you will have some limitations of the features.
I would suggest you to create virtual env from terminal/command prompt.
This is the folder structure which I follow for development while using Pycharm:
C:\workspace\<virtual_env>
C:\workspace\<project_name>
Add the whole workspace folder to your Pycharm as project.
In order to create and activate virtual env in windows refer the following:
https://docs.djangoproject.com/en/1.10/howto/windows/