"pipenv install" in empty directory doesn't create a Pipfile, but uses a Pipfile.lock from somewhere else - pipenv

I ran pipenv install to create a Pipfile in the current directory that doesn't have a Pipfile. It gave the following output but did not create a Pipfile. Why not?
Installing dependencies from Pipfile.lock (639627)…
🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 0/0 — 00:00:00
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
It looks like it found a Pipfile.lock somewhere and used it? (similar to git behavior)

Use the PIPENV_NO_INHERIT environment variable to ignore inheriting from directories above the current directory, e.g.,
PIPENV_NO_INHERIT=True pipenv install
In your case, pipenv searched directories above the current directory and found a Pipfile there that it used (the location of which can be seen with pipenv --where).
(Incidently, I looked at the pipenv documentation but was unable to find where it discussed this behavior, so please add a link here to that documentation if you find it.)

I ran pipenv shell first (creat Pipfile), then pipenv install (creat Pipfile.lock)
'pip install pipenv' command returns "Requirement already satisfied ..."

Related

How to correctly activate pipenv shell in zsh?

I'm currently working on a simple Python script and using pipenv to manage dependencies.
Here's the problem:
When running pipenv shell and then python script.py, I'm getting module not found errors for things that are definitely in Pipfile (e.g. requests).
Here's what's interesting:
I don't get these problems when I use pipenv run python script.py and I don't get these problems when I use bash instead of zsh, i.e. when I do:
bash
pipenv shell
python script.py
the script runs happily and resolves the imports. I tried re-installing pipenv using Homebrew, but zsh still doesn't correctly activate my pipenv environment. Any ideas on how to get pipenv to work with zsh or why this would even be problematic?
Using zsh:
Using bash:

Why does pipenv add extension to the end of the name?

I have a project that is called "sportanalitica". When I run pipenv shell it enters the virtual environment and I see "(sportanalitica-KnWqfC-u)". Also if I install anything it says
Pipfile.lock (295b1e) out of date, updating to (74c969)…
Is there any solution or anything wrong?
No, nothing is wrong. That's just how pipenv works.
I run pipenv shell it enters the virtual environment and I see - (sportanalitica-KnWqfC-u)
That's just how pipenv names the virtual environment when it is created: <folder-name>-<hash>. That will also be an actual folder on your machine. See pipenv docs Virtualenv mapping caveat:
Pipenv automatically maps projects to their specific virtualenvs.
The virtualenv is stored globally with the name of the project’s root directory plus the hash of the full path to the project’s root (e.g., my_project-a3de50).
~$ mkdir sportanalitica
~$ cd sportanalitica/
sportanalitica$ pipenv shell
Creating a virtualenv for this project...
Pipfile: /Users/me/sportanalitica/Pipfile
...
✔ Successfully created virtual environment!
Virtualenv location: /Users/me/.venvs/sportanalitica-3tdk8qV0
Creating a Pipfile for this project...
...
(sportanalitica) sportanalitica$ pipenv --venv
/Users/me/.venvs/sportanalitica-3tdk8qV0
(sportanalitica) sportanalitica$ ll /Users/me/.venvs/sportanalitica-3tdk8qV0
total 8
drwxr-xr-x 23 cerberus staff 736B Dec 30 13:07 bin/
drwxr-xr-x 3 cerberus staff 96B Dec 30 13:07 lib/
-rw-r--r-- 1 cerberus staff 470B Dec 30 13:07 pyvenv.cfg
That's how pipenv knows that every time you run pipenv shell inside that same sportanalitica folder, it will find the existing sportanalitica-3tdk8qV0 virtual environment, and then you can just reuse it ever time.
if I install anything it says Pipfile.lock (295b1e) out of date, updating to (74c969)…
This is also part of pipenv's basic features. When you change something in your virtual environment (i.e. install a new package, uninstall a package, update a version), it will "compute" a new Pipfile.lock file to track the updates:
$ pipenv lock
$ pipenv lock is used to create a Pipfile.lock, which declares all dependencies (and sub-dependencies) of your project, their latest available versions, and the current hashes for the downloaded files. This ensures repeatable, and most importantly deterministic, builds.
The locking is automatically done when you pipenv install/pipenv uninstall. Locking is also part of pipenv's security features.
Basically, what you just did (pipenv shell + pipenv install) follows the Example Pipenv Workflow for pipenv. I recommend going through the Basic Usage of Pipenv docs.

