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.
Related
I need python3.6 for tensorflow installation, so I downloaded python3.6.12.tar. And I found that I should pip install tarfile. However, in this case it is an older version of python. FYI, In my computer(laptop) I installed python3.9.
My question is: can I pip install python.tar inside a virtualenv?
This is not how virtual environments work. I suggest you to do a little bit more research on virtual environments in Python.
Virtual Environments and Packages
Basically you need to install the necessary python version onto your machine. Then go ahead and use that specific python (which is version 3.6 in your case), to create a virtual environment with the command
~ /usr/bin/<path-to-python3.6> -m venv venv
This command will create a folder called venv. Now you need to source the activation script inside this folder to activate your environment.
Handy note: if you are dealing with different versions of python, a more robust way of handling such situations is via using a tool called pyenv.
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.
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/
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.