I am trying to install a python package basicsr through requirements.txt file, and I would like to set an environment variable before doing so.
The usual pip installation goes like this :
BASICSR_EXT=True pip install basicsr
and I would like to translate this in the requirements.txt file.
Can anyone help?
I don't think it's possible in requirements.txt.
If you're using virtualenv (you should), try this tool for virtual environment management: https://virtualenvwrapper.readthedocs.io/en/latest/
It includes scripts, such as postactivate: https://virtualenvwrapper.readthedocs.io/en/latest/scripts.html?highlight=postactivate#postactivate, that are run when the virtual environment is activated.
The local $VIRTUAL_ENV/bin/postactivate script is sourced after the new environment is enabled. $VIRTUAL_ENV refers to the new environment at the time the script runs.
Basically put this in the $VIRTUAL_ENV/bin/postactivate file:
BASICSR_EXT=True
Related
I'm a beginner to Django and Python, and I've never used virtualenv before. However, I do know the exact commands to activate and deactivate virtual environments (online search). However, this learning course takes time and sometimes I need to split the work over 2 days.
When I create a virtualenv today and do some work, I'm unable to access the same virtualenv tomorrow. Even when I navigate to that folder and type in .\venv\Scripts\activate, it says "system cannot find specific path".
How can I open already existing virtual environments and the projects within them? Could it be that I need to end my previous session in a certain way for me to access it the next time?
Even though pipenv had so many problems. I suggest you use it when you are new to virtual env.
Just
pip install pipenv
cd $your-work-directory
pipenv shell
Then you created your project env.
You can active it by:
cd $your-work-directory
pipenv shell
You can install packages by:
cd $your-work-directory
pipenv install $yourpackage --skip-lock
Open the command prompt
Go to the project directory, where you created virtual environment
And type the same as error shows, as in my case it was
File D:\Coding files\Python*recommendation-system\venv\Scripts\activate.ps1* cannot be loaded because running scripts is disabled on this system.
So I typed recommendation-system\venv\Scripts\activate.ps1
And it resolved my problem.
Use this and it will work:
cd $working directory u have virtual env on
pipenv shell
you can use this command it worked for me for reusing your existing venv
$ workon then the name of your existing venv
I've been making a python project using pipenv, and I want to be able to run it in a terminal from any location on my (linux) system.
Specifically, say I have the following directory structure:
/home
/project
Pipfile
main.py
/other_dir
I would like to be able to make an alias that allows me to call main.py like so:
/home/other_dir$ alias_to_my_proyect --some args
and run it in the virtual env, having the same behaviour as
/home/project$ pipenv run python main.py
But in another directory.
If it weren't a pipenv project, I'd just use a shebang a the start of the file and then add an alias to it in my .bashrc, but I want to use pipenv's virtual environment, but I cant find a way to do this with pipenv.
If you want to use a specific python environment for your script you will need to point it to the interpreter of that environment. On Mac the default is that pipenv installs all virtualenvs to /Users/<user_name>/.local/share/virtualenvs/ however that can be set to different locations as described in the manual:
Pipenv automatically honors the WORKON_HOME environment variable, if you have it set — so you can tell pipenv to store your virtual environments wherever you want, e.g.:
export WORKON_HOME=~/.venvs
In addition, you can also have Pipenv stick the virtualenv in project/.venv by setting the PIPENV_VENV_IN_PROJECT environment variable.
You can find out where the exact location of the virtualenv is with pipenv --venv inside your project folder. It returns something like /Users/reedef/.local/share/virtualenvs/project-BpR9WgCa. The interpreter is in ./bin/python of that location.
If we assume that you did not set any environment variable and you are using Mac than that means that you can write a script:
#!/usr/bin/env sh
/Users/reedef/.local/share/virtualenvs/project-BpR9WgCa/bin/python /home/project/main.py
and place it somewhere in your $PATH, e.g. /usr/local/bin/my_fancy_main to let it run in that specific environment.
Note: as mentioned by #Jon in the comments, -BpR9WgCa at the end of the path is stable as it is made from the project path:
hash = hashlib.sha256(location.encode()).digest()[:6]
It should be the same as long as the project path hasn't changed.
You can just use
#!/usr/bin/env pipenv-shebang
in your script after you install my pipenv-shebang package:
pip install pipenv-shebang
You should use the standard setuptools library to write a setup.py file. In particular you can write an entry_points section that names your main script:
entry_points={
'console_scripts': [
'alias_to_my_project = project.main.main'
]
}
Once you've done this, you can activate and install your package into your virtual environment
pipenv install -e .
# or without pipenv
. ~/vpy/bin/activate
pip install -e .
This will create a wrapper script in $VIRTUAL_ENV/bin/alias_to_my_project that loads the project.main Python module and calls its main function.
The wrapper script knows about the virtual environment and can be called directly without specifically activating the virtual environment. So you can do something like
ln -s $VIRTUAL_ENV/bin/alias_to_my_project $HOME/bin/alias_to_my_project
PATH=$HOME/bin:$PATH
and it will always be available.
I am using Virtualenv to learn Python. The author of the book I am reading wants no system wide access of Python available during learning, so we created a virtual environment via virtualenv. This is not built-in Python 3 virtual environment functionality, it is the pip virtualenv. It's an issue for me because I cannot figure out how to run a script while inside the virtualenv. Virtualenv's documentation reads that activation (or path naming) isn't required when running from within the virtual environment's directory and although I have moved my file both there and within the Scripts directory, I cannot run it while inside the virtualenv environment. Any help? I am using Python 3.6.1. The code I'm trying to run is:
def local():
m=7
print(m)
m=5
print(m)
I realize it's not even training wheel code, but what I'm trying to ultimately do is be able to run code from within the virtual environment to follow as the book suggests. I'm also using a fully updated Windows 10 OS.
What happens when I run the script is this:
(.virtualenv) c:\users\aiii> cd c:\users\aiii\desktop\learning.python\.virtualenv
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>scopes1.py
'scopes1.py' is not recognized as an internal or external command, operable program or batch file.
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>python scopes1.py
python: can't open file 'scopes1.py': [Errno 2] No such file or directory.
(.virtualenv) c:\users\aiii\desktop\learning.python\.lpvenv>
I have placed the script both directly in the learning.python folder where the environments are contained c:\users\aiii\desktop\learning.python\.lpvenv and inside the .lpvenv folder in the Scripts folder since that is where other scripts run from within the virtualenv pip are at c:\users\aiii\Desktop\learning.python\.lpvenv\Scripts\
First, install Virtualenv:
sudo apt-get install python-virtualenv
Then Create Virtualenv:
virtualenv venv #venv is name
For activating virtualenv.First, move to folder, In which you want to enable and run this command:
source venv/bin/activate
Once, Your work is done then disable virtualenv:
deactivate
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 installing virtualenv and it seems to access the system site packages before accessing the local site packages. Ipython is required by some other programs so it was automatically installed. This only happened recently and now it finds that version instead of the one found locally in the environment.
How do I tell the environment to use local packages within the environment before global packages? Can you set the Path variable for within the environment?
Ended up being an error with previously had set the PYTHON_PATH variable in .bashrc so this was looking in the system built directories before looking locally. Kind of defeating the purpose of virtual_env.
If you are using distribute + pip to manage dependencies simply run pip -l freeze > requirements.txt, this creates a dependency list of all your local packages. Next remove the current virtualenv; rerun the virtualenv command and specify the --no-site-packages option. Activate your new environment and finally pip install -r requirements.txt to download all the dependencies from the requirements file.