Manage the path to the venv for pipenv

Is it possible to tell pipenv where the venv is located? Perhaps there's something you can put in the pipfile, or something for the .env file?
I fairly frequently have to recreate my venv because pipenv seemingly loses track of where it is.
For example, I started a project using Pycharm to configure the file system and create my pipenv interpreter. It created the venv in ~/.local/share/virtualenvs/my-project-ZbEWMGNA and it was able to keep track of where that interpreter was located.
Switching to a terminal window & running pipenv commands then resulted in;
Warning: No virtualenv has been created for this project yet! Consider running pipenv install first to automatically generate one for you or seepipenv install --help for further instructions.
At which point I ran pipenv install from the terminal & pointed pycharm at that venv, so the path would become ~/apps/my-project-ZbEWMGNA (which sits alongside the project files ~/apps/my-project)
Now I've got venvs in both paths and pipenv still can't find them.
mwalker#Mac my-project % pipenv --where
/Users/mwalker/apps/my-project
mwalker#Mac my-project % pipenv --venv
No virtualenv has been created for this project yet!
Aborted!
mwalker#Mac my-project % ls ~/apps
my-project
my-project-ZbEWMGNA
mwalker#Mac my-project % ls ~/.local/share/virtualenvs
my-project-ZbEWMGNA
Yes, it is possible by setting environment variables. You can set a path for virtual environments via the WORKON_HOME. Or have the virtual environment created in the project with PIPENV_VENV_IN_PROJECT.
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
-- https://pipenv-fork.readthedocs.io/en/latest/advanced.html#custom-virtual-environment-location
or
PIPENV_VENV_IN_PROJECT
If set, creates .venv in your project directory.
-- https://pipenv-fork.readthedocs.io/en/latest/advanced.html#pipenv.environments.PIPENV_VENV_IN_PROJECT
In my experience, PyCharm will uses the existing venv created by Pipenv. Otherwise it will create it in the directory that PyCharm is configured to create it.

pipenv virtual environment depends on current directory?

I am new to pipenv so there might be something I'm not understanding here. However it seems like the virtual environment which is created depends on the current directory, which seems bad to me.
Here is what I did:
Checked out code from Github which already had Pipfile and Pipfile.lock
Did some unrelated stuff... at this point I was in a directory called /home/user/me/miniconda3/bin/
Ran /home/user/me/miniconda3/bin/pipenv run python /home/user/me/my-script-dir/my-script.py
This caused Pipenv to create a virtual environment. Output:
Creating a virtualenv for this project...
Using /home/user/me/miniconda3/bin/python (3.6.4) to create virtualenv…
Already using interpreter /home/user/me/miniconda3/bin/python
Using base prefix '/home/user/me/miniconda3'
New python executable in /home/user/me/.local/share/virtualenvs/bin-YnM8YhRk/bin/python
Installing setuptools, pip, wheel...done.
Virtualenv location: /home/user/me/.local/share/virtualenvs/bin-YnM8YhRk
Creating a Pipfile for this project…
Then I realized that I needed to run pipenv install so this time I cd'd to the directory where the script is actually stored, /home/user/me/my-script-dir/, and ran /home/user/me/miniconda3/bin/pipenv install. Then I got this output:
Creating a virtualenv for this project…
Using /home/user/me/miniconda3/bin/python (3.6.4) to create virtualenv…
Already using interpreter /home/user/me/miniconda3/bin/python
Using base prefix '/home/user/me/miniconda3'
New python executable in /home/user/me/.local/share/virtualenvs/my-script-dir-Ex37BY7g/bin/python
Installing setuptools, pip, wheel...done.
Virtualenv location: /home/user/me/.local/share/virtualenvs/my-script-dir-Ex37BY7g
Installing dependencies from Pipfile.lock (6c24e4)…
So as you can see I actually was running the same script each time, but somehow it created two different virtual environments. And the virtual environments are named after what happened to be my current directory at the time, not the directory of the script. This seems like it would be very unwieldy unless I am missing something.
You are correct, the virtualenv Pipenv uses does depend on the current directory.

