简体   繁体   中英

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

PS 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 foundhere .

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.

  1. 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 ;
  2. Activate your environment by navigating into its Scripts folder and executing activate .
  3. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM