Activate existing venv on pycharm - python

I created venvs for couple diff projects using python3 -m venv my_project
Also use pyenv local version for managing the python version for each project as needed.
My projects folder looks like this:
my-project-one/
my-project-two/
my_project_one/
my_project_two/
The ones with hypens are the repo while the ones with underscores are the venv I create for them. (maybe stupid naming scheme but works for me).
I activate the venv with source ../my_project_one/bin/activate from within the one I want to work with.
It has been great since I was using VSCode. Now I want to use Pycharm and I can't figure out how to make venv work with Pycharm.
In the project interpreter section I can see the two versions under ~/.pyenv/versions/ but that doesn't detect the project dependent requirements I have installed on the venv.
Many articles mentions about adding the python executable unders Scripts folder in venv but there is no such folder in any of mine. (on MacOS)
What am I missing here? Did I create the venv incorrectly or following the instruction for the wrong implementation of virtual env because I really don't want to mess with a working setup just for Pycharm.
I hope it would at the very least detect the project level libraries and not show unresolved reference for them.

Related

PyCharm Virtual Environment Concept - Understanding [duplicate]

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.

Python venv programmatically

We are trying to add venv functionality to our python project ie when one runs project an venv is created in project path and project runs on that venv.
For this to work we used vurtualenv's create environment method and activatethis.py the method creates venv ,
However when project runs it still uses host machine packages. Instead of using venv ones
Am I doing anything wrong..is it possible to do this??
PS: I have tried both site-packages true and false in create environment method .. however to no help
Adding more info:
Commands used are
venv_dir = os.path.join("path to project")
virtualenv.create_environment(venv_dir)
execfile(os.path.join(venv_dir, "bin", "activate_this.py"))
Also updated path environment variable to include bin of venv
After this I expect my code (after this) to run in virtual environment.
I am not using any ide and expect the code to create venv at runtime
Using python 2.7
Is there any other way to run project in virtual environment at run time without any ide in Ubuntu server environment.
You haven't provided enough information for us to come up with a solution to your exact problem, although I will do my best to help you out.
You should not be creating a venv every time you run the project, which it sounds like you are doing. This is essentially creating a new interpreter every single time you run your project. If you're using a decent IDE, such as PyCharm, you should be able to set this up properly through the project settings.
There are plenty of tutorials out there regarding setting up venv and virtualenv on your computer, and given that we don't know much about your system, your best bet is doing some further research.
Essentially, you need to
Create a venv for the project (and a new one for each project you make from here on out, unless you want to use the machine's interpreter)
Set the project interpreter to the venv you just created. Place any external libraries within ...\venv\Lib\site-packages
Hope this helps.

virtualenv and Django directories structure

I was trying to use virutalenv in windows but there is something odd that I barely understand about the directories structure.
When you create a new virtual environemnt it creates for you the following structure:
C:\Projects\djang_venv\
Include\
Lib\
pip-selfcheck.json
Scripts\
tcl\
If I want to work on my django project where should I put my project, in the django_vent directory ?
C:\Projects\djang_venv\
django_site\
Include\
Lib\
pip-selfcheck.json
Scripts\
tcl\
It's not looking right, like something is messy here.
Where should I put my application when I create a virtual environment ?
Found out that someone already asked the same question
And actually one of answers (Not the accepted one) was a very informative and clear (Will include his answer in my conclusion)
This is what I understood from the research I did on virtual environments world in Python:
First of all, it's a matter of opinion. But it is important to note that the experience of the people should be considered, because it is possible to know which method is more appropriate to choose, since the guys with experience understood which method was not good over time.
If you want to stick with virtualenv, one of the solutions keep your directories structure pretty clean outside, Projects directory will stay organized. Put all the virtual environments into one folder, and name each of them after the project you are working on:
c:\projects\virtualenvs\
django_site\ <-- Virtual environment for Django project
flast_site\ <-- Virtual environment for Flask project
c:\projects\django_site\ <-- Django project
c:\projects\flask_site\ <-- Flask project
But it's a bit messy with the source command:
cd /django_site
source ../virtualenvs/django_site/bin/activate
To get the most organized environment, without any headache about the order of the virtual environments directories, there is a wrapper for virtualenv called (surprisingly) virtualenvwrapper. All the virtual environments are stored away from you in your HOME_USER directory, for me it's c:\users\my_user\.virtualenvs. And you get great shortcuts by the package, like mkvirtualenv which creating for you a virtual environment no matter where are you in the file system, then you can switch between the virtual environments with the shortcut workon, Some examples:
$ mkvirtualenv flask_site
New python executable in c:\users\usr\.virtualenvs\flask_site\Scripts\python.exe
Installing setuptools, pip, wheel...done.
(flask_site)$ workon
flask_site
(flask_site)$ mkvirtualenv django_site
New python executable in C:\Users\Emilman\.virtualenvs\django_site\Scripts\python.exe
Installing setuptools, pip, wheel...
(django_site)$ workon
django_site
flask_site
(django_site)$ workon flask_site
(flask_site)$
Actually after checking all the options, I've chosen the virtualenvwrapper. great solution for virtual environments world in Python.

Associating a python project with a virtual environment

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.

Please suggest best practices for using Virtualenv in Pycharm on Windows

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/

Categories

Resources