envsubst command getting stuck in a container

I have a requirement that before an application runs, some part of it needs to read the environmental variable. For this I have the following docker file
FROM nodesource/jessie:0.12.7
# install gettext for envsubst
RUN apt-get update
RUN apt-get install -y gettext-base
# cache package.json and node_modules to speed up builds
ADD package.json package.json
RUN npm install
# Add source files
ADD src src
# Substiture value for backend endpoint env var
RUN envsubst < src/js/envapp.js > src/js/app.js
ADD node_modules node_modules
EXPOSE 8000
CMD ["npm","start"]
The above envsubst line reads (should read) an env variable $MYENV and substitutes it. But when I open the file app.js, its empty.
I checked if the environmental variable exists in the container and it does. Any reason its value is not read and substituted?
I also tried the same command in teh container and it works. It only does not work when I run the image
This is likely because $MYENV is not available for envsubst when you run the image.
Each RUN command runs on its own shell.
From the Docker documentations:
RUN (the command is run in a shell - /bin/sh -c - shell form)
You need to source your profile as well, for example if the $MYENV environment variable is available in the .bashrc file, you can modify your Dockerfile like this:
RUN source ~/.bashrc && envsubst < src/js/envapp.js > src/js/app.js
I encountered the same issues, and after much research and fishing through the internet. I managed to find a few work arounds to this issue. Below I'll list them and identifiable risks at the time of this "Answer post"
Solutions:
1.) apt-get install -y gettext its a standard GNU package language library, one of these libraries that it includes is envsubst` and I can confirm that it works for docker UBUNTU:latest and it should work for every flavored version.
2.) npm install envsub dependent on the "use case" - this approach would be better supported by node based projects.
3.) enstub cli project lib in my opinion it seems a bit overkill to downloading a custom cli from a random stranger but, it's also another option.
Risk:
apt-get install -y gettext:
1.) gettext - this approach would NOT be ideal for VM's as with any package library, it requires maintenance and updates as time passes. However, this isn't necessary for docker because once an a container is initialized and up and running we can create a bashscript to add the package, substitute env vars and then remove the package.
2.) It's a bad idea for VM's because it can be used to execute arbitrary code
npm install ensub
1.) envsub - updating packages and this approach wouldn't be ideal if your dealing with a different stack and not using nodejs.
NOTE:
There's also a PHP version for those developing a PHP application and it seems to work PHP's cli if you need a custom environment.
Resources:
GetText package library info: https://www.gnu.org/software/gettext/
GetText Risk - https://ubuntu.com/security/notices/USN-3815-2
PHP-GetText - apt-get install -y php-gettext
Custom ensubst cli: https://github.com/a8m/envsubst
I suggest that since your are using Node, you use the npm envsub module.
This module is well tested and is developed with docker in mind.
It avoids the need for relying on other dependencies when you already have the full Node arsenal at your fingertips.
envsub is described as
envsub is envsubst for NodeJS
NodeJS global CLI module providing file-level environment variable substitution via Handlebars
I am the author of the package. I think you will enjoy it.
I had some issues with envsubst in Docker.
For some reasons envsubst doesn't work when I try to copy the output in the same file. For example, this is not working:
RUN envsubst < file.conf > file.conf
But when I when I tried to use a temp file the issue disappeared:
RUN envsubst < file.conf > file.conf.temp && cp -f file.conf.temp file.conf

Resources