Create Virtual Environment using Python Documentation - python-3.x

I am very new at command line usage. I am using python 3.7.2, Bash and VSCode Integrated Terminal. I am trying to create a virtual environment using venv and following python documentation:
https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments
The command to use is this one:
$ python3 -m venv test-env
and I get:
bash: python3: command not found
Later I have found a similar answer in an stackoverflow post:
How to create and activate virtual environment in windows 10 using bash command
And I use the command:
py -m virtualenv test-env
and I get this:
No module named virtualenv
I am very new using the command line so i don´t really know what is going on and how to work it around.

Hi i can see that you are using two different tools to create your environment.
Those are "venv" and "virtualenv".
Venv is a library that already comes with your python installation.
Virtualenv is an external one.
I had the same problem before and the solution is very simple.
I recommend you to stick with venv because it works pretty ok and you don´t need to do extra job installing external libraries.
So for solving your problem the Bash Shell is telling you that the command Python3 has not been found.
So try instead just:
python -m venv test-env
Sometimes Python documentation is not accurate enough and I know when you start using commands, accuracy in the sintax is extremely important.

Try this steps,it'll helped you:
First, make a directory :
mkdir testing
Then, moved to this directory named testing :
cd testing
When you type following command in this directory:
python3 -m venv env (OR, python -m venv env)
You got error like :
The virtual environment was not created successfully because ensurepip is not
available. On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
apt install python3.8-venv
Type the following command but before that keep an eye on the version of python you installed on the machine; in my case its python3.8
sudo apt install python3.8-venv
Now, we can create a virtual environment and store its tools in the "bhandari" folder .
python3 -m venv bhandari
Note: you can named this "bhandari" folder; anyname you like( Standard practice is to name it "env" ...)
Now to activate your virtual environment, from the directory of your folder, type the following command this will activate our virtual environment in the “bhandari” folder
source bhandari/bin/activate
If you have successfully activated your virtual environment, you should see the (bhandari) word indicating that we are working in a virtual environment.
After this, we can install anything that will be isolated from the rest of the system....

Related

Error while trying to run snakemake in a venv on a protected server

For a project I made a virtual environment (venv) using Python3. I installed all the necessary dependencies using a simple bash script (see picture below) after I activated my venv. (I verified the installed packages using: pip3 list and concluded that every dependency was installed succesfully.)
My project uses snakemake, so I ran this snakemake commando:
snakemake --snakefile Snakefile.py all
I get this error:
I know it has to do something with the venv, because without the venv snakemake runs perfectly. I have read the Snakemake installation documents and it says I have to install conda and make & activate a conda venv. But, I do not have the sudo privileges to download and install conda (I work on a protected server).
What is happening and does someone know a fix?
One possible reason could be the difference in Python versions. What version of Python does the pip3 prepare environment for?
As I can see from the picture provided, the invalid syntax may be because of the version of Python doesn't support f-strings.
Imagine the following two scenarios: when you run Snakemake manually, you use the latest Python3 (e.g. 3.9). But if the pip3 is configured for an older version (e.g. 3.5), you can configure a very different environment for Python3.5 that doesn't support f-strings.

Dockerfile Build Fails - Pipenv and Pyenv Multiple Versions of Python Found

Question: How can I fix the Dockerfile to properly freeze requirements.txt and successfully build?
I am working through deploying a Dockerfile of a Python script utilizing Pyenv and Pipenv for local development.
On the build step where the Piplock file is frozen to requirements.txt, I receive the following error:
Error: Invalid value for "--python": Expected Python at path
/Users/jz/.local/share/virtualenvs/quanter-TP0oWHoL/bin/python3 does
not exist
My Dockerfile is:
FROM python:3.7
RUN pip install pipenv
COPY Pipfile* /tmp/
RUN cd /tmp && pipenv --python /Users/x/.local/share/virtualenvs/quanter-TP0oWHoL/bin/python3 lock --requirements > requirements.txt
ENV RDKAFKA_INSTALL=system
RUN pip install -r /tmp/requirements.txt
COPY . /tmp/app/
RUN pip install /tmp/app/
CMD ["python", "./tmp/app/main.py"]
Creation of the local Pipenv environment provided this information about the interpreter, (which was used in the Dockerfile):
Using /usr/local/opt/pyenv/versions/3.8.0/bin/python3 (3.8.0) to
create virtualenv… ⠙ Creating virtual environment...Using base prefix
'/usr/local/opt/pyenv/versions/3.8.0' New python executable in
/Users/x/.local/share/virtualenvs/quanter-TP0oWHoL/bin/python3 Also
creating executable in
/Users/x/.local/share/virtualenvs/quanter-TP0oWHoL/bin/python
Pyenv is using 3.8.0 locally:
pyenv versions
system
3.7.5
* 3.8.0 (set by /Users/x/Projects/quanter/.python-version)
Any help getting this working would be greatly appreciated! Thank you.
The original error came from the idea that pipenv can not find python executable on the given path. It is hardcoded, but on every fully new build, pipenv will create env with a different name. Take a look at this interesting discussion: How does pipenv know the virtualenv for the current project?, it seems like it could be predictable, but there should be a better solution.
Firstly, you do not need to specify a path to python executable directly because python image comes only with one system python installation, e.g. it will be available by default via python (of course you can create different environments with different versions of python by hand).
Secondly, you can handle pipenv in docker env in a more nicer and preferable way, instead of converting pipenv into pip like flow. Take a look at the example below:
FROM python:3.7
COPY Pipfile /
COPY Pipfile.lock /
RUN pip3 install pipenv \
&& pipenv install --system --deploy --ignore-pipfile
Several notes worthing to consider:
Commit Pipfile.lock into VCS. In this case, you can be sure that in every environment you have exactly the same versions of packages and its dependencies.
You may be interested in --system option. That says to install dependencies into a system instead of virtualenv (this is what you expect). But according to this answer: https://stackoverflow.com/a/55610857/9926721 It is not officially recommended. But still, a quite popular project wemake-django-template uses it as well.
Consider splitting up environments. There is useful flag --dev for it. No more several requirement files. Handle it more efficient way.

