0

I need to provide my version of Python and packages for a project.
How can I do that?

I tried:

sudo apt-get install python3-venv
virtualenv my-env -p python3
source tutorial-env/bin/activate

This should show installed packages, but it shows:

pip list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
pip (9.0.1)
pkg-resources (0.0.0)
setuptools (39.0.1)

This is also suspicious:

(tutorial-env) linux@LINUXMINT:~$ pip freeze > requirements.txt
(tutorial-env) linux@LINUXMINT:~$ cat requirements.txt
pkg-resources==0.0.0
(tutorial-env) linux@LINUXMINT:~$ python -m pip install -r requirements.txt
Requirement already satisfied: pkg-resources==0.0.0 in ./tutorial-env/lib/python3.6/site-packages (from -r requirements.txt (line 1))

And I cannot find requirements.txt in my directory.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Elena Greg
  • 1,061
  • 1
  • 11
  • 26
  • 2
    You create a virtualenv called my-env and then you activate a virtualenv called tutorial-env, any reason for that ? – NirO Apr 12 '21 at 02:46
  • I tried to follow the advice below. How to create virtualenv including my version of python and all versions of my packages, please? – Elena Greg Apr 12 '21 at 02:48
  • 1
    Your version of `pip` is fairly outdated. Perhaps an update might remove the deprecation warning. – astrochun Apr 12 '21 at 03:18
  • I have upgraded it pip --version --> pip 21.0.1 . However after pip list I still obtain pip (9.0.1) – Elena Greg Apr 12 '21 at 03:22
  • I think you have multiple installations of `python` and `pip`. A `which python` and `which pip` will show where these are installed. Since you're using the default `python` installation with your OS instead of a package manager, you can get the version of python using `python --version` or `python3 --version` – astrochun Apr 12 '21 at 14:41
  • just do: `python -m venv ./venv` to create a virtual environment `venv` in your current folder. Then activate:`source ./venv/bin/activate` (Linux) or `./venv/scripts/activate` (Windows) and pip install your dependencies in this virtual environment `pip install ...`. – Bruno Vermeulen Aug 22 '23 at 05:37
  • After having installed your dependencies in the virtual environment, you can create `requirements.txt` by `pip freeze > requirements.txt`. This tutorial is pretty good: https://realpython.com/python-virtual-environments-a-primer/ – Bruno Vermeulen Aug 22 '23 at 05:41

2 Answers2

1

Pass the version of python when creating your virtualenv like this:

virtualenv my-env -p python3

Nihal Harish
  • 980
  • 5
  • 13
  • 31
0

I think you should not use any external module for creating a virtual environment.

You can create a Virtual Environment by using venv attribute of Python in Command Line.


What is the syntax?

The syntax is pretty simple.

C:\>python -m venv path\to\where\you\want\to\create\it

It can be easily done, and you can get a reference from the example below:

C:\>python -m venv "C:\Users\Bhavyadeep\Desktop\Discord Bots\Bot" 1\Bot-1-env

Here the name Bot-1-env is the name of the folder which will be created on execution of the command, and it doesn't have to exist.

What if I am using an IDE (like VS Code), then how will I create a virtual environment?

Creating a Virtual Environment in an IDE is much easier than creating it using CMD. In CMD you need to specify the full path of the directory where the Environment has to be created whereas in an IDE you can create one using its own terminal and also there won't be any need of adding and full path to the directory.

Syntax for IDEs with their Terminals is:

C:\>python -m venv My-Env

This would simply create a Virtual Environment in the folder of the project you are working on in the IDE. If you still want to create it using full path you can do the same as above in the Terminal of IDE.


Example with Images and Code in One Step:

My target directory would be the Desktop for now to explain.

Write and Execute the command in Command Line.

I entered the line in the image and pressed Enter.

Here the name of the Folder would be Example-Venv and it doesn't exist. This command created a folder with that name and created that a Virtual Environment.

Command and Execution

This command created a folder, and it can be seen in the picture below.

Folder that was created!

Now you can use it anywhere you want by simply making this folder as the Interpreter.

How to set interpreter?

The following links would explain:


I was glad to help! If you still get any problem, please feel free to ask in the comments and I would gladly help you! :)

Thank You! :)

Mike Slinn
  • 7,705
  • 5
  • 51
  • 85
Bhavyadeep Yadav
  • 819
  • 8
  • 25
  • Thank you, I have created a folder, but I do not know how to continue. I don't use Pycharm, VS Code and Spyder, I write it only in a note. – Elena Greg Apr 13 '21 at 05:36
  • Oh! I am really sorry for I cannot answer this question! :( The only thing I can do is ask you to use VS Code. It is a powerful IDE and one of the best for Programming. Sorry Again! – Bhavyadeep Yadav Apr 13 '21 at 06:13
  • 1
    Don't be sorry. I am sorry for my ignorance. I am a beginner. – Elena Greg Apr 13 '21 at 06:15
  • So I would be of every help here. I can guide you about the VS Code IDE, if you want. :) – Bhavyadeep Yadav Apr 13 '21 at 06:16
  • How would it help to use VS Code IDE? I created requirements.txt, and now I need to compile some scripts on another computer than mine using the versions of packages in the *.txt file. – Elena Greg Apr 13 '21 at 06:21
  • This answers would help you with it: https://stackoverflow.com/questions/7225900/how-can-i-install-packages-using-pip-according-to-the-requirements-txt-file-from – Bhavyadeep Yadav Apr 13 '21 at 07:50
  • I strongly recommend to start using VS Code and to leave notepad behind you. It does not cost anything and provides you an excellent coding environment. The sooner you learn it the better. – Bruno Vermeulen Aug 22 '23 at 05:34