Is there a way to specify the location of the virtualenv executable for tox to use?
Here is the problem:
The company I work for uses its own patched python with its own virtualenv. That virtualenv executable is very old, and it cannot be used to install the dependencies needed for the project, so when the project is initialized and the virtual environment for that project is created, the virtualenv executable in that environment gets updated to a later version. When I activate that environment and try to run tox in it, tox uses that environment's virtualenv executable to create its own (that is, tox's) virtual environments. However, that updated virtualenv executable cannot create a correctly configured virtual environment for that patched python. To do that, I need to be able to call the initial 'old' patched virtualenv executable, and thus I need to be able to tell tox where to look for it.
Hope this explanation is not too convoluted.
Your only real option is to create a tox plugin that uses the patched virtualenv - see https://github.com/tox-dev/tox/blob/master/src/tox/venv.py#L698 and https://tox.readthedocs.io/en/latest/plugins.html#creating-a-plugin
Related
I am trying to create a new python 3.7 virtual environment on my local computer running Windows 8. I have python versions 3.6, 3.7, and 3.8 installed. Their exe's are named python36, python37, and python, respectively. All three are correctly added to PATH because I can enter each interpreter.
Within my new project's directory I tried to create a virtual environment with python37 -m venv env. It produced an error: Error: [WinError 2] The system cannot find the file specified, but it still created the directory. However the Scripts subfolder is empty except for pythonw.exe.
In this question someone suggests doing python37 -m venv env --without-pip. When I tried this, the activation/deactivation scripts were created, but the virtual environment is using python 3.8.
It is my understanding that venv will create the virtual environment with what ever python exe you use to call it, so I don't understand how this can happen. I've verified that python37 points to the correct place with where python37, and can even enter the 3.7 interactive interpreter.
The problem was that I renamed the python exe's. I don't know exactly what goes wrong, but presumably at some point venv tries to find python.exe and is thrown off by the name.
Changing them back to python.exe and differentiating between the versions with their location fixed the problem.
Edit:
Check out Eryk's comments for more details.
First create folder at any drive then go to that folder and install virtualenv package using pip.
pip install virtualenv
Then create your virtual environment.
mkvirtualenv myvirtualenv
Then use below command to activate virtualenv in windows.
myvirtualenv\Scripts\activate
After this you can install related package in current virtual environment.
The Python Standard Library for Creating Virtual Environment
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'm using Python 3.4 on Windows.
I have created a virtual environment with
python c:\Python34\Tools\Scripts\pyvenv.py foo
Then activated it
foo\Scripts\activate.bat
And installed several libraries in it.
Question: Is it safe to rename the venv folder "foo" to "bar"?
I.e. once i have renamed foo to bar, then activate it with
bar\Scripts\activate.bat
will it still work?
What could make problems are any environment variable settings and absolute paths.
To be able to do that you should use virtualenv. From the docs:
Normally environments are tied to a specific path. That means that you cannot move an environment around or copy it to another computer. You can fix up an environment to make it relocatable with the command:
$ virtualenv --relocatable ENV
This will make some of the files created by setuptools or distribute use relative paths, and will change all the scripts to use activate_this.py instead of using the location of the Python interpreter to select the environment.
Note: you must run this after you've installed any packages into the environment. If you make an environment relocatable, then install a new package, you must run virtualenv --relocatable again.
But be aware:
The --relocatable option currently has a number of issues, and is not guaranteed to work in all circumstances. It is possible that the option will be deprecated in a future version of virtualenv.
I have completely shifted my all packages to virtualenv, but my project files were generated by the global Django installation.
I want to know what changes I need to make to the manage.py file, and do I need to use the virtualenv django-admin.py file now?
So long as you have all the packages you need, including Django, installed in the virtual environment, and you've activated the virtual environment by sourcing its activate script, you'll have no problem. The one thing to mind for is that you should run manage.py like this:
$ python manage.py
Rather than this:
$ ./manage.py
The former will ensure that you're using the virtual environment's Python interpreter rather than the globally installed one.
Once you've activated the virtual environment, you'll be using its django-admin.py file rather than the globally installed one.
No, only thing what virtualenv does, is that it creates an environment that has its own installation directories, that doesn’t share libraries with other virtualenv environments (and optionally doesn’t access the globally installed libraries either). Therefore it just means, that your project will use libraries and packages from virtualenv. So you won't have to change your manage.py.