`python -m venv foo` does not install pip inside the nested environment

I needed python3.7 for a project and it wasn't available as a ubuntu package, so I bootstrapped a python3.7 environment using conda. From that 'grandparent' environment, I created a 'parent' python3 virtual environment for the specific project.
From the parent environment in my project, I'm running a command line tool from the google-cloud-sdk which creates another test environment. That script creates a 'grandchild' environment by calling something equivalent to (parent) $ python3 -m venv /tmp/grandchild.
That grandchild environment doesn't have the pip binary installed, for some reason or another. And this is the problem. This missing pip causes the google script to fail to install dev-test dependencies. The parent, and the child do have pip installed, however, but pip doesn't get passed down.
When I take conda out of the picture, and rely only on the python that ships with my ubuntu package system (under /usr/lib), I can nest my virtual environments ad nauseam, and pip always seems to get inherited properly. I think this is something specific to python/pip conda environments that I'm tripping on.
I think this is a separate cause problem to this: pip missing from Python venv
(I don't have that file ~/.pydistutils.cfg anywhere on my box)
UPDATE:
I've found a way to reliably repro this, and without the need for conda. This happens when the parent is created with virtualenv, i.e. virtualenv parent, and the child gets created from that parent using -m venv, i.e. (parent) $ python3 -m venv child. The child then doesn't get pip copied into it.
Nesting environments created with virtualenv works fine, and nesting environments created with venv works well too, but not when venv is used within a virtualenv-created environnment. They don't mix.
Note: environments don't really "nest" per-se, they are independent copies. I mean that one is created from the other.
One workaround is to create all the environments with the same method.
e.g. use python3 -m venv for all environments along the chain.
python3 -m venv parent; source parent/bin/activate
(parent) $ python3 -m venv child
If that seems to tangle you into a web of symlinks, you may also provide the --copies flag: python3 -m venv --copies …, which can avoid that.

How to get virtualenv to run Python 3 instead of Python 2.7?

On Mac, if I simply open a new terminal window and run:
python --version
I get:
3.6
but if I do this:
virtualenv venv && source venv/bin/activate
and then, in that environment, I run :
python --version
I get:
2.7
I need virtualenv to run 3.6. How do I do that?
This :
/usr/bin/python
is 2.7 but this:
/usr/local/bin/python
is 3.6. The path for my normal user has /usr/local/bin come up before /usr/bin/. Is virtualenv running as someone else? How do I control its path?
I ran this:
virtualenv -p /usr/local//Cellar/python/3.6.5/bin/python3 venv
but then I do this:
virtualenv venv && source venv/bin/activate
and I'm running in an environment with 2.7.
On Python 3 you don't need virtualenv script anymore, you should just use the venv module included with standard lib:
python3 -m venv myvenv
But if you really want to keep using the old virtualenv script, you can - specify the interpreter explicitly with the -p option:
virtualenv -p /path/to/python3 myvenv
The easiest way is to change python globally to Python3 as I think you're using it more often than Python 2.7 (or hopefully always). To achieve this, add the following line of code at the end of your .bash_profile:
alias python='python3'
virtuanenv is using /usr/bin/python, hence it should work now.
If you don't want to change it globally, than you should use the following command to create your Python3.6 virtual environment:
python3 -m venv venv
or the explicit Python version if you have multiple Python3 versions installed:
python3.6 -m venv venv
On more suggestion at the end: I recommend you to read something about pipenv as it's the new recommended way to handle virtual environments and your whole package management at once. It's super easy and fixes a lot of common issues. Here's a nice article from realpython.com on that topic.
Hope I could help you. Have a nice day.

error creating python3 virtualenv for readthedocs

I am trying to use a local instance of readthedocs to build documentation for a project written in python 3. (I already have successfully built documentation for one python 2 project).
When I select the option to set The Python interpreter used to create the virtual environment to CPython3.X in the advanced settings, then the build fails, with the command
python3.5 -mvirtualenv --no-site-packages --no-download /home/user/rtd/checkouts/readthedocs.org/user_builds/project/envs/latest
producing the error /usr/bin/python3.5: No module named virtualenv.
However pip3 install virtualenv yeilds Requirement already satisfied: virtualenv in /usr/local/lib/python2.7/dist-packages. I've tried switching to using conda but am having problems with that. Is there anyway to use virtualenv with the global python 3 installation?
I know from the installation instructions that
you’ll need to install Python 3 with virtualenv in your system as well.
However it is not clear to me what they are saying there. Could someone clarify?

Resources