I have created a PyCharm project containing several Python scripts, it's using virtual environment. All is set-up and it's up and running. Windows 10.
I would now like to run the same Python scripts from within Cygwin command line. Is there a way to reuse the virtual environment created by PyCharm (C:\Users\joe_doe\\.virtualenvs\prj_name)?
I would say: no, I don't believe it is possible, and even if it were it is not worth the trouble.
Virtual environments should probably be considered as throwaway things. Use something like pip freeze > requirements.txt to save the list of projects installed in the virtual environment. And then pip install --requirement requirements.txt to install these projects in a new environment. It is a good habit to curate the list of requirements and one should be comfortable with deleting and recreating virtual environments on a whim without fear of losing any information.
Related
This question already has answers here:
How can I set up a virtual environment for Python in Visual Studio Code?
(23 answers)
Closed 2 years ago.
just wanted to set up my new MacBookPro M1. As I want to organize my MB this time, i want to start using virtualenv.
So, what I've done so far:
installed brew
installed virtualenv
set up a dir, in there create my first env called sec_env
installed some packages for testing
Now I want to use my virtualenv:
I started it, source sec_env/dir/activate
AND now here we go, I want to code something in this env. So I start up my code-insiders and try to import the package i already installed....does not work ;( (EDIT1: Maybe i failed config it inside vs code?)
Do I missunderstand the use of virtualenv? I thought of it kinda like a virtual machine...So i can install package in need for one project and code it. But if i work on another, i would just switch, start up my vs-code again and keep writing on the other project.
Or is the problem just, that all the project I want to code have to be inside the dir of the virtualenv(sec_env)? At the moment , I have a dir virtualenvs where I store all my environments , start one up and change to desktop to work . And all the projects are on my desktop.
Would be awesome if someone give me any tipps on this, or another way to separate my different projects. I am super new to this topic, since I used different virtual-box images before...now i am forced to use something else...M1 :D !
Your understanding is generally correct as virtualenv are a way to keep projects' dependencies separated from each other, like a VM would.
Your code doesn't need to be in the same directory as where your virtual environment, but many people tend to organize it that way for the sake of convenience. That way you don't need to think about what venv you coded a project with since it's right there in the directory.
With your steps, I think you installed a package before activating the environment. Doing it in that order installs the package in your system site-packages, not your virtual environment packages. Before you install a package, you need to activate your environment. Also, it appears from How to tell Homebrew to install inside virtualenv? that homebrew doesn't support installing a package into a virtual env. So in order to install packages into a virtualenv, I would suggest using pip as your package manager.
So the sequence of commands would be...
source <path to virtualenv>/dir/activate
pip install <modules you want to install>
# Now you can run your code that references those installed modules.
I'm new to python, so please be gentle.
In learning python and writing my first few scripts, I quickly glossed over any tutorial sections on virtualenv, figuring it wouldn't provide me any benefit in my nascent stage.
I proceeded to hack away, installing packages as I went with pip3 install package
Now I've built something that is potentially useful to my organization, and I'd like to share it. In this case, I want to distribute it as a windows executable.
Before building this distribution, I figure it's now time to take the next leap from individual scripts to proper python projects. It seems like virtualenv should be part of that.
Given that I've installed a number of packages to my "base" python environment: in order to do development in a "clean" virtual environment, do I need to somehow "revert" my base python environment (i.e. uninstall all non-standard packages), or will virtualenv shield a project within a virtual environment from non-standard packages installed to my "base" environment?
If you are using the venv module there is --system-site-packages flag that will grant the created virtual environment access to the system-wide site-packages directory:
--system-site-packages
Give the virtual environment access to the system
site-packages dir.
Go install VirtualEnvWrapper first. After that, create a new virtualenv, activate it, and run pip freeze. You should see nothing in there because nothing is installed. Deactivate the env to go back to your 'Base' environment and pip freeze again. You will see all the installs you have.
A best practice is to create a requirements.txt file and version control it so everyone can use the same versions of the same packages. If you don't want to do this, simply activate your new virtual env and pip install everything you want.
You can specify separately the required libraries and check if they are installed and if not then you can install them automatically.
Have a look at:
https://packaging.python.org/discussions/install-requires-vs-requirements/
In my company I have a setup where I have an original canopy distribution installed. Through some batch process a virtual environment is then created of that which contains additional python packages.
The virtual environment works fine from pycharm, however, I have the following problems:
When starting pip or python from the command line, the original canopy installation seems to be started. Am I right in thinking that 'activating' the virtual environment simply means adjusting the path variables to folders of the virtual environment? How is this best done automatically? Does canopy or python provide a good script? I want pip to install packages to the virtual environment, which it currently doesn't.
What is the best way to create a new virtual environment based on the virtual environment I already have?
I know that with anaconda this would all be easier, but my solution needs to be based on pure python or canopy.
Not sure about your specific environment, but for python projects, I usually get by with
pip freeze > requirements.txt
to save the list of packages installed in a virtual environment to a file
and
pip install -r requirements.txt
to restore the packages on a new virtual environment.
I've used requirements.txt as the filename, but you can pretty much use any file name you want for this.
I am new to Python. Just installed Anaconda and everything works fine. also the documentation mentioned it is good to configure virtual environment.
Since Anaconda works like a virtual environment, I don't need to configure another virtual environment.
Right or wrong?
Wrong, but also right.
Even when using Anaconda, it is good to use virtual environments (conda env) for each project so that you avoid issues with conflicting dependencies between projects. For example, one project you are working on requires Python 2.7 + flask 0.9 (not Python 3 compatible), whereas another project requires Python 3.4 + flask 0.11. The easiest way to manage these different dependencies is via conda virtual environments.
Note that conda envs function in a similar way to standard virtual environments, but do have a few differences. You are right in that you don't need traditional Python virtual environments any more. You still need to set up a new environment for each project, but this now becomes a conda env.
An additional benefit to using virtual environments is that you can easily create a requirements file which only contains the packages required for that project:
conda env export > environment.yml
If you try to do this outside your virtual environment, then you will end up putting every package you've ever installed into your environment.yml file.
http://conda.pydata.org/docs/using/envs.html
It is better to have separate environment for every project. Maybe one project needs some package version 1.3 and other needs 1.6. So it is much easier to have an environment for each project then to have one for all.
If you had only one environment you would have to change update(change) packages every time you wanted to compile a project that needs some different versions.
While working on a new python project and trying to learn my way through virtual environments, I've stumbled twice with the following problem:
I create my virtual environment called venv. Running pip freeze shows nothing.
I install my dependencies using pip install dependency. the venv library starts to populate, as confirmed by pip freeze.
After a couple of days, I go back to my project, and after activating the virtual environment via source venv/bin/activate, when running pip freeze I see the whole list of libraries installed in the system python distribution (I'm using Mac Os 10.9.5), instead of the small subset I wanted to keep inside my virtual environment.
I'm sure I must be doing something wrong in between, but I have no idea how could this happen. Any ideas?
Update:
after looking at this answer, I realized the that when running pip freeze, the pip command that's being invoked is the one at /usr/local/bin/pip instead of the one inside my virtual environment. So the virtual environment is fine, but I wonder what changes in the path might be causing this, and how to prevent them to happen again (my PYTHONPATH variable is not set).
I realized that my problem arose when moving my virtual environment folder around the system. The fix was to modify the activate and pip scripts located inside the venv/bin folder to point to the new venv location, as suggested by this answer. Now my pip freeze is showing the right files.