pretend-engineer
pretend-engineer

Reputation: 33

Transferring a Python project to different computers

I am working on a Python project on both my work computer and my home computer. GitHub has made the experience pretty seamless.

But I'm having a problem with the pyvenv.cfg file in my venv folder. Because my Python SDK has a different file path on my work computer compared to my home computer, I have to manually go into pyvenv.cfg to change the home = C:\Users\myName\... filepath each time I pull the updated version of my project from my other computer, or else the interpreter doesn't work.

Does anyone know a solution to this problem?

Upvotes: 2

Views: 4969

Answers (2)

Grismar
Grismar

Reputation: 31319

As confirmed in the comments, you've added the virtual environment folder to your project and included it in the files that you put on GitHub.

That's generally a bad idea, since it defeats part of the purpose of having a virtual environment in the first place. Your virtual environment will contain packages specific to the platform and configuration of the machine it is on - you could be developing on Linux on one machine and Windows on another and you'd have bigger problems than just one line in a configuration file.

What you should do:

  • create a virtual environment in a folder outside your project / source folder.
  • assuming you're using pip, you can run pip freeze > requirements.txt to create a requirements.txt folder which you can then use on the other system with pip install -r requirements.txt to recreate the exact same virtual environment.

All you have to do is keep that requirements.txt up to date and update the virtual environments on either computer whenever it changes, instead of pulling it through GitHub.

In more detail, a simple example (for Windows, very similar for Linux):

  • create a project folder, e.g. C:\projects\my_project
  • create a virtual environment for the project, e.g. python -m venv C:\projects\venv\my_project
  • activate the environment, i.e. C:\projects\venv\my_project\Scripts\activate.bat
  • install packages, e.g. pip install numpy
  • save what packages were installed to a file in C:\projects\my_project, called requirements.txt with pip freeze > requirements.txt
  • store the project in a Git repo, including that file
  • on another development machine, clone or pull the project, e.g. git clone https://github.com/my_project D:\workwork\projects\my_project
  • on that machine, create a new virtual environment, e.g. python -m venv D:\workwork\venv\my_project
  • activate the environment, i.e. D:\workwork\venv\my_project\Scripts\activate.bat
  • install the packages that are required with pip install -r D:\workwork\projects\my_project\requirements.txt

Since you say you're using PyCharm, it's a lot easier still, just make sure that the environment created by PyCharm sits outside your project folder. I like to keep all my virtual environments in one folder, with venv-names that match the project names.

You can still create a requirements.txt in your project and when you pull the project to another PC with PyCharm, just do the same: create a venv outside the project folder. PyCharm will recognise that it's missing packages from the requirements file and offer to install them for you.

Upvotes: 3

AKX
AKX

Reputation: 168967

You shouldn't keep the full virtualenv in source control, since more often than not it's much larger than your code, and there may be platform-and-interpreter-version specific bits and bobs in there.

Instead, you should save the packages required -- the tried and tested way is a requirements.txt file (but there are plenty of alternatives such as Pyenv, Poetry, Dephell, ...) -- and recreate the virtualenv on each machine you need to run the project on.

To save a requirements file for a pre-existing project, you can

pip freeze > requirements.txt

when the virtualenv is active.

Then, you can use

pip install -r requirements.txt

to install exactly those packages.

In general, I like to use pip-tools, so I only manage a requirements.in file with package requirement names, and the pip-compile utility then locks those requirements with exact versions into requirements.txt.

Upvotes: 3

Related Questions